GUI编程

GUI编程

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件

1.简介

Gui核心技术:Swing,AWT

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

为什么学习

  1. 可以写自己心中想要的一些小工具
  2. 工作的时候,也可能要维护到Swing界面。
  3. 了解MVC架构,了解监听!

2.AWT

2.1、Awt介绍

包含了很多的类和接口!GUI:图形用户接口编程

2.2 组件和容器

窗口,列表,按钮,鼠标

1、Frame创建窗口
import java.awt.*;

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

        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(146, 43, 129));
//        frame.setBackground(Color.BLACK);//可设置的定义好的颜色
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

运行结果
frame结果

问题:窗口关闭不掉,只能停止java程序

打开多个窗口

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.MAGENTA);
    }
}

class MyFrame extends Frame {
    static int id = 0;

    public MyFrame(int x, int y,int w, int h,Color color) throws HeadlessException {
        super("Myframe"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);

    }
}

运行结果

多个窗口建立

2、面板Panel
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//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,500,500);
        frame.setBackground(new Color(40,161,35));

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

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

        frame.setVisible(true);

        //关闭解决方法,
        //监听事件,监听窗口关闭事件,System.exit(0)
        //适配器模式。
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭时候要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

    }

}

运行结构

Panel面板

2.3、布局管理器

  • 流式布局
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.setLayout(new FlowLayout(FlowLayout.RIGHT));//靠右


        frame.setSize(200,200);
        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);
            }
        });
    }
  • 东西南北中
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(200,200);
        frame.setVisible(true);

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


    }
}
  • 表格布局 Grid
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();//自动选择最优的位置


        frame.setVisible(true);

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

2.4、事件监听

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

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;

public class TestActionEvent {
    public static void main(String[] args) {
        //按一下按钮,触发一个事件
        Frame frame = new Frame();
        Button button = new Button();


        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();


        windowClose(frame);
        frame.setVisible(true);


    }

    //关闭窗体的事情
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


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

多个按钮共享一个事情

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

public class TestActionEventTwo {
    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.pack();
        frame.setVisible(true);

    }
}


class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了!:msg"+e.getActionCommand());
    }
}

2.5、输入框TextField监听

public class TestText01 {
    public static void main(String[] args) {
        new Myframe();
    }
}

class Myframe extends Frame{
    public Myframe() throws HeadlessException {
        TextField textField = new TextField();
        add(textField);

        //监听这个文本框输入文字
        MyActionListenerTwo myActionListenerTwo = new MyActionListenerTwo();
        //按下enter就会触发这个输入框事情
        textField.addActionListener(myActionListenerTwo);

        pack();
        setVisible(true);
    }
}

class MyActionListenerTwo implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得一些资源,返回一个对象。
        TextField field= (TextField) e.getSource();
        //获得输入框的文本
        System.out.println(field.getText());
        field.setText("");
    }

2.6、建议计算器,组合+内部类

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

class CalcInner extends Frame{
    TextField num1,num2,num3;
    Button button;
    Label label;
    public void loadFrame(){
        //3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //1个按钮
        button = new Button("=");
        //1个标签
        label = new Label("+");

        button.addActionListener(new MyCalculatorInnerListener());

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();

        //关闭窗口
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);
    }

    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法。

    private class MyCalculatorInnerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            //2.将这个值+法运算后,放到第三个框
            //3.清除前两个框
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}

2.7、 画笔

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

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

}

class MyPaint extends Frame{

    public void loadFrame(){
        setBounds(200,200,600,400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    @Override
    public void paint(Graphics g)  {
        //画笔,需要有颜色,画笔可以画画
        g.setColor(Color.red);
        g.fillOval(100,100,100,100);

        g.setColor(Color.GREEN);
        g.fillRect(150,200,200,200);

        //养成习惯,画笔用完将他还原到最初的颜色

    }
}

2.8 、 鼠标监听

目的:实现鼠标画画。

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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<>();
        //设置可见
        setVisible(true);
        //鼠标监听,正对这个窗口
        this.addMouseListener(new MyMouseListener());

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

    @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  addpoint(Point point){
        points.add(point);
    }
    //适配器模式
    private static class MyMouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            //点一下,就返回一个鼠标的位置 x和 y
            frame.addpoint(new Point(e.getX(),e.getY()));
            frame.repaint();
        }
    }
}

2.9 、窗口监听

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

public class TestWindowTwo {
    public static void main(String[] args) {
        new MyTestWindowTwo();
    }
}

class MyTestWindowTwo extends Frame {
    public MyTestWindowTwo(){
        setBackground(Color.green);
        setBounds(100,100,200,200);
        setVisible(true);

        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        super.windowClosing(e);
                        System.exit(0);
                    }
                    @Override
                    public void windowActivated(WindowEvent e) {
                        super.windowActivated(e);
                        System.out.println("我被激活了")}
                }
        );
    }
}

2.10、 键盘监听

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() throws HeadlessException {
        setBounds(100, 200, 300, 300);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //按下键盘
            @Override
            public void keyPressed(KeyEvent e) {
                //键盘的码
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了向上键");
                }
            }
        });
    }
}

