1. 程式人生 > 遊戲 >街機空戰遊戲《飛天無限》7月29日推出 登陸各大平臺

街機空戰遊戲《飛天無限》7月29日推出 登陸各大平臺

GUI程式設計

元件

  • 視窗
  • 彈窗
  • 面板
  • 文字框
  • 列表框
  • 按鈕
  • 圖片
  • 監聽事件
  • 滑鼠事件
  • 鍵盤事件

1.簡介

GUI的核心技術:Swing AWT

GUI不流行的原因

  1. 因為介面不美觀
  2. 需要jre環境

為什麼要學GUI?

  1. 可以寫出自己心中想要的一些小工具
  2. 工作時候,也可能需要維護到swing介面,概率極小
  3. 瞭解MVC架構,瞭解監聽

2.AWT

2.1AWT介紹

  1. 包含了很多的類和介面
  2. 有許多元素:視窗、按鈕、文字框
  3. java.awt包

元件:Component

  • button

  • TextArea

  • Label

  • 容器Container

    • 視窗 Window

      • Frame
      • Dialog
    • 面板 Panel

      • Applet

2.2元件和容器

  1. Frame
package com.lvcong.lesson01;

import java.awt.*;

//GUI的第一個介面
public class TestFrame {
    public static void main(String[] args) {
        //Frame 看原始碼
        Frame frame = new Frame("我的第一個Java影象介面視窗");
        //要想視窗顯示出來,需要設定可見性w,h
        frame.setVisible(true);
        //設定視窗大小
        frame.setSize(400, 400);
        //設定背景顏色color
        frame.setBackground(new Color(1, 150, 255));
        //設定視窗彈出的初始位置
        frame.setLocation(822, 157);
        //設定視窗大小固定
        frame.setResizable(false);
    }

}

問題:發現視窗關閉不掉,停止Java程式!

嘗試回顧封裝:

package com.lvcong.lesson01;

import java.awt.*;

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

class MyFrame extends Frame {
    static int id = 0;//可能存在多個視窗,我們需要一個計數器

    public MyFrame(int x, int y, int w, int h, Color color) {
        super("MyFrame" + (++id));
        setBackground(color);
        setBounds(x, y, w, h);
        setVisible(true);
    }
}
  1. Panel面板

解決了關閉事件

package com.lvcong.lesson01;

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

//panel可以看成是一個空間,但是不能單獨存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //設定佈局
        frame.setLayout(null);
        //座標
        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(new Color(5, 7, 12));
        //panel設定座標,相對於frame
        panel.setBounds(51, 51, 157, 157);
        panel.setBackground(new Color(157, 199, 197));
        //將面板新增到視窗
        frame.add(panel);
        frame.setVisible(true);
        //監聽事件,監聽視窗關閉事件 system.exit(0)
        //介面卡模式:
        frame.addWindowListener(new WindowAdapter() {
            //視窗點選關閉的時候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //結束程式
                System.exit(0);
            }
        });
    }
}

3.佈局管理器

  • 流式佈局
package com.lvcong.lesson01;

import java.awt.*;

public class TestFlowLayout {
    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());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(200, 200);
        //把按鈕新增到視窗
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);

    }
}
  • 東南西北中
package com.lvcong.lesson01;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");
        Button east = new Button("east");
        Button west = new Button("west");
        Button south = new Button("south");
        Button north = new Button("north");
        Button center = new Button("center");

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

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

    }
}
  • 表格佈局
package com.lvcong.lesson01;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        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.pack();
        frame.setVisible(true);


    }
}

練習

package com.lvcong.lesson01;

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

public class Demo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(520, 520);
        frame.setLocation(300, 300);
        frame.setBackground(Color.red);
        frame.setLayout(new GridLayout(2, 1));
        //4個面板
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2, 1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2, 2));

        panel1.add(new Button("East-1"), BorderLayout.EAST);
        panel1.add(new Button("West-1"), BorderLayout.WEST);
        panel2.add(new Button("panel2-btn-1"));
        panel2.add(new Button("panel2-btn-2"));
        panel1.add(panel2, BorderLayout.CENTER);

        panel3.add(new Button("East-2"), BorderLayout.EAST);
        panel3.add(new Button("West-2"), BorderLayout.WEST);
//        panel4.add(new Button("panel4-btn-1"));
//        panel4.add(new Button("panel4-btn-2"));
//        panel4.add(new Button("panel4-btn-3"));
//        panel4.add(new Button("panel4-btn-4"));
        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("four" + i));
        }
        panel3.add(panel4, BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel3);

        frame.setVisible(true);
    }
}

