Java GUI编程

AWT图形化开发:

AWT是java最开始的图形化开发体系,awt的皮肤都是调用于操作系统的,依赖着操作系统。
awt里最高父类是awt包下的Component组件类,继承于组件类的是Container容器类。
容器类的分支有Panel无边框容器和Window有边框容器
Window下分支有Frame可以放大缩小窗口,和Dialog这个只能在最上面显示并且不能放大缩小的窗口类。
组件类下还有一个Button按钮类和一个TextComponent文本框组件类。
TextField、TextArea、Checkbox等文本框都是继承于文本框组件类。

Swing图形化开发:

Swing则是后来发展的图形化开发体系,纯java的图形化开发,图形化风格不一样,并且没有丢弃awt的结构。
Swing组件类继承于awt的Container容器类。
Swing里的无边框容器,继承于JComponent,有边框容器,则继承于awt的Window类。
Swing里的JFrame继承于awt中的Frame类,JDialog继承于awt的Dialog类。
文本框组件类JTextComponent继承于awt的TextComponent。
JTextField继承于TextField,JTextArea继承于TextArea,JPasswordField密码文本框,继承于JTextField。
Swing里有个抽象Button类,是继承于JComponent组件类的。
JButton、JToggleButton都是继承于抽象Button类。
JCheckBox、JRadioButton则继承于JToggleButton。

image-20210514163521294

image-20210514162929104

图形化开发的一些常用方法:

add(popupMenu) 添加一个弹出式菜单
setBackground(color) 设置背景颜色 color RGB 三原色调配
setBounds(int,int,x,y) 设置 坐标x,坐标y,宽,高
setBounds(Rectangle) 调用Rectangle包装好的 坐标x,坐标y,宽,高
setCursor(Cursor) 设置光标 去找color这个类的常量
setFont(Font) 设置字体 例如:new Font("仿宋",Font.b,12);
setForegroud(Color) 设置字体颜色
setLocation(int,int) 设置坐标
setLocation(Point) 调用Point 封装好的坐标x,坐标y
setSize(int,int) 设置窗口的宽和高
setSize(Dimension)
setVisible(boolean) true 组件显示 false则组件隐藏 窗口中也可以控制
getBackground() 得到背景颜色
getBounds() 得到 坐标x,坐标y,宽,高
getCursor() 得到光标
getFont() 得到字体
getForeground() 得到字体颜色
getHeight() 得到高
getLocation() 得到地址
getSize() 得到尺寸
getWidth() 得到宽
getx() 得到x坐标
gety() 得到y坐标

组件和容器

public class TestFrame {
    public static void main(String[] args) {

        // Frame
        Frame frame = new Frame("Hello GUI!");
        // 需要设置可见性
        frame.setVisible(true);
        // 设置窗口大小
        frame.setSize(400, 400);
        // 设置背景颜色 Color
        frame.setBackground(new Color(76, 173, 165));
        // 设置初始位置
        frame.setLocation(400, 200);
        // 设置大小固定
        frame.setResizable(false);
    }
}

image-20210513193335219

问题: 窗口无法关闭

尝试封装:

public class TeatFrame2 {
    public static void main(String[] args) {
        MyFrame m1 = new MyFrame(400, 300, 300, 100, Color.BLUE);
        MyFrame m2 = new MyFrame(400, 400, 300, 100, Color.orange);
        MyFrame m3 = new MyFrame(700, 300, 300, 100, Color.red);
        MyFrame m4 = new MyFrame(700, 400, 300, 100, Color.green);
    }
}

class MyFrame extends Frame {
    static int id = 0;

    /**
     * x,y 设置初始位置
     * w,h 设置初始大小
     * color 设置背景色
     */
    public MyFrame(int x, int y, int w, int h, Color color) {
        super("MyFrame:" + (++id));
        setBackground(color);
        setVisible(true);
        setBounds(x, y, w, h);
    }
}

image-20210513200245755

面板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, 400, 400);
        frame.setBackground(new Color(255, 255, 255));

        // panel 设置坐标(相对于frame)
        panel.setBounds(50, 50, 300, 300);
        panel.setBackground(new Color(179, 179, 179, 163));

        // frame.add(panel);
        frame.add(panel);

        frame.setVisible(true);

        // 监听事件,监听窗口关闭事件 System.exit(0)
        // 适配器模式:直接new WindowListener()太多不需要的,可以new一个它的实现类来操作
        frame.addWindowListener(new WindowAdapter() {
            // 窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                // 结束程序
                System.exit(0);
            }
        });
    }
}