3.Swing

3.1、 窗口、面板

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

public class JFrameDemo {
    //init();初始化方法
    public void init(){
        JFrame jframe = new JFrame("这是一个JFrame窗口");
        jframe.setVisible(true);
        jframe.setBounds(100,100,200,200);
        jframe.getContentPane().setBackground(Color.red);
        //设置文字Jlabel
        JLabel label = new JLabel("欢迎来到Java系列节目");
        jframe.add(label);
        //标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //关闭
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();

    }
}

3.2 、弹窗

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

        //JFame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局,不能更改
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(30,30,200,50);
        //点击弹出新窗口
        button.addActionListener(new ActionListener() {
            //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();

            }
        });
        container.add(button);

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


class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);

        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel jlabel = new JLabel("学java");
        jlabel.setBounds(100,100,100,100);
        jlabel.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(jlabel);
    }
}

3.3、标签

label

new JLabel("xxx");

图标 ICON

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

public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    //无参构造
    public IconDemo() throws HeadlessException {
    }

    public IconDemo(int width, int height) throws HeadlessException {
        this.width = width;
        this.height = height;
    }

    public void init(){
        IconDemo iconDemo = new IconDemo(15,15);
        //图标放在标签,也可以放在按钮上
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.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;
    }
}

ImageIcon

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() throws HeadlessException {
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("tt.png");

        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

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

3.4、面板

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

public class JPanelDemo extends JFrame {
    public JPanelDemo() throws HeadlessException {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));

        JPanel jpanel1 = new JPanel(new GridLayout(1, 3));
        JPanel jpanel2 = new JPanel(new GridLayout(1, 2));
        JPanel jpanel3 = new JPanel(new GridLayout(2, 1));
        JPanel jpanel4 = new JPanel(new GridLayout(3, 2));

        jpanel1.add(new JButton("1"));
        jpanel1.add(new JButton("1"));
        jpanel1.add(new JButton("1"));
        jpanel1.add(new JButton("1"));
        jpanel2.add(new JButton("2"));
        jpanel2.add(new JButton("2"));
        jpanel3.add(new JButton("3"));
        jpanel3.add(new JButton("3"));
        jpanel4.add(new JButton("4"));
        jpanel4.add(new JButton("4"));
        jpanel4.add(new JButton("4"));
        jpanel4.add(new JButton("4"));
        jpanel4.add(new JButton("4"));
        jpanel4.add(new JButton("4"));

        container.add(jpanel1);
        container.add(jpanel2);
        container.add(jpanel3);
        container.add(jpanel4);

        setVisible(true);
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

    }
}

JScrollPanel

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("欢迎来学习java");

        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);

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

    }

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

3.5、按钮

  • 图片按钮
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("tt.png");
        Icon icon = new ImageIcon(resource);

        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setToolTipText("图片按钮");

        container.add(jButton);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

    }
}
  • 单选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame{
    public JButtonDemo02() {
        Container container = this.getContentPane();

        URL resource = JButtonDemo01.class.getResource("tt.png");
        Icon icon = new ImageIcon(resource);

        //单选框
        JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");

        //由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton01);
        buttonGroup.add(jRadioButton02);
        buttonGroup.add(jRadioButton03);

        container.add(jRadioButton01,BorderLayout.CENTER);
        container.add(jRadioButton02,BorderLayout.NORTH);
        container.add(jRadioButton03,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

    }
}
  • 复选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo03 extends JFrame {

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

        URL resource = JButtonDemo01.class.getResource("tt.png");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        JCheckBox checkBox03 = new JCheckBox("checkBox03");


        container.add(checkBox01,BorderLayout.CENTER);
        container.add(checkBox02,BorderLayout.NORTH);
        container.add(checkBox03,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

    }
}

3.6、列表

  • 下拉框
import javax.swing.*;
import java.awt.*;

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01() throws HeadlessException {
        Container container = this.getContentPane();
        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("已下架");
        jComboBox.addItem("即将上映");

        container.add(jComboBox);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}
  • 列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class TestComboboxDemo02 extends JFrame{
    public TestComboboxDemo02() throws HeadlessException {
        Container container = this.getContentPane();

        //生产内容
        String[] contents = {"1","2","3"};

        //动态数据
        //Vector content = new Vector();
        JList jList = new JList(contents);

        container.add(jList);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}
  • 应用场景
    • 选择地区,或者一些单个选项
    • 列表,展示信息,一般是动态扩容!

3.7、文本框

  • 文本框
import javax.swing.*;
import java.awt.*;

public class TesttextDemo01 extends JFrame {
    public TesttextDemo01() throws HeadlessException {
        Container container = this.getContentPane();

        JTextField jTextField01 = new JTextField("hello");
        JTextField jTextField02 = new JTextField("world",20);

        container.add(jTextField01,BorderLayout.NORTH);
        container.add(jTextField02,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TesttextDemo01();
    }
}
  • 密码框
import javax.swing.*;
import java.awt.*;

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03() throws HeadlessException {
        Container container = this.getContentPane();

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

        container.add(jPasswordField);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}
  • 文本域
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值