Java GUI之---[观看bilibili-狂神说Java--学习笔记]

1. AWT


1.1 AWT介绍
  1. 包含了很多类和接口!GUI:
  2. 元素:窗口、按钮、文本框
  3. java.awt

在这里插入图片描述

1.1.1 组件和容器
  1. Frame
package com.zj.lesson1;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {

    public static void main(String[] args) {
        //Frame
        Frame frame = new Frame("我的第一个java图像窗口");

        //需要设置可见性
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);

        //设置背景颜色
        frame.setBackground(new Color(72, 187, 32));

        //弹出的初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);
    }
}
//出现问题:无法关闭窗口,只能停止程序
  1. 面板Panel

解决了窗口无法关闭的问题,
出现问题:效果未像狂老师那样

原因:如果不主动设置layout(布局管理器),默认是BorderLayout,
frame.add(contentPane);这条语句默认情况下将contentPane放在了BorderLayout.CENTER位置,且frame上没有其它的组件,因此contentPane覆盖了frame的全部区域。

package com.zj.lesson1;

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

//Panel 可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //布局的概念
        Panel panel = new Panel();
        Panel panel1 = new Panel();


        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,0,1000,1000);
        frame.setBackground(new Color(40,161,35));

        //panel设置坐标,相对于frame
        panel.setBounds(100,50,200,200);
        panel.setBackground(new Color(193,15,60));

        //panel设置坐标,相对于frame
        panel1.setBounds(500,350,200,200);
        panel1.setBackground(new Color(15, 27, 193));

        //frame.add(panel)
        frame.add(panel);
        frame.add(panel1);
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件 System.exit(0);
        //适配器模式
        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {

            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭时的事件
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

  1. 布局管理器
  • 流式布局
package com.zj.lesson1;

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

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(500,500);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });


    }
}

  • 东西南北中

在这里插入图片描述

package com.zj.lesson1;

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

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

        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(500,500);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

  • 表格布局 Grid
package com.zj.lesson1;

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

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

        Frame frame = new Frame("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");

        frame.setLayout(new GridLayout(3, 2));
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();//java 的函数 自动选择最优的布局,自动填充,不需要设置大小
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}


表格布局

  1. 大小,定位,背景颜色,可见性,监听
布局管理器

package com.zj.practice;

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

//联系frame,panel,button,布局
public class Exdemo01 {
  public static void main(String[] args) {
    //总frame
    Frame frame = new Frame("练习题");
    frame.setSize(400, 300);
    frame.setLocation(300, 400);
    frame.setBackground(Color.BLACK);
    frame.setVisible(true);
    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("p2-btn-1"));
    panel2.add(new Button("p2-btn-2"));
    panel1.add(panel2, BorderLayout.CENTER);

    //下面
    panel3.add(new Button("East-2"), BorderLayout.EAST);
    panel3.add(new Button("West-2"), BorderLayout.WEST);
    for (int i = 0; i < 4; i++) {
      panel4.add(new Button("for-" + i));
    }
    panel3.add(panel4);
    frame.add(panel1);
    frame.add(panel3);
    frame.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        super.windowClosing(e);
        System.exit(0);
      }
    });
  }
}

总结:

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中
  3. 布局管理器
    1. 流式
    2. 东西南北中
    3. 表格
  4. 大小、定位、背景颜色、可见性、监听
事件监听

事件监听:当某个事件发生的时候,干什么。

package com.zj.lesson2;

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();
        frame.setVisible(true);

        windowClose(frame);
    }

    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

//事件监听
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

多个按钮,共享一个事件实现

package com.zj.lesson2;

import javax.swing.*;
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.setVisible(true);
    }
}

class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {

        //e.getActionCommand()获得按钮的信息
        System.out.println("按钮被点击了:msg"+e.getActionCommand());

        if (e.getActionCommand().equals("start")){
            System.out.println("你点击了开始按钮");
        }
        else if (e.getActionCommand().equals("button2-stop")){
            System.out.println("点击了退出按钮");
            System.exit(0);
        }
    }
}
输入框TextField监听
package com.zj.lesson2;

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

public class TestTextField {
    public static void main(String[] args) {
        //服务器应该只有一个启动类
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionLictener myActionLictener = new MyActionLictener();
        //按下Enter就会触发这个事件
        textField.addActionListener(myActionLictener);
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class MyActionLictener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        e.getSource();//获得一些资源

        TextField field = (TextField) e.getSource();
        System.out.println(field.getText());//获得文本框输入的文本
        //每次输入清空
        field.setText("");//null.""
    }
}
画笔
package com.zj.lesson2;

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();
        frame.setVisible(true);

        windowClose(frame);
    }

    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

//事件监听
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

鼠标监听
package com.zj.lesson3;

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, 400, 400);

        //存鼠标点击点
        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.cyan);
                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 myFreame = (MyFrame) e.getSource();
               //TODO :这里我们点击的时候,就会在界面产生一个点
                // 这个点就是鼠标的点
                myFreame.addPaint(new Point(e.getX(),e.getY()));
                //每次点击鼠标都需要重新画一遍
                myFreame.repaint();
            }
        }

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UmhtMrc9-1654312239998)(img.png)]

窗口监听

class WindowFrame extends Frame {
    public WindowFrame(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        addWindowListener(new MyWindowListener());
    }
    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口,通过按钮
            System.exit(0);
        }
    }
}

优化