布局管理器

- 流式布局

public class TestFlowLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        // 组件 - 按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        // 当用户关闭应用程序时,终止程序
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 设置为流式布局
//        frame.setLayout(new FlowLayout());
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setBounds(200, 200, 400, 400);

        // 把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

image-20210514183618148

- 东南西北中

public class TestBorderLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("TestBorderLayout");

        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.setBounds(300, 300, 400, 400);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

image-20210514183659697

- 网格布局

​ 创建一个网格布局,默认情况下在每个行中每个组件一列
参数:
​ rows–行,值为零表示任意数量的行
​ cols –列,值为零表示任意数量的列
​ hgap –水平间隙
​ vgap –垂直间隙

public class TestGridLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("TestGridLayout");

        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        /**
         * 创建一个网格布局,默认情况下在每个行中每个组件一列
         * 参数:
         * rows–行,值为零表示任意数量的行
         * cols –列,值为零表示任意数量的列
         * hgap –水平间隙
         * vgap –垂直间隙
         */
        frame.setLayout(new GridLayout(3, 2));
        frame.setBounds(500, 300, 300, 300);

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();   // 根据窗口里面的布局及组件的preferredSize来确定frame的最佳大小。
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

练习

image-20210514201143580

public class Demo {
    public static void main(String[] args) {
        JFrame demo = new JFrame("demo");
        demo.setBounds(500, 200, 400, 300);
        demo.setVisible(true);
        demo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        demo.setLayout(new GridLayout(2, 1));   // 定义布局为表格式(panel1 panel2)

        // new Panel()使用指定的布局管理器创建一个新面板。
        // new BorderLayout()构造一个新的边框布局,组件之间没有间隙
        Panel panel1 = new Panel(new BorderLayout());   // 上切割布局
        Panel panel2 = new Panel(new BorderLayout());   // 下切割布局
        Panel panel3 = new Panel(new GridLayout(2, 1)); // panel1.add()中的区域,两行一列
        Panel panel4 = new Panel(new GridLayout(2, 2)); // panel2.add()中的区域,两行两列

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

        panel3.add(new Button("1"));
        panel3.add(new Button("2"));

        panel4.add(new Button("3"));
        panel4.add(new Button("4"));
        panel4.add(new Button("5"));
        panel4.add(new Button("6"));

        // 将panel嵌套到另一个panel中,最终添加到demo中
        panel1.add(panel3, BorderLayout.CENTER);
        panel2.add(panel4, BorderLayout.CENTER);
        demo.add(panel1);
        demo.add(panel2);
    }
}

事件监听

适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

public class TestActionEvent {
    public static void main(String[] args) {
        // 按下按钮,触发一些事件
        JFrame jFrame = new JFrame();
        Button button = new Button("aaaa");

        // 因为 addActionListener() 需要一个ActionListener,所以需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        jFrame.add(button);
        jFrame.setBounds(300, 300, 300, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗口
    }
}

// 事件监听
class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("abc");
        // 开始套娃
        JFrame jFrame1 = new JFrame();
        Button button = new Button("bbbb");

        jFrame1.add(button);
        jFrame1.setBounds(500, 300, 300, 200);
        jFrame1.setVisible(true);
        jFrame1.setDefaultCloseOperation(jFrame1.EXIT_ON_CLOSE);
    }
}

输入框事件监听

public class TestField {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {
    public MyFrame() {
        TextField textField = new TextField();
        add(textField);

        // 监听文本框内容
        MyActionListener2 myActionListener2 = new MyActionListener2();
        // 回车监听事件,按下enter 就会出发这个输入框的事件
        textField.addActionListener(myActionListener2);

        // 替换文本框中显示的编码
        textField.setEchoChar('*');

        setVisible(true);
        setBounds(300, 300, 300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class MyActionListener2 implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();  // 最初发生事件的对象,获得一些资源,返回一个对象
        System.out.println(field.getText());    // 获得输入框中的文本
        field.setText(""); // 设置回车显示字符 null
    }
}

练习:简易计算器

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

// 计算器类
class Calculator2 extends JFrame {
    public Calculator2() {
        JFrame jFrame = new JFrame("简易加法计算器");
        // 三个文本框
        TextField field1 = new TextField(10);// 字符数
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        // 一个按钮
        Button button = new Button("=");
        // 一个标签
        Label label = new Label("+");
        // 布局
        jFrame.setLayout(new FlowLayout());

        jFrame.add(field1);
        jFrame.add(label);
        jFrame.add(field2);
        jFrame.add(button);
        jFrame.add(field3);

        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 监视器
        MyCalculator2 myC = new MyCalculator2(field1, field2, field3);
        field1.addActionListener(myC);
        field2.addActionListener(myC);
        field3.addActionListener(myC);
        button.addActionListener(myC);
    }
}

// 监听器类
class MyCalculator2 implements ActionListener {
    // 获取三个变量
    private TextField num1, num2, num3;

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