總結

  1. Frame是一個頂級視窗
  2. Panel無法單獨顯示,必須新增到某個容器中
  3. 佈局管理器
    1. 流式
    2. 東西南北中
    3. 表格
  4. 大小、定位、背景顏色、可見性、監聽

4.事件監聽

package com.lvcong.lesson01;

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

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按鈕,觸發一些事件
        Frame frame = new Frame();
        Button button = new Button();
        //因為addActionListener()需要一個ActionListener,所以我們需要構造一個ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);//關閉視窗
        frame.setVisible(true);
    }

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

//事件監聽
class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("我愛鄭爽");
    }
}

多個按鈕共享一個事件

package com.lvcong.lesson01;

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

public class TestActionTwo {
    public static void main(String[] args) {
        //兩個按鈕,實現同一個監聽
        //開始 停止
        Frame frame = new Frame("開始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //可以顯示的定義觸發會返回的命令,如果不顯示定義,則會走預設的值!
        //可以多個按鈕只寫一個監聽類
        button2.setActionCommand("button2-stop");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1, BorderLayout.NORTH);
        frame.add(button2, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);

    }
}

class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按鈕被點選了:msg-->" + e.getActionCommand());

    }
}

輸入框監聽事件

package com.lvcong.lesson01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.FeatureDescriptor;

public class TestText01 {
    public static void main(String[] args) {
        //啟動!
        new MyFrame02();
    }
}

class MyFrame02 extends Frame {
    public MyFrame02() {
        TextField textField = new TextField();
        //因為是繼承了Frame
        add(textField);
        //監聽這個文字框輸入的文字
        MyActionListener02 myActionListener02 = new MyActionListener02();
        //按下enter,觸發事件
        textField.addActionListener(myActionListener02);
        //設定替換編碼
        textField.setEchoChar('*');
        setVisible(true);
        pack();

    }
}

class MyActionListener02 implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//獲得一些資源,返回一個Object
        System.out.println(textField.getText());//獲得輸入框的文字
        textField.setText("");//null
    }
}

簡易計算器,組合+內部類回顧複習

oop原則:組合大於繼承

class A extends B{
    
}
class A{
    public B b;
}

普通寫法-面向過程

package com.lvcong.lesson01;

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

//簡易計算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}

//計算器類
class Calculator extends Frame {
    public Calculator() {
        //3個文字框
        TextField num1 = new TextField(10);//字元數
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        //1個按鈕
        Button button = new Button("=");
        //button新增監聽事件
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));
        //1個標籤
        Label label = new Label("+");
        //佈局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }
}

//監聽器類
class MyCalculatorListener implements ActionListener {
    //獲取三個變數
    private TextField num1, num2, num3;

    public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //獲得加數和被加數
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        //將這個值+法運算後,放到第三個框
        num3.setText("" + (n1 + n2));
        //清除前兩個框
        num1.setText("");
        num2.setText("");
    }
}

完全改造為面向物件寫法

package com.lvcong.lesson01;

import com.sun.prism.shader.FillCircle_RadialGradient_PAD_AlphaTest_Loader;

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

//簡易計算器
public class TestCalc1 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}

//計算器類
class Calculator1 extends Frame {
    //屬性
    TextField num1, num2, num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener1(this));
        //佈局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }
}

//監聽器類
class MyCalculatorListener1 implements ActionListener {
    //獲取計算器這個物件,在一個類中組合另外一個類
    Calculator1 calculator1 = null;

    public MyCalculatorListener1(Calculator1 calculator1) {
        this.calculator1 = calculator1;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//    1.獲得加數和被加數
//    2.將這個值加法運算後,放到第三個框
//    3.清除前兩個框
        int n1 = Integer.parseInt(calculator1.num1.getText());
        int n2 = Integer.parseInt(calculator1.num2.getText());
        calculator1.num3.setText("" + (n1 + n2));
        calculator1.num1.setText("");
        calculator1.num2.setText("");
    }
}

內部類

  • 更好的包裝
package com.lvcong.lesson01;

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

public class TestCalc2 {
    public static void main(String[] args) {
        new Calculator2().loadFrame();
    }
}

//計算器類
class Calculator2 extends Frame {
    //屬性
    TextField num1, num2, num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener2());
        //佈局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }

    //監聽器類
    //內部類最大的好處,就是可以暢通無阻的訪問外部的屬性和方法
    private class MyCalculatorListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