class WindowFrame extends Frame {
    public WindowFrame(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);//隐藏窗口,通过按钮
                System.exit(0);
            }
        });
    }
}
键盘监听
package com.zj.lesson3;

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(){
        setBounds(1,2,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //键盘按下
                //不需要去记录这个数值,直接使用静态属性VK_XXX
                System.out.println(e.getKeyCode());
                if (e.getKeyCode()==KeyEvent.VK_UP)
                {
                    System.out.println("你按下了上键");
                }
                //根据按下不同操作,产生不同结果
            }
        });
    }

}

Swing

窗口、面板

package com.zj.lesson4;

import javax.swing.*;

public class JFrameDemo {
    //init();初始化
    public void init(){
        //顶级窗口
        JFrame jframe = new JFrame("这是一个JFrame窗口");
        jframe.setVisible(true);
        jframe.setBounds(100,100,200,200);

        //设置文字 Jlabel
        JLabel jLabel = new JLabel("hello,world");
        jframe.add(jLabel);

        //关闭事件
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}

标签居中

package com.zj.lesson4;

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

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJframe2().init();
    }
}
class MyJframe2 extends JFrame{
    public void init(){
        this.setBounds(10,10,200,200);
        this.setVisible(true);
        //设置文字 Jlabel
        JLabel jLabel = new JLabel("hello,world");
        this.add(jLabel);
        //让文本居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        //获得一个容器
        Container contentPane = this.getContentPane();
        contentPane.setBackground(Color.BLUE);
    }
}

弹窗

弹窗默认就有退出不需要this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

package com.zj.lesson4;

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 button = new JButton("点击弹出一个对话框");//创建
        button.setBounds(30,30,200,50);
        container.add(button);
        //点击这个按钮的时候,弹出一个弹窗
        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() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel label = new JLabel("学习java真开心");
        label.setBounds(50,50,100,100);
        container.add(label);
    }
}

标签

label

new label("xxx")

面板

JPanel

package com.zj.lesson5;

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 jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel1 = new JPanel(new GridLayout(2,1));
        JPanel jPanel2 = new JPanel(new GridLayout(2,3));
        JPanel jPanel3 = new JPanel(new GridLayout(1,1));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("2"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel3.add(new JButton("4"));
        this.setVisible(true);
        //this.setBounds(50,50,400,400);
        container.add(jPanel);
        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

滚动条 JScrollPanel

package com.zj.lesson5;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("Hello World");
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);

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

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

按钮

图片按钮

package com.zj.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container container = this.getContentPane();
        //将图片变成图标
        URL resource = JButtonDemo01.class.getResource("tx.jpg");//这里报异常,重启一下IDEA
        //URL url = ImageIconTest.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(resource);
        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

  • 单选按钮
package com.zj.lesson5;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container container = this.getContentPane();
        //将图片变成图标
        URL resource = JButtonDemo02.class.getResource("tx.jpg");//这里报异常,重启一下IDEA
        //URL url = ImageIconTest.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(resource);
        //弄一个单选框
        JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");

        //单选框只能选择一个,分组,不分组可以多选
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);
        //add
        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

  • 复选按钮
package com.zj.lesson5;

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

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container container = this.getContentPane();
        //将图片变成图标
        URL resource = JButtonDemo02.class.getResource("tx.jpg");//这里报异常,重启一下IDEA
        //URL url = ImageIconTest.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(resource);
        //弄一个多选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");

        container.add(jCheckBox1,BorderLayout.CENTER);
        container.add(jCheckBox2,BorderLayout.NORTH);
        container.add(jCheckBox3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

列表

  • 下拉框
package com.zj.lesson6;

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

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container = new Container();
        JComboBox status = new JComboBox();
        status.addItem(null);//项数为-1
        status.addItem("正在上映");//为0
        status.addItem("已下架");
        status.addItem("即将上映");
        status.setSize(200,200);
        status.setBackground(Color.black);
        status.setVisible(true);
        //监听
        //status.addActionListener();
        //status.addItemListener();
        System.out.println(status.getSelectedIndex());//返回项数
        System.out.println(status.getSelectedItem());//返回内容
        container.add(status,BorderLayout.CENTER);
        this.add(container);
        this.setSize(500,350);
        this.setVisible(true);
        status.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

  • 列表框
package com.zj.lesson6;

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

public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02(){
        Container container = new Container();
        //生成列表的内容
        String[] contents = {"1","2","3"};
        //列表中需要放内容
        JList jList = new JList(contents);
        //Vector vector = new Vector();
        //JList jList1 = new JList(vector);
        //vector.add("1");
        //vector.add("12");
        //vector.add("13");
        container.add(jList);
        //container.add(jList1);
        container.setVisible(true);
        this.add(jList);
        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}

  • 应用场景

    • 选择地区,或者一些单个选项
    • 列表,展示信息,一般是动态扩容

文本框

  • 文本框
package com.zj.lesson6;

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

//文本框
public class TestTextDemo01 extends JFrame
{
    public TestTextDemo01() throws HeadlessException {
        Container container = this.getContentPane();


        JTextField jTextField = new JTextField("hello");
        JTextField jTextField2 = new JTextField("world",20);
        container.add(jTextField,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);
        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

  • 密码框
package com.zj.lesson6;

import javax.swing.*;
import java.awt.*;
//密码框
public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() throws HeadlessException {
        Container container = this.getContentPane();

        //面板制作,不用
        JPasswordField jTextField = new JPasswordField();//***
        jTextField.setEchoChar('*');
        container.add(jTextField,BorderLayout.NORTH);
        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

  • 文本域
package com.zj.lesson6;

import com.zj.lesson5.JScrollDemo;

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

//文本域
public class TestTextDemo03 extends JFrame {
    public TestTextDemo03() throws HeadlessException {
        Container container = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("好好学习,天天向上");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        container.add(scrollPane);
        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhoujian970

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值