    @Override
    public void actionPerformed(ActionEvent e) {
        // 1. 获得加数被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        // 2. 将这个值 + 法运算后,放到第三个框
        num3.setText("" + (n1 + n2));

        // 3. 清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

鼠标监听

画笔

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

class MyPaint extends Frame {

    public void loadFrame() {
        setVisible(true);
        setBounds(300, 300, 600, 500);
    }

    // 画笔
    @Override
    public void paint(Graphics g) {
        // 画笔,需要有颜色可以画画
        g.setColor(Color.RED);
        g.drawOval(20, 50, 200, 200);  // drawOval空心圆
        g.fillOval(220, 50, 200, 200); // fillOval实心圆
        g.setColor(Color.BLUE);
        g.fillRect(20, 250, 200, 200);
        // 养成习惯,画笔用完,将它还原到最初的颜色
    }
}

image-20210516155250648

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

// 自己的类
class MyFrame extends Frame {
    // 画面需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;

    public MyFrame(String title) {
        super(title);
        setBounds(300, 300, 300, 300);
        //  村鼠标的点
        points = new ArrayList<>();

        setVisible(true);
        // 鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
    }

    @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);
    }

    /**
     * 适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。
     * Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
     */
    private class MyMouseListener extends MouseAdapter {
        // 鼠标: 按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {    // e:获取鼠标信息
            MyFrame myFrame = (MyFrame) e.getSource();
            // 点击界面时,产生一个点,这个点就是鼠标产生的点
            myFrame.addPaint(new Point(e.getX(), e.getY()));

            // 每次点击鼠标都需要重新画一遍
            myFrame.repaint();
        }
    }
}

键盘监听

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

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.LIGHT_GRAY);
        setBounds(300, 300, 300, 300);
        setVisible(true);
        // 方式一 调用内部类
        // addWindowListener(new MyWindowListener());

        // 方式二 推荐使用欧冠匿名内部类
        this.addWindowListener(new WindowAdapter() {
            // 关闭窗口时
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }

            // 激活窗口时
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame source = (WindowFrame) e.getSource();
                source.setTitle("已被激活");    // 被激活时窗口显示
                System.out.println("windowActivated");
            }
        });
    }
}

JFrame

注意:

add() 属于awt中的方法,只能使用BorderLayout定位,SwingConstants属于swing中的方法不能使用!!

标签居中

public class JFrameDemo {
    // init初始化
    public void init() {
        JFrame jFrame = new JFrame("这是一个JFrame窗口!");
        jFrame.setBounds(300, 300, 350, 100);
        jFrame.setVisible(true);
        // 关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 设置文字 JLabel
        // SwingConstants 设置组件位置
        JLabel jLabel = new JLabel("炉石酒馆欢迎你~", SwingConstants.CENTER);
        jFrame.add(jLabel);
        // getContentPane() 获得一个容器
        jFrame.getContentPane().setBackground(Color.CYAN);
    }

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

弹窗

JDialog 默认存在关闭事件

// 主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setTitle("启动界面");
        this.setVisible(true);
        this.setBounds(300, 300, 300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // JFrame 储存容器
        Container contentPane = this.getContentPane();
        // 绝对布局
        contentPane.setLayout(null);

        // 按钮
        JButton button = new JButton("点击开始游戏");// 创建
        button.setBounds(30, 30, 200, 50);
        this.add(button, BorderLayout.CENTER);

        // 点击这个按钮的时候,弹出一个弹窗
        button.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() {
        JFrame jframe = new JFrame("准备开始");
        jframe.setVisible(true);
        jframe.setBounds(600, 300, 300, 300);
        JLabel jLabel = new JLabel("Loading~~~~~~", SwingConstants.CENTER);
        jframe.add(jLabel);
    }
}

标签

// 图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {

    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(15, 15);
        // 图标放在标签上,也可以放在按钮上
        JLabel jLabel = new JLabel("Icontest", iconDemo, SwingConstants.CENTER);
        // 创建带有初始文本和图标的按钮
      	JButton button = new JButton("标签", iconDemo);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        contentPane.add(button, BorderLayout.SOUTH);

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

