【JavaSE】之GUI编程


前言

本文为Java基础GUI编程相关知识,Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~

一、GUI简介

GUI的核心技术: Swing, AWT
GUI缺点与不足:

  • 1.界面不美观
  • 2.需要jre环境

为什么要学习GUI?

  • 可以写出自己心中想要的一些小工具。
  • 工作时候,也可能维护到swing界面,但概率极小。
  • 了解MVC架构,了解监听!

二、AWT

1. AWT介绍

  • 包含了很多类和接口!GUI:图像用户界面。 Eeclipse:java环境写的
  • 元素:窗口、按钮、文本框
  • java.awt包里

2.组件和容器

Frame

package com.wang.lesson1;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame,JDK  看源码;
        Frame frame = new Frame("我的第一个java图形界面窗口");
        //设置可见性 w h 没有
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //背景颜色 Color
        Color color = new Color(155, 89, 104);
        frame.setBackground(color);
        //弹出的初始位置
        frame.setLocation(200, 200);
        //设置大小固定
        frame.setResizable(false);
    }
}

问题:窗口无法关闭—>需通过强制java停止运行来关闭

面板panel

  • 解决了无法关闭问题,即调用addWindowsListener方法的子方法,并重写其中的WindowsClosing方法,来调用程序关闭的.exit(0)方法
package com.wang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel 可以看成一个空间,但是不能单独存在,得放在Frame上
public class TsetPanel {
    public static void main(String[] args) {
       //窗口
        Frame frame = new Frame();
        //布局的概念
        //面板
        Panel panel = new Panel();
        /*设置布局  不设置会默认置顶
        未设置Layout时,java默认为flowLayout布局的,
         设置为null即为清空布局管理器,之后添加组件,常常是设置组件左上角坐标相
         对于容器左上角(0,0)的x,y值来确定组件的位置,即使更改容器大小也不会
         改变位置。这种方式常常用于窗体大小固定的容器里。*/
       frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(59, 164, 125));
        //panel 设置坐标,相对于Frame的坐标
        panel.setBounds(50,50,200,200);
        panel.setBackground(new Color(90, 46, 30));
        //frame.add(panel)frame添加面板
        frame.add(panel);//Panel经过三层继承,最终继承了Component
        frame.setVisible(true);
        //监听时间,监听窗口关闭事件   System.exit(0)
        //适配器模式: new 重写的太多了  new其子类 本来new WindowsListener的,但是要重写的实在太多了
        frame.addWindowListener(new WindowAdapter() {
           //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

3.布局管理器

流式布局 从左到右

package com.wang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TsetFlowLayout {
    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.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setSize(200,200);
        frame.setVisible(true);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

东西南北中

package com.wang.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("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.setSize(300,300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
    }
}

表格布局三行两列这种Grid

package com.wang.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("TsetGridLayout");
        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);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

4.事件监听

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

package com.wang.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 button1 = new Button("button1");
        //因为addActionListener需要ActionListener,因此我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button1.addActionListener(new MyActionListener());
        frame.add(button1, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        windowsClosing(frame);
    }
    //关闭窗体的事件
    private static void windowsClosing(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
//事件监听
    static class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("11");
        }
    }
}

多个按钮共享一个事件

package com.wang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
public class TestActionEvent2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        Frame frame = new Frame("1111");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //可以显示的定义出发会返回的命令,如果不显示定义,则会走默认值
        //可以多个按钮只写一个监听类
        button1.setActionCommand("3333");
        My my = new My();
        button1.addActionListener(my);
        button2.addActionListener(my);
        frame.add(button1, BorderLayout.WEST);
        frame.add(button2, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }
    static class My implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //e.getActionCommand()获得按钮的信息
            System.out.println("按钮被点击了:msg" + e.getActionCommand());
        }
    }
}

5.输入框事件监听

  • 输入框中输入的字,可以打印出来,并将输入的字全部删除。
package com.wang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
    public static void main(String[] args) {
        //启动!只负责启动
        new MyFrame();
}
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        //按下回车键,就会触发这个输入框的事件,在下边的重写方法中重写的语句为  获得输入框的文本并打印
        textField.addActionListener(new My());
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class My implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField text = (TextField) e.getSource();//获得一些资源,返回的一个对象
        System.out.println(text.getText());//获得输入框的文本
        //每次都设置为空 即每次文本框输入完以后,都会全部删除清零
        text.setText("");
    }
}

