java Swing程式設計入門

haojun1996發表於2018-03-22

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);

    }

 

}

 


相關文章