【Java GUI 程式設計】Swing 使用者介面開發工具包

錢不錢的無所謂,我主要是愛程式設計發表於2020-10-20

Swing

使用者介面開發工具包,比 AWT 更加高階一點,Swing 可以使用任何可插拔的外觀風格 ,用很少的程式碼就可以建立優雅的使用者介面,工具包中所有的包都是以swing作為名稱

視窗

package JavaGUI;

import javax.swing.*;
import java.awt.*;

/**
 * @Title: Test13JFrame
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 13:57
 */
public class Test13JFrame {
    public static void main(String[] args) {
        new MyJFrame().init();
    }
}

class MyJFrame extends JFrame{
    public void init(){
        // 獲得一個容器
        Container contentPane = this.getContentPane();
        contentPane.setBackground(Color.red);

        JLabel label = new JLabel("歡迎來到 Java !");
        this.add(label);

        // 水平居中標籤
        label.setHorizontalAlignment(SwingConstants.CENTER);

        this.setVisible(true);
        this.setBounds(1,1,200,200);
    }
}

彈窗

JDialog 用來被彈出視窗

package JavaGUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Title: Test15Dialog
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 17:31
 */
public class Test15Dialog extends JFrame{
    public Test15Dialog(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 放東西,容器
        Container container = this.getContentPane();
        container.setLayout(null); // 絕對佈局

        JButton button = new JButton("點選彈出一個對話方塊");
        button.setBounds(30,30,200,50);
        // 當點選這個按鈕的時候彈窗,監聽器
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });
        container.add(button);
    }

    public static void main(String[] args) {
        new Test15Dialog();
    }
}

// 彈窗的視窗
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        Container container = this.getContentPane();
        container.setLayout(null); //絕對定位
        container.add(new Label("學 Java GUI 程式設計"));
    }
}

在這裡插入圖片描述

標籤

label

畫了一個圓作為標籤

public class Test16IconDemo1 extends JFrame implements Icon {

    private int width;
    private int height;
    
    public Test16IconDemo1(){ }
    
    public Test16IconDemo1(int width,int height){
        this.width = width;
        this.height = height;
    }
    public void init(){
        Test16IconDemo1 iconDemo1 = new Test16IconDemo1(15,15);
        // 圖示放在標籤上,也可以放在按鈕上
        JLabel jLabel = new JLabel("icontest",iconDemo1,SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(jLabel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }


    public static void main(String[] args) {
        new Test16IconDemo1().init();
    }

    // 畫了一個圓作為標籤
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}

效果如下

在這裡插入圖片描述

自定以圖片作為標籤

package JavaGUI;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @Title:
 * @Package
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 18:10
 */
public class Test16IconDemo1 extends JFrame{
    public void init(){
        JLabel jLabel = new JLabel("icontest");
        // 獲取圖片地址 
        URL url = Test16IconDemo1.class.getResource("1.png");
        // 載入 url
        ImageIcon icon = new ImageIcon(url);
        
        // 設定標籤屬性
        jLabel.setIcon(icon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        // 把標籤新增到容器中
        Container container = getContentPane();
        container.add(jLabel);

        // 設定 JFrame 視窗 
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }

    public static void main(String[] args) {
        new Test16IconDemo1().init();
    }
}

如圖所示

在這裡插入圖片描述

皮膚

JPainel

public class Test17JPanelDemo1 extends JFrame {
    public static void main(String[] args) {
        new Test17JPanelDemo1();
    }
    public Test17JPanelDemo1() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));

        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(2,3));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        container.add(panel);
        container.add(panel2);
        container.add(panel3);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

滾動條

JScrollPanel

package JavaGUI;

import javax.swing.*;
import java.awt.*;

/**
 * @Title: Test18JScrollPanel
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 21:54
 */
public class Test18JScrollPanel extends JFrame {
    public Test18JScrollPanel() {
        // 容器
        Container container = this.getContentPane();
        //文字域
        TextArea area = new TextArea(20,100);
        area.setText("歡迎來到 Java 的世界,這裡是 GUI Swing 工具包的滾動條實現!\n" +
                " Java是目前最為廣泛的網路程式語言。它具有簡單,物件導向,穩定等特點。\n" +
                " 2.Java 語言簡單是指這門語言既易學好用。不要將簡單誤解為這門語言很乾癟。\n" +
                "如果你學習過 C++語言,你會感覺 Java很眼熟,因為 Java中許多基本語句的語法和 C++一樣\n" +
                "。如果從語言的簡單性方面看...nhhhhhhhhhhhhhhh");

        // 新增一個Scroll 皮膚
        JScrollPane scrollPane = new JScrollPane(area);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(100,100,300,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test18JScrollPanel();
    }
}

如下圖

在這裡插入圖片描述

按鈕

