packagecom.homework;
import java.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.WindowConstants;
public class TextFrame {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String title = "Hello";
JFrame jf=new JFrame(title);
Container container=jf.getContentPane();
container.setBackground(Color.yellow);
jf.setVisible(true);
jf.setSize(600, 400);
jf.setLayout(new FlowLayout(1,10,10));
/*
*set Button
*/
JButton jb_1= new JButton("蓝色");
JButton jb_2= new JButton("黄色");
JButton jb_3= new JButton("退出");
jf.add(jb_1);
jf.add(jb_2);
jf.add(jb_3);
/*
*Button 事件
*/
jb_1.addActionListener(newActionListener() {
public void actionPerformed(ActionEvent e) {
container.setBackground(Color.blue);
}
});
jb_2.addActionListener(newActionListener() {
public void actionPerformed(ActionEvent e) {
container.setBackground(Color.yellow);
}
});
jb_3.addActionListener(newActionListener() {
public void actionPerformed(ActionEvente) {
System.exit(0);
}
});
/*
* 设置窗体关闭方式
*/
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}