1. 程式人生 > 實用技巧 >Java:GUI-AWT(上)

Java:GUI-AWT(上)

目錄

學習資料

b站狂神說:https://www.bilibili.com/video/BV1DJ411B75F

第一個視窗Farme類

package com.zy7y.gui;

import java.awt.*;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午5:35
 * @Description: 建立第一個視窗
 */
public class TestFrame {

    public static void main(String[] args) {
        // 例項化視窗物件
        Frame frame = new Frame("第一個Java影象介面視窗");
        // 設定視窗可見性
        frame.setVisible(true);
        // 設定視窗大小
        frame.setSize(200,300);
        // 設定背景色
        frame.setBackground(new Color(0x9797DC));
        // 初始位置
        frame.setLocation(200,200);
        // 設定視窗大小固定:
        frame.setResizable(false);
    }
}

封裝建立視窗的方法

package com.zy7y.gui;

import java.awt.*;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午5:44
 * @Description:
 */
public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame(100,200,100,100,Color.CYAN);
        MyFrame myFrame1 = new MyFrame(300,200,200,200,Color.cyan);
        MyFrame myFrame2 = new MyFrame(200,200,300,300,Color.yellow);
        MyFrame myFrame3 = new MyFrame(400,200,400,400,Color.BLACK);
    }

}

class MyFrame extends Frame{
    static int id = 0;
    public MyFrame(int x, int y, int w, int h, Color color){
        // 呼叫父類構造方法初始化一個Frame物件
        super("第" + ++id + "個視窗");
        // 視窗標題
        // 視窗尺寸
        setBounds(x,y,w,h);
        // 視窗調節
        setResizable(false);
        // 視窗可見性
        setVisible(true);
        // 視窗彈出位置
        setLocation(200,200);
        // 視窗顏色
        setBackground(color);
    }
}

面板Plan類

面板可以巢狀

package com.zy7y.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午5:54
 * @Description: 面板 Plan類
 */
public class TestPlan {
    public static void main(String[] args) {
        // 建立視窗
        Frame frame = new Frame();
        // 建立面板
        Panel panel = new Panel();
        // 設定佈局
        frame.setLayout(null);

        // 座標
        frame.setBounds(300,300,300,300);
        frame.setBackground(Color.cyan);

        // 設定面板
        panel.setBackground(Color.red);
        panel.setBounds(100,100,100,100);

        // 將面板加入到frame視窗中
        frame.add(panel);
        frame.setVisible(true);

        // 監聽,介面卡模式;
        frame.addWindowListener(new WindowAdapter() {
            // 視窗點選關閉的時候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
//                super.windowClosing(e);
                // 強制退出程式
                System.exit(0);
            }
        });
    }
}

佈局管理器

流式佈局

package com.zy7y.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午6:08
 * @Description:
 */
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("流式佈局");

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

        // 設定為流式佈局,並靠視窗Frame左側
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setBounds(200,200,100,100);
        // 將元件加入視窗中
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);

        // 監聽,關閉視窗
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

東南西北中

package com.zy7y.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午6:16
 * @Description: 東西南北中
 */
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("東西南北中");
        Button button1 = new Button("東");
        Button button2 = new Button("西");
        Button button3 = new Button("南");
        Button button4 = new Button("北");
        Button button5 = new Button("中");
				
      	// 新增到東,西,南,北,中的位置
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);

        frame.setBounds(500,500,900,600);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

表格佈局

package com.zy7y.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午6:28
 * @Description: 網格佈局
 */
public class TestGridLayout {
    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");

        // 設定成3行2列的表格佈局
        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.setBounds(500,500,900,600);

        frame.setVisible(true);


        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

佈局作業

package com.zy7y.gui;

import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午6:36
 * @Description:  佈局作業, 面板可以巢狀
 */
public class TestLayoutTopic {
    public static void main(String[] args) {
        Frame frame = new Frame("佈局作業");
        frame.setBounds(500,500,900,600);


        Panel panel1 = new Panel(new BorderLayout());
        panel1.setBackground(Color.black);
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        panel3.setBackground(Color.red);
        Panel panel4 = new Panel(new GridLayout(2,2));

        // 內上
        panel1.add(new Button("第一行-東"), BorderLayout.EAST);
        panel1.add(new Button("第一行-西"), BorderLayout.WEST);
        panel2.add(new Button("第一行-第一行"));
        panel2.add(new Button("第一行-第二行"));
      	// 將面板panel2 新增到 面板panle1中間的位置
        panel1.add(panel2,BorderLayout.CENTER);

        // 內下
        panel3.add(new Button("第二行-東"), BorderLayout.EAST);
        panel3.add(new Button("第二行-西"), BorderLayout.WEST);
        panel4.add(new Button("第二行-中-左上"));
        panel4.add(new Button("第二行-中-右上"));
        panel4.add(new Button("第二行-中-左下"));
        panel4.add(new Button("第二行-中-右下"));
        panel3.add(panel4, BorderLayout.CENTER);

        
        frame.add(panel1);
        frame.add(panel3);
        frame.setLayout(new GridLayout(2,1));
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

事件監聽

package com.zy7y.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午7:27
 * @Description: 事件監聽
 */
public class TestActionEvent {
    public static void main(String[] args) {

        Frame frame = new Frame();
        Button button = new Button();

        // 按鈕事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按鈕被按下");
            }
        });
        frame.add(button);
        frame.pack();

        frame.setVisible(true);

        // 視窗關閉事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}
package com.zy7y.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 下午8:10
 * @Description: 兩個按鈕實現同一個監聽
 */
public class TestActionEvent2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button start = new Button("start");
        Button stop = new Button("stop");

        Button control = new Button("開始");
        control.setBackground(Color.green);

        MyMonitor myMonitor = new MyMonitor();
        start.addActionListener(myMonitor);
        stop.addActionListener(myMonitor);
        control.addActionListener(myMonitor);
        frame.add(start);
        frame.add(stop);
        frame.add(control);
        frame.setLayout(new GridBagLayout());
        frame.pack();
        closeWindows(frame);
        frame.setVisible(true);
    }

    // 關閉視窗
    public static void closeWindows(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


class MyMonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // e.getActionCommand() 獲取按鈕資訊
        System.out.println("按鈕被點選:" + e.getActionCommand());
        
        // 獲得資源物件,強轉成button,實現一個按鈕切換的操作
        Button button = (Button)e.getSource();

        if (button.getLabel().equals("開始")) {
            System.out.println("你點下了開始按鈕");
            button.setLabel("結束");
            button.setBackground(Color.red);
        }else if (button.getLabel().equals("結束")) {
            System.out.println("你點下了結束按鈕");
            button.setLabel("開始");
            button.setBackground(Color.green);
        }
    }
}