6.画笔

package com.wang.lesson3;
import java.awt.*;
public class TestPaint {
    public static void main(String[] args) {
        new MYpaint().loadFrame1();
    }
}
class MYpaint extends Frame{
    public void loadFrame1(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        //super.paint(g);有些类的父类有一些初始化操作,不能随便干掉
        //画笔,需要颜色,画笔可以画画
        g.setColor(Color.red);
        g.drawOval(100,100,100,100);
        g.fillOval(200,200,100,100);//实心的⚪
        g.setColor(Color.green);
        g.fillRect(300,300,40,20);
        g.drawRect(300,350,40,20);
        //养成习惯 画笔画完,将他还原到最初的颜色
        g.setColor(Color.BLACK);
    }
}

7.鼠标监听

以实现鼠标画画为例:

package com.wang.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(100, 100, 500, 400);
        //存鼠标的点
        points = new ArrayList<>();
        //鼠标监听器,针对这个窗口
        setVisible(true);
        this.addMouseListener(new MyML());
    }
    @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 MyML extends MouseAdapter {
        //鼠标 按下,弹起,按住不放
        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame myframe = (MyFrame) e.getSource();
            //这里我们点击的时候,就会在界面产生一个点
            myframe.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            myframe.repaint();//刷新
        }
    }
}

8.窗口监听

package com.wang.lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
    public static void main(String[] args) {
        new WindowF();
    }
}
class WindowF extends Frame {
    public WindowF() {
        setBackground(Color.BLUE);
        setBounds(100, 100, 200, 200);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowsClosing");
                        System.exit(0);
                    }
                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowF source = (WindowF) e.getSource();
                        source.setTitle("已激活");
                        System.out.println("windowActivated");
                    }
                });
    }
           /* @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);// 隐藏窗口
                System.exit(0);//正常退出   1是非正常退出
            };*/
}

9.键盘监听

package com.wang.lesson3;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLOutput;
//键
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyF();
    }
}
class KeyF extends Frame{
    public KeyF(){
        setBounds(0,0,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
           //键盘按下
           @Override
            public void keyPressed(KeyEvent e) {
               int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性VK_xxx
               System.out.println(keyCode);
               if (keyCode == KeyEvent.VK_UP){
                   System.out.println("你按了上键盘");
                   //根据不同的操作,进行不同的结果
               }
           }
        });
    }
}

三、Swing

1.窗口、面板JFrame

package com.wang.lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
    //init();初始化
    public void init(){
        //顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setBounds(100,100,400,300);
        //设置文字Label->JLabel
        jf.setBackground(Color.BLUE);
        JLabel jl = new JLabel("JJJJJ");
        jf.add(jl);
        //让文本标签居中
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        //容器实例化
        jf.getContentPane().setBackground(Color.red);
        jf.setVisible(true);
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

2.弹窗

package com.wang.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.setBounds(100, 100, 400, 400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Jframe 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //设置背景
        contentPane.setBackground(Color.BLUE);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");
        jButton.setBounds(30, 30, 200, 50);
        //点击按钮弹出弹框
        jButton.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });
        contentPane.add(jButton);
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialog extends JDialog {
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);
       // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //JDialog退出只能是D0_ONTHING,HIDE,DISPOSE这三个中的一种
        //应该是默认就有关闭事件
        this.setTitle("这是一个弹窗");
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.ORANGE);
        JLabel jjj = new JLabel("学习学习");
        contentPane.add(jjj);
        jjj.setBounds(20,20,50,50);
    }
}

3.标签

label

new JLabel("xxx");

图标Icon

package com.wang.lesson4;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
    private  int width;
    private  int hight;
    public IconDemo(){};//无参构造
    //有参构造
    public IconDemo(int width,int hight){
        this.width = width;
        this.hight = hight;
    };
    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        //图标可以放在标签,也可以放在按钮上!
        JLabel jLabel = new JLabel("标签",iconDemo,SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.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,hight);
    }
    @Override
    public int getIconWidth() {
        return this.width;
    }
    @Override
    public int getIconHeight() {
        return this.hight;
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }
}

图片

package com.wang.lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        JLabel jLabel = new JLabel("图片");
        URL resource = ImageIconDemo.class.getResource("4.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,800,800);
    }
    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

4.面板

JPanel

