第一篇 Swing 入門

1. Swing是一個爲Java設計的GUI工具包。java

知道是作UI界面的便可了。編程

2.Swing 編程基本流程設計模式

第一步: 取得主窗體ide

JFrame jf = new JFrame("Demo1");工具

第二步:得到主窗體的容器佈局

Container c = jf.getContentPane();學習

第三步:設置容器佈局this

c.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));spa

第四步:添加組件及設置組件屬性設計

JLabel label1 = new JLabel("Hello World!");
JLabel label2 = new JLabel("Bye World!");
label1.setBackground(Color.BLUE);
label1.setOpaque(true);

第五步:設置窗體屬性,關閉主窗體,退出程序

jf.setSize(200, 100); //設置主窗體大小
jf.setVisible(true);

jf.setResizable(false);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置窗體關閉時,退出程序

另:
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);也可以使用如下代碼代替

jf.addWindowListener(new WindowAdapter() {

@Override
     public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
});

 

3.我的的一點學習心得:

   *學習Swing,作出簡單的小軟件,並不困難,想要作的好看,就須要對佈局有較深的研究!

   *使用Swing的一個用處:Swing雖然已經有些過期了,但能夠作些小工具,輔助工做等,也能夠自娛自樂。我的對電腦圖形界面挺感興趣。

   *Swing組件使用一些設計模式,是挺值得研究一下,對編程挺要用處!

   *學習東西貴在堅持,不少工具包都是相似的,對一種有所精深,其餘亦能舉一反三!

4.附一個在Youtobe視頻中學習到的一個Swing程序:

package com.ting723.www;

 

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class Demo10XOGame extends JFrame{

 

         JPanel jp = new JPanel();

 

         public Demo10XOGame() {

                   Container c = this.getContentPane();

                   c.add(jp);

                   jp.setLayout(new GridLayout(3, 3));

                   for (int i = 0; i < 9; i++) {

                            XOButton jb = new XOButton();

                            jp.add(jb);

                   }

 

                   this.setSize(500, 500);

                   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                   this.setLocationRelativeTo(null);

                   this.setVisible(true);

         }

 

         public static void main(String[] args) {

 

                   new Demo10XOGame();

         }

 

 

}

 

class XOButton extends JButton implements ActionListener {

         private ImageIcon X, O;

         byte value = 0;

         public XOButton() {

                   X = new ImageIcon(this.getClass().getResource("x.png"));

                   O = new ImageIcon(this.getClass().getResource("o.png"));

                   this.addActionListener(this);

 

         }

 

         @Override

         public void actionPerformed(ActionEvent e) {

 

                   value++;

                   value %= 3;

                   switch (value) {

                   case 0:

                            setIcon(null);

                            break;

                   case 1:

                            setIcon(X);

                            break;

                   case 2:

                            setIcon(O);

                   }

         }

}

 

       

 


不管作什麼,堅持一下,總會出些成果!人生,高興就好,莫爲結果爲傷神!

相關文章
相關標籤/搜索