//    1.獲得加數和被加數
//    2.將這個值加法運算後,放到第三個框
//    3.清除前兩個框
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText("" + (n1 + n2));
            num1.setText("");
            num2.setText("");
        }
    }
}

畫筆paint

package com.lvcong.lesson02;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame {
    public void loadFrame() {
        setBounds(200, 200, 800, 800);
        setVisible(true);
    }
    //畫筆

    @Override
    public void paint(Graphics g) {
        //畫筆需要顏色,可以用來畫畫
        g.setColor(Color.BLUE);
        g.drawOval(100,100,200,200);
        //養成習慣,畫筆用完,將它還原到最初的顏色
    }
}

滑鼠監聽

目的:想要實現滑鼠畫畫

package com.lvcong.lesson02;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//滑鼠監聽事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("畫圖");
    }
}

//自己的類
class MyFrame extends Frame {
    //畫畫需要畫筆,需要監聽滑鼠當前的位置,需要集合來儲存這些點
    ArrayList points;

    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 500, 500);
        //存滑鼠點選的點
        points = new ArrayList<>();
        //滑鼠監聽器,針對這個視窗
        this.addMouseListener(new MyMouseListener());
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //畫畫需要監聽滑鼠的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }

    //新增點到介面
    public void addPaint(Point point) {
        points.add(point);
    }

    //介面卡模式
    private class MyMouseListener extends MouseAdapter {
        //只需要滑鼠點選的事件
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //這個我們點選的時候,就會在介面上產生一個點
            //這個點就是滑鼠的點
            myFrame.addPaint(new Point(e.getX(), e.getY()));
            //每次點選滑鼠都需要重新畫一遍
            myFrame.repaint();//重新整理

        }
    }
}

視窗監聽

package com.lvcong.lesson02;

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

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.BLUE);
        setBounds(100,100,200,200);
        setVisible(true);
//        addWindowListener(new MyWindowListener());
        //匿名內部類
        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame)e.getSource();
                        source.setTitle("被激活了");
                    }
                }
        );
    }

//    class MyWindowListener extends WindowAdapter {
//        @Override
//        public void windowClosing(WindowEvent e) {
////            setVisible(false);//隱藏視窗
//            System.exit(0);//正常退出
//        }
//    }
}

鍵盤監聽

package com.lvcong.lesson02;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBackground(Color.BLUE);
        setBounds(300,300,500,500);
        setVisible(true);
        this.addKeyListener(
                new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        //獲得鍵盤下的鍵是哪一個
                        int keyCode = e.getKeyCode();//不需要去記錄這個數字,直接使用靜態屬性VK_XXX
                        if(keyCode == KeyEvent.VK_UP){
                            System.out.println("你按下了上鍵");

                        }
                    }
                }
        );
    }
}

Swing

package com.lvcong.lesson03;

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

public class JFrameDemo {
    //init();初始化
    public void init(){
        JFrame jFrame = new JFrame("這是一個JFrame視窗");
        jFrame.setVisible(true);
        jFrame.setBounds(300,300,500,500);
        jFrame.setBackground(Color.BLUE);
        //設定文字Jlabel
        JLabel jLabel = new JLabel("我愛鄭爽");
        jFrame.add(jLabel);
        //關閉事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一個視窗
        new JFrameDemo().init();
    }
}

標籤居中

package com.lvcong.lesson03;

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

public class JFramDemo02 {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
}
class MyJFrame2 extends JFrame{
    public void init(){
        this.setBounds(100,100,500,500);
        this.setVisible(true);
        JLabel jLabel = new JLabel("645213");
        this.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //獲得一個容器
        Container container = this.getContentPane();
        container.setBackground(Color.BLUE);
        //關閉事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }
}

彈窗

package com.lvcong.lesson03;

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

//主視窗
public class DialogDemo extends JFrame {
    public DialogDemo()  {
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame放東西需要容器
        Container container = this.getContentPane();
        //絕對佈局
        container.setLayout(null);
        //按鈕
        JButton jButton = new JButton("點選彈出一個對話方塊");
        jButton.setBounds(30,30,200,50);
        container.add(jButton);
        //點選這個按鈕的時候,彈出一個彈窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {//監聽器
                //彈窗
                new MyDialogDemo();
            }
        });
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
//彈窗的視窗
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,200,330,440);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("4444444465464656646"));
    }
}

標籤

label

new JLabel("xxx");

圖示

package com.lvcong.lesson03;

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

//圖示,需要實現類,Frame繼承
public class IconDemo extends JFrame implements Icon {
    public static void main(String[] args) {
        new IconDemo().init();
    }

    private int width;
    private int height;

