
java改变按钮颜色
比如说setBackground(backgroundColor);是设置背景颜色,我想让它再加一个功能改变背景颜色的同时也将按钮颜色改变,怎么办...
比如说setBackground(backgroundColor);是设置背景颜色,我想让它再加一个功能改变背景颜色的同时也将按钮颜色改变,怎么办
展开
2个回答
展开全部
为yellow、blue、red3个按钮添加actionlistener,当按钮点击时执行setBackground(backgroundColor),同时执行 按钮.setBackground(backgroundColor)即可,比如:
JButton btnYellow = null;
JButton btnBlue = null;
JButton btnRed = null;
btnYellow.setActionCommand("yellow");
btnBlue.setActionCommand("blue");
btnRed.setActionCommand("red");
public void actionPerformed(ActionEvent e) {
String command = event.getActionCommand();
if( "yellow".equals(command) ){
setBackground(Color.yellow);
btnYellow.setBackground(Color.yellow);
}else if( "blue".equals(command) ){
setBackground(Color.blue);
btnBlue.setBackground(Color.blue);
}else if( "red".equals(command) ){
setBackground(Color.red);
btnRed.setBackground(Color.red);
}
}
写出了部分代码
JButton btnYellow = null;
JButton btnBlue = null;
JButton btnRed = null;
btnYellow.setActionCommand("yellow");
btnBlue.setActionCommand("blue");
btnRed.setActionCommand("red");
public void actionPerformed(ActionEvent e) {
String command = event.getActionCommand();
if( "yellow".equals(command) ){
setBackground(Color.yellow);
btnYellow.setBackground(Color.yellow);
}else if( "blue".equals(command) ){
setBackground(Color.blue);
btnBlue.setBackground(Color.blue);
}else if( "red".equals(command) ){
setBackground(Color.red);
btnRed.setBackground(Color.red);
}
}
写出了部分代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |