【问题标题】:JButton will not change colour when I change it's colour当我改变它的颜色时,JButton 不会改变颜色
【发布时间】:2016-11-28 19:00:27
【问题描述】:

我有一排按钮都具有相同的颜色,绿色,除了“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;}
}

【问题讨论】:

  • 尝试从扩展类调用repaint方法而不是paint方法(在这个方法之外)this.repaint(),JButton在这种情况下是容器!
  • 我刚才试过了,必须在StudentButton parent = this这一行中添加,然后在mouseReleased方法中我调用了parent.revalidate()和parent.repaint(),但它仍然deosn'不工作
  • 如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。

标签: java swing button paint


【解决方案1】:

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

           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();
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 2014-09-24
    • 2015-09-22
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多