import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Exam extends JFrame implements ActionListener{
JButton[] b = {new JButton("红色"),new JButton("绿色"),new JButton("蓝色")};
Container con ;
public static void main(String[] args){
Exam frm = new Exam("按钮事件");
frm.setBounds(100,100,300,200);
frm.setDefaultCloseOpetion(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
Exam(String s){
setTitle(s);
con = getContentPane();
add(b[0]); add(b[1]); add(b[2]);
b[0].addActionListener(this);
b[1].addActionListener(this);
b[2].addActionListener(this);
}
public void performed(ActionEvent e){
if(e.getSource()==b[0])
con.setBackground(Color.red);
else if(e.getSource==b[1])
con.setBackground(Color.green);
else
con.setBackground(Color.blue);
}
}
效果如下
修改的错误
import java.awt.Color;我刚才改了部分,但是还是运行错误
嗯嗯,我现在改正了,知道了,谢谢
你需要全部修改啊 . 照上面的代码, 全部修改
我刚才改了部分,但是还是运行错误