我有一排按鈕,它們都具有相同的顏色,綠色,除了「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/)。 –