如何创建一个AWT面板,该面板包含三个不同颜色按钮,单击每个按钮是窗口背景色显示为相应颜色? 30
importjava.awt.*;importjava.awt.event.*;publicclassJawtimplementsActionListener{Frame...
import java.awt.*;
import java.awt.event.*;
public class Jawt implements ActionListener {
Frame frm;
Button b1;
Button b2;
Button b3;
Panel p;
public static void main(String args[]){
new Jawt().init();
}
public void init(){
Frame frm=new Frame("click to show the color");
frm.setLayout(new FlowLayout());
Button b1=new Button("red");
Button b2=new Button("blue");
Button b3=new Button("yellow");
Panel p=new Panel();
frm.add(b1);
frm.add(b2);
frm.add(b3);
frm.add(p);
frm.setSize(400,300);
frm.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1) {p.setBackground(Color.red);}
if(e.getSource() == b2) {p.setBackground(Color.blue);}
if(e.getSource() == b3) {p.setBackground(Color.yellow);}
}
}
这个中有什么问题? 展开
import java.awt.event.*;
public class Jawt implements ActionListener {
Frame frm;
Button b1;
Button b2;
Button b3;
Panel p;
public static void main(String args[]){
new Jawt().init();
}
public void init(){
Frame frm=new Frame("click to show the color");
frm.setLayout(new FlowLayout());
Button b1=new Button("red");
Button b2=new Button("blue");
Button b3=new Button("yellow");
Panel p=new Panel();
frm.add(b1);
frm.add(b2);
frm.add(b3);
frm.add(p);
frm.setSize(400,300);
frm.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1) {p.setBackground(Color.red);}
if(e.getSource() == b2) {p.setBackground(Color.blue);}
if(e.getSource() == b3) {p.setBackground(Color.yellow);}
}
}
这个中有什么问题? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询