  • 圖片按鈕
package JavaGUI;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @Title: Test19JButton
 * @Package JavaGUI
 * @Description: 實現一個圖片按鈕
 * @author: maze
 * @date 2020/10/20下午 22:38
 */
public class Test19JButton extends JFrame {

    public Test19JButton() {
        Container container = this.getContentPane();
        // 將一個圖片變成圖示
        URL url = Test19JButton.class.getResource("1.png");
        ImageIcon imageIcon = new ImageIcon(url);

        // 把這個圖示放在按鈕上
        JButton button = new JButton();
        button.setIcon(imageIcon);
        button.setToolTipText("圖片按鈕");

        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test19JButton();
    }
}
  • 單選按鈕

將三個按鈕放在一個組裡,在組裡只能有一個被選中

package JavaGUI;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @Title: Test20Button
 * @Package
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 22:49
 */
public class Test20Button extends JFrame{

    public Test20Button() throws HeadlessException {
        Container container = this.getContentPane();
        // 將一個圖片變成圖示
        URL url = Test19JButton.class.getResource("1.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //單選框
        JRadioButton radioButton01 = new JRadioButton("男");
        JRadioButton radioButton02 = new JRadioButton("女");
        JRadioButton radioButton03 = new JRadioButton("未知");
        // 由於是單選框只能選擇一個,分組,一個組中只能選擇一個
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);

        container.add(radioButton01,BorderLayout.CENTER);
        container.add(radioButton02,BorderLayout.NORTH);
        container.add(radioButton03,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test20Button();
    }
}

如下圖

在這裡插入圖片描述

  • 核取按鈕

去掉分組框,就變成核取按鈕了

package JavaGUI;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @Title: Test20Button
 * @Package
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 22:49
 */
public class Test20Button extends JFrame{

    public Test20Button() throws HeadlessException {
        Container container = this.getContentPane();
        // 將一個圖片變成圖示
        URL url = Test19JButton.class.getResource("1.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //單選框
        JRadioButton radioButton01 = new JRadioButton("寫程式碼");
        JRadioButton radioButton02 = new JRadioButton("讀書");
        JRadioButton radioButton03 = new JRadioButton("陪女朋友逛街");
        JRadioButton radioButton04 = new JRadioButton("打遊戲");

        // 由於是單選框只能選擇一個,分組,一個組中只能選擇一個
        container.setLayout(new GridLayout(1,4));
        container.add(radioButton01);
        container.add(radioButton02);
        container.add(radioButton03);
        container.add(radioButton04);
        this.pack();
        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test20Button();
    }
}

在這裡插入圖片描述

列表

package JavaGUI;

import javax.swing.*;
import java.awt.*;

/**
 * @Title: Test21Combobox
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 23:29
 */
public class Test21Combobox extends JFrame {
    public Test21Combobox() {
        Container container = this.getContentPane();

        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("正在上映");
        comboBox.addItem("即將上映");
        comboBox.addItem("已下架");
        container.add(comboBox);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test21Combobox();
    }
}

在這裡插入圖片描述

列表框

public class Test21Combobox extends JFrame {
    public Test21Combobox() {
        Container container = this.getContentPane();

        // 生成列表內容
        String[] contents = {"1","2","3"};
        // 列表中需要放入的內容
        JList jList = new JList(contents);
        container.add(jList);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test21Combobox();
    }
}

文字框

  • 文字框
public class Test21Combobox extends JFrame {
    public Test21Combobox() {
        Container container = this.getContentPane();

        JTextField field1 = new JTextField("hello");
        JTextField field2 = new JTextField("hello",20);
        container.add(field1,BorderLayout.NORTH);
        container.add(field2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test21Combobox();
    }
}
  • 密碼框
public class Test21Combobox extends JFrame {
    public Test21Combobox() {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        container.add(passwordField);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test21Combobox();
    }
}
  • 文字域
public class Test21Combobox extends JFrame {
    public Test21Combobox() {
        Container container = this.getContentPane();

        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("歡迎來到 Java GUI 系列的學習");

        //皮膚
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Test21Combobox();
    }
}

到這裡 GUI 的重點知識就差不多了

寫一篇部落格是,利用 GUI 程式設計實現一個貪吃蛇遊戲

相關文章