当我更改颜色时,JButton不会改变颜色

时间:2016-11-28 19:00:27

标签: java swing button paint

我有一排按钮,它们都有相同的颜色,绿色,除了" selectedButton",它是橙色。当我点击一个按钮时,它变成新的selectedButton,因此它变为橙色,其他所有变为绿色。我的代码正确地将新JButton更改为橙色,但是不会将之前的JButton从橙色更改为绿色。

如果我在paintComponent()方法中调用revalidate()和repaint()它可以很好地工作,但有些东西告诉我这是一个糟糕的主意,因为repaint()方法只是再次调用paintComponent()方法,所以我最终可能会进行某种无限递归,但到目前为止我还没有确定原因。我有信心revalidate()和repaint()将解决问题,如果放在我的代码中的最佳位置,但我应该在哪里添加它们。在我更改了mouseReleased()事件上按钮的颜色后,我累了添加两个,但它没有用。

class StudentButton extends JButton{
    private int index;
    private Color startGradient, endGradient;   //Used for colouring JButton
    private static StudentButton selectedButton;

    public StudentButton(int i, boolean isSelected) { this("", i, isSelected); }
    public StudentButton(String text, int i, boolean isSelected){
        super(text); index = i;
        if(isSelected)
            StudentButton.setSelectedButton(this);
            //Colour set to orange
            startGradient = new Color(234, 249, 99);
            endGradient = new Color(230, 252, 35);
        }
        else {
            //Colour set to green
            startGradient = new Color(159, 255, 76);
            endGradient = new Color(101, 183, 29);
        }

        addMouseListener(new MouseListener() {
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}

            //mouseReleased better than mouseClicked, since it always triggers for an attempted click
            public void mouseReleased(MouseEvent e) {
                StudentButton selButton = StudentButton.getSelectedButton();
                selButton.setColourGradients(new Color(159, 255, 76), new Color(101, 183, 29));

                StudentButton newSelButton = (StudentButton)(e.getSource());
                newSelButton.setColourGradients(new Color(234, 249, 99), new Color(230, 252, 35));
                StudentButton.setSelectedButton(newSelButton);
                ////////////////////////////////////////////////
                ////////////////////////////////////////////////
                ////////////////////////////////////////////////
                //tried adding revalidate() and repaint() here, wouldn't work
                ////////////////////////////////////////////////
                ////////////////////////////////////////////////
                ////////////////////////////////////////////////
            }
            public void mouseClicked(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
        });

        setContentAreaFilled(false);   ///removes ugly button border
    }

    public static StudentButton getSelectedButton() { return selectedButton; }
    public static void setSelectedButton(StudentButton sb) { selectedButton = sb; }

    public void setColourGradients(Color start, Color end) {
        startGradient = start; endGradient = end;
        System.out.println("Button "+index+", set to colour"+start);
    }
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        GradientPaint p;
        p = new GradientPaint(0, 0, startGradient,
                0, getHeight(), endGradient);

        g2.setPaint(p);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setPaint(g2.getPaint());
        ////////////////////////////////////////////////
        ////////////////////////////////////////////////
        //This makes the buttons change correctly, but seems like a very bad idea                
        revalidate(); repaint();
        ////////////////////////////////////////////////
        ////////////////////////////////////////////////
        ////////////////////////////////////////////////

        super.paintComponent(g);
    }

    public int getIndex() {return index;}
}

1 个答案:

答案 0 :(得分:0)

感谢@Bo_Halim我意识到我在单个StudentButton对象上调用了repaint()和revalidate(),而不是在mouseReleased()方法中都进行了更改。我已经改变了mousePressed方法,以便重新选择旧的选定按钮和新选择的按钮。

           public void mousePressed(MouseEvent e) {
                StudentButton selButton = StudentButton.getSelectedButton();
                selButton.setColourGradients(new Color(159, 255, 76), new Color(101, 183, 29));
                selButton.revalidate(); selButton.repaint();

                StudentButton newSelButton = (StudentButton)(e.getSource());
                newSelButton.setColourGradients(new Color(234, 249, 99), new Color(230, 252, 35));
                StudentButton.setSelectedButton(newSelButton);
                newSelButton.revalidate(); newSelButton.repaint();
            }