我在使用JRadioButton 按钮中,使用了
setBackground(Color.ORANGE );
把按钮设置成了橘黄色
但是之后我什么改回来初始的颜色呢(就是没有改颜色之前的模样)?
------解决思路----------------------
做一个变量存储之前的背景色。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class MyFrame extends JFrame{
public void service(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
final JRadioButton button1 = new JRadioButton("A");
final Color colorBefore = button1.getBackground();
button1.setBackground(Color.ORANGE);
JButton button2 = new JButton("改变背景色");
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
button1.setBackground(colorBefore);
}
});
add(button1,BorderLayout.NORTH);
add(button2,BorderLayout.WEST);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.service();
}
}