package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
    public  JPanelDemo(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new GridLayout(2,1,10,10));//后边两个是间距
        JPanel jPanel = new JPanel(new GridLayout(1, 3));
        JPanel jPane2 = new JPanel(new GridLayout(1, 2));
        JPanel jPane3 = new JPanel(new GridLayout(1, 1));
        jPanel.add(new JButton("aaa"));
        jPanel.add(new JButton("bbb"));
        jPanel.add(new JButton("ccc"));
        jPane2.add(new JButton("111"));
        jPane2.add(new JButton("222"));
        jPane3.add(new JButton("---"));
        setBounds(100,100,500,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        contentPane.add(jPanel);
        contentPane.add(jPane2);
        contentPane.add(jPane3);
        setVisible(true);
        contentPane.setBackground(Color.YELLOW);
    }
    public static void main(String[] args) {
        new JPanelDemo();
    }
}

JScrollPanel

package com.wang.lesson4;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo extends JFrame {
    public JScrollPanelDemo(){
        Container contentPane = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("学习学习");
        //面板  并添加到contentpane
        contentPane.add(new JScrollPane(jTextArea));
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        contentPane.setBackground(Color.BLUE);
    }
    public static void main(String[] args) {
        new JScrollPanelDemo();
    }
}

5.按钮

图片按钮

package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = JButtonDemo01.class.getResource("4.png");
        Icon icon = new ImageIcon(resource);
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        //悬浮框
        jButton.setToolTipText("这是一个图片按钮");
        contentPane.add(jButton);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

单选按钮

package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = JButtonDemo01.class.getResource("4.png");
        Icon icon = new ImageIcon(resource);
       //单选框
        JRadioButton jrb01 = new JRadioButton("jrb01");
        JRadioButton jrb02 = new JRadioButton("jrb02");
        JRadioButton jrb03 = new JRadioButton("jrb03");
        //由于单选框只能选择一个,分组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jrb01);
        buttonGroup.add(jrb02);
        buttonGroup.add(jrb03);
        contentPane.add(jrb01,BorderLayout.CENTER);
        contentPane.add(jrb02,BorderLayout.NORTH);
        contentPane.add(jrb03,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

复选按钮

package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
    public JButtonDemo03(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = JButtonDemo01.class.getResource("4.png");
        Icon icon = new ImageIcon(resource);
        //多选框
        JCheckBox jcb1 = new JCheckBox("jcb1");
        JCheckBox jcb2 = new JCheckBox("jcb2");
        JCheckBox jcb3 = new JCheckBox("jcb3");
        //流式布局
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        contentPane.add(jcb1);
        contentPane.add(jcb2);
        contentPane.add(jcb3);
        //东西南北中布局
        /*
         contentPane.add(jcb1,BorderLayout.NORTH);
        contentPane.add(jcb2,BorderLayout.CENTER);
        contentPane.add(jcb3,BorderLayout.SOUTH);
        */
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo03();
    }
}

6.列表

下拉框

package com.wang.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem("未上映");
        status.addItem("正在热映");
        status.addItem("已下架");
        container.add(status);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,400);
    }
    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

列表框
应用场景:

  • 选择地区,或者一些单个选项
  • 列表,展示信息,一般是动态扩展
package com.wang.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02(){
        Container container = this.getContentPane();
        //生成列表的内容
       // String[] contents = {"1","2","3"};
        //列表中需要的内容
        Vector contents = new Vector();
        JList jList = new JList(contents);
        JList jList1 = new JList(contents);
        contents.add("2222");
        contents.add("333");
        container.add(jList);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,400);
    }
    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}

7.文本框

文本框

package com.wang.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container container = this.getContentPane();
        //不布局只会出现WORLD,且位置不对
        this.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JTextField jTextField1 = new JTextField("HELLO");
        JTextField jTextField2 = new JTextField("WORLD",20);
        container.add(jTextField1);
        container.add(jTextField2);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,400,300);
    }
    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

密码框

package com.wang.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        Container container = this.getContentPane();
        JPasswordField jPasswordField = new JPasswordField();//---
        jPasswordField.setEchoChar('-');
        container.add(jPasswordField);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,400,300);
    }
    public static void main(String[] args) {
        new TestTextDemo02();
    }
}

文本域

//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("学习学习");
//面板  并添加到contentpane
contentPane.add(new JScrollPane(jTextArea));

后记

Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小新要变强

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

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

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

打赏作者

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

抵扣说明:

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

余额充值