1. 程式人生 > 其它 >JavaGUI程式設計入門

JavaGUI程式設計入門

簡介


AWT

AWT介紹

AWT:抽象的視窗工具

GUI:圖形使用者程式設計


1.元件和容器

視窗(Windows)

Frame

package AWT_Study;

import java.awt.*;

public class Frame_Test {
    public static void main(String[] args) {
        //Frame,JDK,看原始碼
        Frame frame = new Frame("我的第一個Java圖形介面視窗");

        //設定視窗大小
        frame.setSize(400,400);

        //設定背景顏色
        frame.setBackground(new Color(1,1,1));

        //設定彈出的初始位置
        frame.setLocation(200,200);

        //設定視窗大小固定(不可拉伸)
        frame.setResizable(false);
        
        //需要設定可見性
        frame.setVisible(true);
    }
}


回顧封裝

package AWT_Study;

import java.awt.*;

public class Frame_Test02 {
    public static void main(String[] args) {
        //展示多個視窗
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.cyan);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.black);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.GREEN);
    }
}

class MyFrame extends Frame {
    //全域性變數,可能存在多個視窗,需要計數
    static int id = 0;

    //構造方法,引數:初始座標(x,y),長寬,顏色
    public MyFrame(int x, int y, int w, int h, Color color) {
        //呼叫父類構造方法,Frame(String);
        //MyFrame1,MyFrame2...
        super("MyFrame" + (++id));

        //由於繼承了Frame 可直接呼叫Frame的共有方法
        setBounds(x, y, w, h);
        setBackground(color);
        setVisible(true);
    }
}


面板(Panel)

Panel放在Frame裡面

package AWT_Study;

import java.awt.*;

public class Panel_Test01 {
    public static void main(String[] args) {

        Frame frame = new Frame();

        Panel panel = new Panel();

        //設定佈局(預設會把frame置頂)
        frame.setLayout(null);

        //設定frame
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(204, 255, 124));

        //設定panel
        panel.setBounds(50,50,400,400);//設定座標(相對於frame)
        panel.setBackground(new Color(238, 248, 211, 176));

        //給frame新增panel
        //add(Component),Panel繼承自Container繼承自Component
        frame.add(panel);

        frame.setVisible(true);
    }
}


佈局管理器

  • 流式佈局-FlowLayout
package AWT_Study;

import java.awt.*;

public class FlowLayout_Test {

    public static void main(String[] args) {
        Frame frame = new Frame();

        //元件-按鈕
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //設定為流式佈局
        //frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setBounds(100,100,200,200);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }

}

以FlowLayout.RIGHT為例


  • 東西南北中-BorderLayout
package AWT_Study;

import java.awt.*;

public class BorderLayout_Test {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button north = new Button("North");
        Button south = new Button("South");
        Button west = new Button("West");
        Button east = new Button("East");
        Button center = new Button("Center");

        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(west,BorderLayout.WEST);
        frame.add(east,BorderLayout.EAST);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(100,100);
        frame.setVisible(true);

    }
}


  • 表格佈局-GridLayout
package AWT_Study;

import java.awt.*;

public class GridLayout_Test {
    public static void main(String[] args) {

        Frame frame = new Frame();

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.setSize(100,100);

        frame.setVisible(true);
    }
}


練習題

我的答案

        //frame
        Frame frame = new Frame();
        frame.setBounds(200,200,500,300);
        frame.setBackground(Color.black);
        //設定為Grid佈局 兩行一列
        frame.setLayout(new GridLayout(2,1));
        //填充frame
        Panel pUp = new Panel(new BorderLayout());
        pUp.setBackground(Color.blue);

        Panel pDown = new Panel(new BorderLayout());
        pDown.setBackground(Color.GRAY);

        frame.add(pUp);
        frame.add(pDown);
        //填充pUp
        Button pUp_Button_west = new Button("west_Up");
        Button pUp_Button_east = new Button("east_Up");

        Panel pUp_Panel = new Panel(new GridLayout(2,1));
        Button pUp_Panel_Button1 = new Button("pUp_Panel_Button1");
        Button pUp_Panel_Button2 = new Button("pUp_Panel_Button2");
        pUp_Panel.add(pUp_Panel_Button1);
        pUp_Panel.add(pUp_Panel_Button2);


        pUp.add(pUp_Button_west,BorderLayout.WEST);
        pUp.add(pUp_Button_east,BorderLayout.EAST);
        pUp.add(pUp_Panel,BorderLayout.CENTER);
        //填充pDown
        Button pDown_Button_west = new Button("west_Do");
        Button pDown_Button_east = new Button("east_Do");

        Panel pDown_Panel = new Panel(new GridLayout(2,2));
        Button pDown_Panel_Button1 = new Button("pDown_Panel_Button1");
        Button pDown_Panel_Button2 = new Button("pDown_Panel_Button2");
        Button pDown_Panel_Button3 = new Button("pDown_Panel_Button3");
        Button pDown_Panel_Button4 = new Button("pDown_Panel_Button4");
        pDown_Panel.add(pDown_Panel_Button1);
        pDown_Panel.add(pDown_Panel_Button2);
        pDown_Panel.add(pDown_Panel_Button3);
        pDown_Panel.add(pDown_Panel_Button4);

        pDown.add(pDown_Button_west,BorderLayout.WEST);
        pDown.add(pDown_Button_east,BorderLayout.EAST);
        pDown.add(pDown_Panel,BorderLayout.CENTER);

更好的寫法

        //frame(裝p1,p2)
        Frame frame = new Frame();
        frame.setBounds(200,200,500,300);
        frame.setBackground(Color.black);
        //設定為Grid佈局 兩行一列
        frame.setLayout(new GridLayout(2,1));

        //四個面板(p1裝p2 p3裝p4)
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        //上面
        p2.add(new Button("p2-Btn-1"));
        p2.add(new Button("p2-Btn-2"));

        p1.add(new Button("West-1"),BorderLayout.WEST);
        p1.add(new Button("Eest-1"),BorderLayout.EAST);
        p1.add(p2,BorderLayout.CENTER);

        //下面
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i));
        }

        p3.add(new Button("West-2"),BorderLayout.WEST);
        p3.add(new Button("Eest-2"),BorderLayout.EAST);
        p3.add(p4,BorderLayout.CENTER);


        frame.add(p1);
        frame.add(p3);

        frame.setVisible(true);

總結


Swing