实训作业——窗口

package bbb;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class ccc extends JFrame implements ActionListener {
JFrame f;
JPanel p;
JButton b1,b2,b3;
public static void main(String[] args) {
ccc tb = new ccc();
}

public ccc(){
p = new JPanel();
b1 = new JButton("Red");
b2 = new JButton("Green");
b3 = new JButton("Blue");
this.add(p);
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(this);
b1.setActionCommand("Red");
b2.addActionListener(this);
b2.setActionCommand("Green");
b3.addActionListener(this);
b3.setActionCommand("Blue");
this.setTitle("点击按钮");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Red")){

p.setBackground(Color.RED);
}else if(e.getActionCommand().equals("Green")){

p.setBackground(Color.GREEN);
}else if(e.getActionCommand().equals("Blue")){

p.setBackground(Color.BLUE);
}
}

}

猜你喜欢

转载自www.cnblogs.com/cafun/p/11010229.html
今日推荐