GUI(事件监听机制的演示) java图形化界面初学

作者: Rememberautumn 分类: Java 发布时间: 2014-09-27 19:44 阅读: 2,254
  1. package cn.itcast.gui.pl.awt;
  2. import java.awt.Button;
  3. import java.awt.FlowLayout;
  4. import java.awt.Frame;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10. public class AwtDemo {
  11.     public static void main(String[] args){
  12.         //新建一个窗体
  13.         Frame f=new Frame(“My Frame”);
  14.         //设置窗体的大小
  15.     //  f.setSize(600,500);
  16.         //设置窗体的位置
  17.         //f.setLocation(350, 150);
  18.         f.setBounds(350150600,500);
  19.         //新建一个按钮
  20.         Button but=new Button(“你猜”);
  21.         //将按钮加入到窗体中
  22.         f.add(but);
  23. //      设置按钮的排列方式
  24.         f.setLayout(new FlowLayout());
  25. //      创建一个窗体的监听器
  26.         f.addWindowListener(new WindowAdapter() {
  27. //           关闭窗口的事件
  28.             @Override
  29.             public void windowClosing(WindowEvent e) {
  30.                 // TODO Auto-generated method stub
  31.                 System.exit(0);
  32.             }
  33.         });
  34. //    点击按钮关闭窗体
  35.         but.addActionListener(new ActionListener() {
  36.             @Override
  37.             public void actionPerformed(ActionEvent arg0) {
  38.                 // TODO Auto-generated method stub
  39.                 System.exit(0);
  40.             }
  41.         });
  42. //      将窗体显示
  43.         f.setVisible(true);
  44.     }
  45. }
  46. /*
  47. class MyWindowListenerImpl implements WindowListener{
  48.     @Override
  49.     public void windowActivated(WindowEvent e) {
  50.         // TODO Auto-generated method stub
  51.         
  52.     }
  53.     @Override
  54.     public void windowClosed(WindowEvent e) {
  55.         // TODO Auto-generated method stub
  56.         
  57.     }
  58.     @Override
  59.     public void windowClosing(WindowEvent e) {
  60.         // TODO Auto-generated method stub
  61.         //System.out.println(“vsjdkjvhlskdhvlks”);
  62.         System.exit(0);
  63.         
  64.     }
  65.     @Override
  66.     public void windowDeactivated(WindowEvent e) {
  67.         // TODO Auto-generated method stub
  68.         
  69.     }
  70.     @Override
  71.     public void windowDeiconified(WindowEvent e) {
  72.         // TODO Auto-generated method stub
  73.         
  74.     }
  75.     @Override
  76.     public void windowIconified(WindowEvent e) {
  77.         // TODO Auto-generated method stub
  78.         
  79.     }
  80.     @Override
  81.     public void windowOpened(WindowEvent e) {
  82.         // TODO Auto-generated method stub
  83.         //System.out.println(“vsjdkjvhlskdhvlks”);
  84.         
  85.     }
  86. }
  87. */

效果如下:

Swing

  1. package cn.itcast.gui.swing;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. public class SwingDemo {
  8.     public static void main(String[] args){
  9.          JFrame frame=new JFrame();
  10.          frame.setBounds(350,150,600,500);
  11.          frame.setLayout(new FlowLayout());
  12.          JButton but=new JButton(“anjiu”);
  13.          frame.add(but);
  14. //        点击按钮关闭窗体
  15.             but.addActionListener(new ActionListener() {
  16.                 @Override
  17.                 public void actionPerformed(ActionEvent arg0) {
  18.                     // TODO Auto-generated method stub
  19.                     System.exit(0);
  20.                 }
  21.             });
  22.          frame.setVisible(true);
  23.     }
  24. }

效果如下:

ORCK(2FZH]TWPAA`UM(_[PP

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注