【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea

GUI编程

03 Swing

3.6 列表

  1. 下拉框
package com.duo.lesson06;

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

public class ComboBoxDemo01 extends JFrame {
    public ComboBoxDemo01() throws HeadlessException {
        Container contentPane = getContentPane();

        JComboBox<Object> objectJComboBox = new JComboBox<>();
        objectJComboBox.addItem(null);
        objectJComboBox.addItem("热映中");
        objectJComboBox.addItem("即将上映");
        objectJComboBox.addItem("已下架");

        objectJComboBox.setFont(new Font("宋体", Font.BOLD, 20));

        contentPane.add(objectJComboBox);

        setTitle("下拉列表测试窗口");
        setVisible(true);
        setSize(350, 100);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea_第1张图片

  1. 列表框
package com.duo.lesson06;

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

public class ComboBoxDemo02 extends JFrame {
    public ComboBoxDemo02() {
        Container contentPane = getContentPane();

        //先生成一个列表
        String[] contents = {"One", "Two", "Three"};
        //用于展示contents的列表框
        JList<Object> objectJList1 = new JList<>(contents);

        contentPane.add(objectJList1);

        setTitle("列表框测试窗口");
        setVisible(true);
        setSize(650, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea_第2张图片

如图,列表框JList用于展示列表中的参数量。

列表的具体应用场景:

  • 下拉列表:选择地区,或一些单个的选项
  • 列表框:展示信息(可以自动动态扩容)

3.7 文本框

  1. 文本框
package com.duo.lesson06;

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

public class TextDemo01 extends JFrame {
    public TextDemo01() {
        Container contentPane = getContentPane();
        contentPane.setLayout(null);  //设置为绝对布局

        JTextField jTextField = new JTextField("Hello", 20);
        jTextField.setBounds(20, 20, 100, 25);
        JTextField jTextField2 = new JTextField("world", 20);
        jTextField2.setBounds(20, 50, 100, 25);

        contentPane.add(jTextField);
        contentPane.add(jTextField2);

        setTitle("文本框测试窗口");
        setVisible(true);
        setSize(650, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea_第3张图片

如上,此窗口面板设为了绝对布局。

  1. 密码框
package com.duo.lesson06;

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

public class TextDemo02 extends JFrame {
    public TextDemo02() {
        Container contentPane = getContentPane();

        JPasswordField jPasswordField = new JPasswordField();  //密码框文本域默认输入为小黑圆点
        //jPasswordField.setEchoChar('*');  //同样可通过语句来设置输入文本后的显示样式为'*'
        contentPane.add(jPasswordField);

        setTitle("文本框测试窗口");
        setVisible(true);
        setSize(650, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea_第4张图片

  1. 文本域
package com.duo.lesson06;

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

public class TextDemo03 extends JFrame {

    public TextDemo03() {
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        JTextArea jTextArea = new JTextArea("This is Schrodinger's channel.");
        jTextArea.setFont(new Font("Times New Roman", Font.PLAIN, 22));
        Dimension preferredSize = jTextArea.getPreferredSize();  //获得文本域的首选大小
        jTextArea.setBounds((650 - preferredSize.width) / 2,
                (500 - preferredSize.height) / 2 - preferredSize.height / 2,
                preferredSize.width, preferredSize.height);

        contentPane.add(jTextArea);

        setTitle("文本域测试窗口");
        setVisible(true);
        setSize(650, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea_第5张图片


你可能感兴趣的:(java,笔记)