0

I have a gui that is basically a bunch of color buttons and by clicking on one of the button it changes the default color.

below is a minimum reproduction of the problem

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class minimumRepro extends JFrame implements ActionListener{
    JButton red, yellow, white, pink, orange, magenta, light_gray, green, gray, dark_gray, cyan, blue, black; 
    Color currentColor;

    public minimumRepro(){
        red = new JButton("red");
        red.setBackground(Color.red);
        red.addActionListener(this);

        yellow = new JButton("yellow");
        yellow.setBackground(Color.yellow);
        yellow.addActionListener(this);

        white = new JButton("white");
        white.setBackground(Color.white);
        white.addActionListener(this);

        pink = new JButton("pink");
        pink.setBackground(Color.pink);
        pink.addActionListener(this);

        orange = new JButton("orange");
        orange.setBackground(Color.orange);
        orange.addActionListener(this);

        magenta = new JButton("magenta");
        magenta.setBackground(Color.magenta);
        magenta.addActionListener(this);

        light_gray = new JButton("light_gray");
        light_gray.setBackground(Color.lightGray);
        light_gray.addActionListener(this);

        green = new JButton("green");
        green.setBackground(Color.green);
        green.addActionListener(this);

        gray = new JButton("gray");
        gray.setBackground(Color.gray);
        gray.addActionListener(this);

        dark_gray = new JButton("dark_gray");
        dark_gray.setBackground(Color.darkGray);
        dark_gray.addActionListener(this);

        cyan = new JButton("cyan");
        cyan.setBackground(Color.cyan);
        cyan.addActionListener(this);

        blue = new JButton("blue");
        blue.setBackground(Color.blue);
        blue.addActionListener(this);

        black = new JButton("black");
        black.setBackground(Color.black);
        black.addActionListener(this);

        JPanel colorButtonPanel=new JPanel();
        colorButtonPanel.setLayout(new GridLayout(12,1));//row,collum
        colorButtonPanel.setBackground(Color.WHITE);

        colorButtonPanel.add(black);
        colorButtonPanel.add(cyan);
        colorButtonPanel.add(dark_gray);
        colorButtonPanel.add(gray);
        colorButtonPanel.add(green);
        colorButtonPanel.add(light_gray);
        colorButtonPanel.add(magenta);
        colorButtonPanel.add(orange);
        colorButtonPanel.add(pink);
        colorButtonPanel.add(white);
        colorButtonPanel.add(yellow);
        colorButtonPanel.add(red);

        Container pane = this.getContentPane();
        pane.setLayout(new BorderLayout()); 
        pane.add(colorButtonPanel, BorderLayout.EAST);
        pack();
        setVisible (true);

    }

    public static void main(String[] args) {
        minimumRepro m = new minimumRepro();
        System.out.println("this is a minium reproduction");
    }

    public void actionPerformed (ActionEvent e) {
        String action = e.getActionCommand();// string of the button clicked
        System.out.println("this is the buttonn clicked "+action);
        switch(action) {
        case "black":
            currentColor = Color.black;
            System.out.println("this is the current color "+ currentColor);
        case "cyan":
            currentColor = Color.cyan;
            System.out.println("this is the current color "+ currentColor);
        case "dark_gray":
            currentColor = Color.darkGray;
            System.out.println("this is the current color "+ currentColor);
        case "gray":
            currentColor = Color.gray;
            System.out.println("this is the current color "+ currentColor);
        case "green":
            currentColor = Color.green;
            System.out.println("this is the current color "+ currentColor);
        case "light_gray":
            currentColor = Color.lightGray;         
            System.out.println("this is the current color "+ currentColor);
        case "magenta":
            currentColor = Color.magenta;
            System.out.println("this is the current color "+ currentColor);
        case "orange":
            currentColor = Color.orange;
            System.out.println("this is the current color "+ currentColor);
        case "pink":
            currentColor = Color.pink;
            System.out.println("this is the current color "+ currentColor);
        case "white":
            currentColor = Color.white;
            System.out.println("this is the current color "+ currentColor);
        case "yellow":
            currentColor = Color.yellow;
            System.out.println("this is the current color "+ currentColor);
        case "red":
            currentColor = Color.red;
            System.out.println("this is the current color "+ currentColor);
        default:
            currentColor = Color.black;
        }

    }

}

Whats troubling me is that for some reason the program is incorrectly executing the switch case in many cases executing it more than once. And very often performing the wrong action. Even though the switch seems to be correct.

an example of the expected output should be

this is a minimum reproduction this is the button clicked black this is the current color java.awt.color[r=255,g=255,b=255]

Thanx in advance

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Icarus
  • 501
  • 2
  • 16

0 Answers0