天天看點

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.Swing

3.1、視窗&面闆

代碼:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 08:31
 * @desc Swing
 **/
public class JFrameDemo1 {
    public static void main(String[] args) {
        new JFrameDemo1().init();
    }
//    init()初始化。
    public void init(){
        JFrame jFrame = new JFrame("這是我們的JFrame視窗。");
        jFrame.setVisible(true);
        jFrame.setBackground(Color.BLUE);
        jFrame.setBounds(377, 377, 277, 277);

//        設定文字。 JLabel
        JLabel jLabel = new JLabel("Welcome to EdwinD's Way.");
        jFrame.add(jLabel);

//        關閉事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

框框2:标簽居中。

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 09:05
 * @desc Swing
 **/
public class JFrameDemo2 {
    public static void main(String[] args) {
        MyJFrame2 myJFrame2 = new MyJFrame2();
        myJFrame2.setTitle("這是我們的JFrame視窗2.0");
        myJFrame2.init();
    }
}
class MyJFrame2 extends JFrame {
    public void init(){
        setBounds(377, 377, 277, 277);
        setVisible(true);
        JLabel jLabel = new JLabel("Welcome to EdwinD's Way-2.0.");
        this.add(jLabel);
//        label居中,設定水準對齊
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//        獲得一個容器,
        Container contentPane = this.getContentPane();
        contentPane.setBackground(new Color(77,77,77));
//        關閉事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.2、彈窗

代碼:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 09:46
 * @desc 彈窗
 **/
//主視窗
public class DialogDemo1 extends JFrame {
    public static void main(String[] args) {
        new DialogDemo1();
    }

    public DialogDemo1() {
        this.setVisible(true);
        this.setBounds(277, 277, 577, 577);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//        JFrame 存放東西,容器
        Container container = this.getContentPane();
//        絕對布局
        container.setLayout(null);
//        按鈕
        JButton jButton = new JButton("彈窗顯靈!");
        jButton.setBounds(100, 100, 100, 70);
//       點選此按鈕,會有彈窗。
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                彈窗
                new MyDialogDemo();
            }
        });
        container.add(jButton);
    }
}

//彈窗
class MyDialogDemo extends JDialog {
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBackground(Color.BLACK);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.add(new Label("跟着老秦學Java"));
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.3、标簽

Label

圖示’s Icon

代碼:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 10:48
 * @desc 标簽。圖示需要實作類,Frame繼承等。
 **/
public class IconDemo1 extends JFrame implements Icon {
    private int width;
    private int height;

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

    //無參構造
    public IconDemo1() {
    }
    //有參構造
    public IconDemo1(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init() {
        IconDemo1 iconDemo1 = new IconDemo1(17, 17);
        JLabel label = new JLabel("icontest", iconDemo1, SwingConstants.CENTER);

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

        this.setVisible(true);
        this.setBounds(277, 277, 677, 677);
        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;
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

圖中的小點點,就是我們創造的圖示。

2.0

圖檔’s Icon

代碼:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 11:17
 * @desc 圖檔顯示
 **/
public class ImageIconDemo extends JFrame{
    public static void main(String[] args) {
        new ImageIconDemo();
    }

    public ImageIconDemo() {
//        擷取圖檔的位址
        URL url = ImageIconDemo.class.getResource("Study.jpg");
//        label作為承載圖檔的載體。
        JLabel label = new JLabel("圖檔旁邊的小标簽");
//        注意命名,不要和關鍵詞重合。
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

        setVisible(true);
        setBounds(277, 277, 877, 877);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

注:

由于IDEA存在緩存機制,有可能在第一次的編譯過程中會出現空指針異常現象(ImageIcon imageIcon = new ImageIcon(url);),再更改了代碼後依舊是無法正常運轉,建議此時進行重新開機。!!!。!!!

3.4、面闆

JPanel

試用Scroll面闆,帶滾輪,可伸縮。

代碼:

package com.edwin.lesson05;

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

/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:29
 * @desc
 **/
public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo();
    }

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

//        文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
                "But we must hand on it and try our best to do.");
//        Scroll面闆,帶滾輪,可伸縮。
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(277, 277, 300, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.5、按鈕

圖檔按鈕:

将按鈕的内容設定為一個圖檔,點選後是按鈕的效果:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:47
 * @desc 按鈕
 **/
public class JButton1 extends JFrame {
    public static void main(String[] args) {
        new JButton1();
    }

    public JButton1() {
        Container container = this.getContentPane();
//        将一個圖檔,變為圖示
        URL url = JButton1.class.getResource("Study.jpg");
        Icon icon = new ImageIcon(url);

//        把圖檔放在按鈕上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("圖檔按鈕");

//        add
        container.add(button);

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻
  • 單選按鈕

多個選項隻能選取一個,按組進行分類,一個組選出來一個。

代碼:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 06:05
 * @desc 單選框按鈕
 **/
public class JButton2 extends JFrame {
    public static void main(String[] args) {
        new JButton2();
    }

    public JButton2() {
        Container container = this.getContentPane();
//        單選框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton3");

//        由于單選框隻能選擇一個,是以說我們采取“分組”的方法,一個組内隻有一個可以被選中
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

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

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻
  • 複選按鈕

每個選項單獨成組,可以一次性選擇多個按鈕内容。

代碼:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 06:10
 * @desc 多選框
 **/
public class JButton3 extends JFrame {
    public static void main(String[] args) {
        new JButton3();
    }

    public JButton3() {
        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.setBounds(277, 277, 500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.6、清單

  • 下拉框

代碼:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 06:27
 * @desc 下拉框
 **/
public class TextCombobox1 extends JFrame {
    public static void main(String[] args) {
        new TextCombobox1();
    }

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

        JComboBox status = new JComboBox();
        status.addItem("起始預設字");
        status.addItem("正在熱映");
        status.addItem("即将上映");
        status.addItem("已下架");

        container.add(status);

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻
  • 清單框

應用場景:

​ - 填寫資訊時選擇地區,或者一些單選項目。

​ - 展示清單資訊,一般是動态擴容的。

代碼:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 06:27
 * @desc 下拉框
 **/
public class TextCombobox1 extends JFrame {
    public static void main(String[] args) {
        new TextCombobox1();
    }

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

//        生成清單内容
        String[] content = {"7", "77", "777"};

        Vector vector = new Vector();

        vector.add("Alita");
        vector.add("Jack");
        vector.add("Toney");

//        清單中需要放入的内容
        JList jList1 = new JList(content);
        JList jList2 = new JList(vector);

        container.add(jList1);
        container.add(jList2);

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

3.7、文本框

  • 文本框

代碼:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 07:15
 * @desc 文本框
 **/
public class TextText1 extends JFrame {
    public static void main(String[] args) {
        new TextText1();
    }

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

        JTextField textField1 = new JTextField("Hello");
        JTextField textField2 = new JTextField("World!",20);//規定最多20個字元。

        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻
  • 密碼框

代碼:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 07:20
 * @desc 密碼框
 **/
public class TextPassword1 extends JFrame {
    public static void main(String[] args) {
        new TextPassword1();
    }

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

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

        container.add(passwordField);

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

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻
  • 文本域

同Lesson5裡面的文本域

代碼:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:29
 * @desc
 **/
public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo();
    }

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

//        文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
                "But we must hand on it and try our best to do.");
//        Scroll面闆
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(277, 277, 300, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}
           

輸出:

Java基礎-GUI入門-Swing詳解3.Swing參考文獻

路漫漫其修遠兮,吾将上下而求索。

參考文獻

《【狂神說Java】GUI程式設計入門到遊戲實戰》

視訊連結

2020.08.19