    public static void main(String[] args) {
        new IconDemo().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;
    }
}

图标

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        Container contentPane = getContentPane();
        // 获取图片地址
        URL url = ImageIconDemo.class.getResource("img.png");
        // 将图片转换为图标
        ImageIcon imageIcon = new ImageIcon(url);

        // 把这个图标放在按钮上
        JLabel jLabel = new JLabel("ImageIconDemo");
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        contentPane.add(jLabel);

        this.setVisible(true);
        this.setBounds(300, 300, 320, 280);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

JScrollPane 面板

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container contentPane = getContentPane();
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("JScrollDemo面板实现");   // 设置默认文本
        // Scroll面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        contentPane.add(scrollPane);

        this.setVisible(true);
        this.setBounds(300, 300, 200, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

按钮

单选框
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02() {
        Container contentPane = this.getContentPane();
        JButton button = new JButton("确定");
        contentPane.add(button, BorderLayout.SOUTH);
      	setTitle("单选框");

        // 单选框
        JRadioButton button1 = new JRadioButton("男");
        JRadioButton button2 = new JRadioButton("女");
        JRadioButton button3 = new JRadioButton("畸形");
        // 分组:分组可以使单元框只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);

        contentPane.add(button1, BorderLayout.WEST);
        contentPane.add(button2, BorderLayout.CENTER);
        contentPane.add(button3, BorderLayout.EAST);

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

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
复选框
public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container contentPane = this.getContentPane();
        JButton button = new JButton("确定");
        contentPane.add(button, BorderLayout.SOUTH);
        setTitle("多选框");

        // 多选框
        JCheckBox jCheckBox1 = new JCheckBox("java");
        JCheckBox jCheckBox2 = new JCheckBox("mysql");
        JCheckBox jCheckBox3 = new JCheckBox("python");

        contentPane.add(jCheckBox1, BorderLayout.WEST);
        contentPane.add(jCheckBox2, BorderLayout.CENTER);
        contentPane.add(jCheckBox3, BorderLayout.EAST);

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

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

列表

下拉框
public class TestComboxDemo01 extends JFrame {
    public TestComboxDemo01() {
        Container contentPane = this.getContentPane();
        JPanel jPanel = new JPanel();

        JLabel label = new JLabel("选择一款游戏:");
        JComboBox box = new JComboBox();
        box.addItem(null);
        box.addItem("穿越火线");
        box.addItem("守望先锋");
        box.addItem("炉石传说");

        jPanel.add(label, BorderLayout.SOUTH);
        jPanel.add(box, BorderLayout.SOUTH);
        contentPane.add(jPanel);

        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(box.getSelectedIndex()); // 返回项数
                System.out.println(box.getSelectedItem());  // 返回内容
            }
        });
        
        this.setVisible(true);
        this.setBounds(300, 300, 300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboxDemo01();
    }
}
列表框
public class TestComboxDemo02 extends JFrame {
    public TestComboxDemo02() {
        Container contentPane = this.getContentPane();
        JPanel jPanel = new JPanel();

        // 生成列表的内容
        // String[] contents = {"第一列", "第二列", "第三列"};

        // 动态存入数据
        Vector contents = new Vector();
        contents.add("三张");
        contents.add("四李");
        contents.add("五王");

        // 把内容放入列表中
        JList jList = new JList(contents);

        jPanel.add(jList);
        contentPane.add(jPanel, BorderLayout.NORTH);

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

    public static void main(String[] args) {
        new TestComboxDemo02();
    }
}
  • 应用场景
    • 如选择地区时,或者一些单个选项。
    • 列表,战士信息,一般是动态扩容~

文本框

密码框
public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() {
        Container contentPane = this.getContentPane();
        JPanel jPanel = new JPanel();

        JLabel jLabel = new JLabel("密码:");
        JPasswordField jPasswordField = new JPasswordField(10);
        jPasswordField.setEchoChar('*');    // 设置返回显示的字符

        jPanel.add(jLabel);
        jPanel.add(jPasswordField);
        contentPane.add(jPanel);

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

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
文本域
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container contentPane = getContentPane();
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("JScrollDemo面板实现");   // 设置默认文本
        // Scroll面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        contentPane.add(scrollPane);

        this.setVisible(true);
        this.setBounds(300, 300, 200, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

贪吃蛇

posted @ 2021-05-27 15:53  C_Long  阅读(124)  评论(0)    收藏  举报