做一个变量存储之前的背景色。
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();
}
}
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();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货