    public IconDemo() {
    }//無參構造

    public IconDemo(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init() {
        IconDemo iconDemo = new IconDemo(100, 100);
        //圖示放在標籤,也可以放在按鈕上
        JLabel jLabel = new JLabel("SHENYANG", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @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 com.lvcong.lesson03;

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

public class IconImageDemo extends JFrame {
    public IconImageDemo() {
        //獲取圖片的地址
        JLabel jLabel = new JLabel("zs");
        URL url = IconImageDemo.class.getResource("zs.jpeg");
        ImageIcon imageIcon = new ImageIcon(url);//建立一個圖示
        jLabel.setIcon(imageIcon);//將圖示加到標籤上
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(200, 200, 520, 520);

    }

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

面板

JPanel

package com.lvcong.lesson03;

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

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));//後面引數的意思是間距
        JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
        JPanel jPanel2 = new JPanel(new GridLayout(2, 2));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        container.add(jPanel1);
        container.add(jPanel2);
        this.setVisible(true);
        this.setBounds(200, 200, 500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

JScrollPanel

package com.lvcong.lesson03;

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

public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo();
    }

    public JScrollDemo() {
        Container container = this.getContentPane();
        //文字域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("鄭爽");
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);

        this.setVisible(true);
        this.setBounds(200, 200, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

按鈕

  • 圖片按鈕
package com.lvcong.Lesson04;

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

public class JButtonDemo extends JFrame {
    public static void main(String[] args) {
        new JButtonDemo();
    }

    public JButtonDemo() {
        Container container = this.getContentPane();
        //將一個圖片變為圖示
        URL url = JButtonDemo.class.getResource("zs.jpeg");
        Icon icon = new ImageIcon(url);
        //把這個圖示放到按鈕上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("圖片按鈕");
        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }
}
  • 單選按鈕
package com.lvcong.Lesson04;

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

public class JButtonDemo02 extends JFrame {
    public static void main(String[] args) {
        new JButtonDemo02();
    }

    public JButtonDemo02() {
        Container container = this.getContentPane();
        //將一個圖片變為圖示
        URL url = JButtonDemo.class.getResource("zs.jpeg");
        Icon icon = new ImageIcon(url);
        //單選框
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");

        //由於單選框只能選一個,所以需要分組,一個組裡只能選一個
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1, BorderLayout.CENTER);
        container.add(radioButton2, BorderLayout.NORTH);
        container.add(radioButton3, BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 多選按鈕
package com.lvcong.Lesson04;

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

public class JButtonDemo03 extends JFrame {
    public static void main(String[] args) {
        new JButtonDemo03();
    }

    public JButtonDemo03() {
        Container container = this.getContentPane();
        //多選框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");

        container.add(checkBox1, BorderLayout.NORTH);
        container.add(checkBox2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

列表

  • 下拉框
package com.lvcong.Lesson04;

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

public class TestBoxDemo extends JFrame {


    public TestBoxDemo() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正版");
        status.addItem("微光");
        status.addItem("無所畏懼");
        container.add(status);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

    }
}
  • 列表框
package com.lvcong.Lesson04;

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

public class TextBoxDemo02 extends JFrame {


    public TextBoxDemo02() {
        Container container = this.getContentPane();

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

        //動態
        Vector vector = new Vector();
        JList jList1 = new JList(vector);
        vector.add("111");
        vector.add("222");
        vector.add("333");
        container.add(jList1);


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

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

    }
}

應用場景

  • 下拉框:選擇地區,或者一些單個選項
  • 列表,展示資訊,一般是動態擴容!

文字框

  • 文字框
package com.lvcong.Lesson04;

import sun.net.NetworkServer;

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

public class TestTextDemo extends JFrame {
    public static void main(String[] args) {
        new TestTextDemo();
    }

    public TestTextDemo() {
        Container container = this.getContentPane();
        //文字框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world", 20);
        container.add(textField1, BorderLayout.NORTH);
        container.add(textField2, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 密碼框
package com.lvcong.Lesson04;

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

public class TestTextDemo2 extends JFrame {
    public static void main(String[] args) {
        new TestTextDemo2();
    }

    public TestTextDemo2() {
        Container container = this.getContentPane();
        //密碼框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('·');
        container.add(passwordField);

        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 文字域
package com.lvcong.Lesson04;

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

public class TestTextDemo3 extends JFrame {
    public static void main(String[] args) {
        new TestTextDemo3();
    }

    public TestTextDemo3() {
        Container container = this.getContentPane();
        //文字域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("鄭爽");
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}