Java 使用ActionListner更改JTextField的颜色
我只是想当我再次点击按钮时,颜色变回白色 这是代码,我必须让它改变第一次点击Java 使用ActionListner更改JTextField的颜色,java,swing,user-interface,Java,Swing,User Interface,我只是想当我再次点击按钮时,颜色变回白色 这是代码,我必须让它改变第一次点击 private void colourButton() { inputField.setBackground(Color.WHITE); } // listener method public void actionPerformed( ActionEvent e) { inputField.setBackground(Color.RED); resultMessage.setText("C
private void colourButton()
{
inputField.setBackground(Color.WHITE);
}
// listener method
public void actionPerformed( ActionEvent e)
{
inputField.setBackground(Color.RED);
resultMessage.setText("Colour Button Pressed");
}
现在我只想再次单击它,输入字段的颜色应该会变回白色。到目前为止,您已经有了代码,可以在单击按钮时更改文本字段的颜色,因此,您现在所需要的似乎只是在中添加一个条件语句,以根据它已经是什么颜色来决定将其更改为什么颜色
if(inputField.getBackground() == Color.RED)
{
inputField.setBackground(Color.WHITE);
}
else
{
inputField.setBackground(Color.RED);
}
public void actionPerformed( ActionEvent e)
{
if (inputField.getBackground() == Color.WHITE) {
inputField.setBackground(Color.RED);
} else {
inputField.setBackground(Color.WHITE);
}
resultMessage.setText("Colour Button Pressed");
}
您可能还希望使用类似的方法来研究在GUI线程中执行实际的颜色更改。有更多关于这个的信息