我有一排按钮,它们都具有相同的颜色,绿色,除了“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(),但它仍然不工作 – user217339
为了更好地提供帮助, ,张贴[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 –