Java——GUI

目录

AWT内容

Frame框架

创建窗口Frame

框架属性设置

​ 

封装框架

Panel面板

创建Panel

 Panel基础属性设置

将Panel放入(添加至) Frame中

监听事件,监听窗口关闭事件 System.exit(0)适配器模式:Adapter

布局管理器

按钮 Button

流式布局

东西南北中Border(边框、边界)

表格布局Grid

事件监听

添加监听器 

 两个按钮实现一个监听事件

输入框事件监听

创建文本框

 获取文本框输入消息

鼠标监听

键盘监听

窗口监听

Swing

JFrame

init()初始化

JDialog弹窗

标签JLabel

初始化

图标Icon

将自定义图片作为图标

面板JPanel

 JScroll(滚动条)

按钮JButton

单选框

多选框

下拉框JComboBox

列表框

文本框

密码框


AWT内容

Frame框架

创建窗口Frame

 

源码
public Frame(String title) throws HeadlessException {
    init(title, null);
}
       //Frame
       Frame frame =  new Frame("我的第一个Java图像界面窗口");

效果类似这样

框架属性设置

 

       //此时窗口在内存中,不可见
       //需要设置可见性
        frame.setVisible(true);

        //设置窗口大小 width宽 high高
        frame.setSize(400,400);

        //设置颜色 Color
        //frame.setBackground(Color.CYAN);
        frame.setBackground(new Color(114, 165, 206));

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

        //设置大小固定
        frame.setResizable(false);  //false窗口不可改变

Color源码

public Color(int r, int g, int b) {
    this(r, g, b, 255);
}        //r - red; g - green; b - blue 

 

idea也可以智能变色

(0,0)点初始位置在屏幕左上角 

 

封装框架

package lit.jxxy.rg1508.java.gui;

import java.awt.*;

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

        //展示多个窗口 new
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.WHITE);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.BLACK);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.YELLOW);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.BLUE);

    }
}

//封装
class MyFrame extends Frame{

    static int id = 0;  //可能存在多个窗口,需要一个计数器;
    public MyFrame(){

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


    }

}

 效果:


Panel面板

创建Panel

源码:

public Panel() {
    this(new FlowLayout());
}        //流布局
        Frame frame = new Frame("第二个Java图像窗口");
        //布局
        Panel panel = new Panel();

 Panel基础属性设置

(panel设置坐标,相对于frame)

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(139, 207, 231));
        //设置布局
        frame.setLayout(null);

将Panel放入(添加至) Frame中

frame.add(panel);

add中应放入Component(部件)类型数据,由源码只可以放入Panel

public class Panel extends Container
public class Container extends Component

效果: 

测试代码

 

package lit.jxxy.rg1508.java.gui;

import java.awt.*;

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

        Frame frame = new Frame("第二个Java图像窗口");
        //布局
        Panel panel = new Panel();

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

        //坐标
        frame.setBounds(300,300,500,500);
        //颜色
        frame.setBackground(new Color(255, 232, 123));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(139, 207, 231));

        //frame.add(panel) 把布局放入窗口中
        frame.add(panel);

        //可见
        frame.setVisible(true);

    }
}

监听事件,监听窗口关闭事件 System.exit(0)
适配器模式:Adapter

源码
public abstract class WindowAdapter
    implements WindowListener, WindowStateListener, WindowFocusListener
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭的适合需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });

布局管理器

按钮 Button

源码

public class Button extends Component
        //组件-按钮button
        Button button1 = new Button("Button1");
        Button button2 = new Button("Button2");
        Button button3 = new Button("Button3");

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

 效果:(默认剧中)

靠左

        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

效果:

 


流式布局

源码

/**
 * This value indicates that each row of components
 * should be left-justified.
 */
public static final int LEFT        = 0;

/**
 * This value indicates that each row of components
 * should be centered.
 */
public static final int CENTER      = 1;

/**
 * This value indicates that each row of components
 * should be right-justified.
 */
public static final int RIGHT       = 2;
public FlowLayout() {
    this(CENTER, 5, 5);
}

东西南北中Border(边框、边界)

源码

public class BorderLayout implements LayoutManager2,
/**
 * Constant to specify components location to be the
 *      north portion of the border layout.
 * @serial
 * @see #getChild(String, boolean)
 * @see #addLayoutComponent
 * @see #getLayoutAlignmentX
 * @see #getLayoutAlignmentY
 * @see #removeLayoutComponent
 */
    Component north;
 /**
 * Constant to specify components location to be the
 *      west portion of the border layout.
 * @serial
 * @see #getChild(String, boolean)
 * @see #addLayoutComponent
 * @see #getLayoutAlignmentX
 * @see #getLayoutAlignmentY
 * @see #removeLayoutComponent
 */
    Component west;
/**
 * Constant to specify components location to be the
 *      east portion of the border layout.
 * @serial
 * @see #getChild(String, boolean)
 * @see #addLayoutComponent
 * @see #getLayoutAlignmentX
 * @see #getLayoutAlignmentY
 * @see #removeLayoutComponent
 */
    Component east;
/**
 * Constant to specify components location to be the
 *      south portion of the border layout.
 * @serial
 * @see #getChild(String, boolean)
 * @see #addLayoutComponent
 * @see #getLayoutAlignmentX
 * @see #getLayoutAlignmentY
 * @see #removeLayoutComponent
 */
Component south;
/**
 * Constant to specify components location to be the
 *      center portion of the border layout.
 * @serial
 * @see #getChild(String, boolean)
 * @see #addLayoutComponent
 * @see #getLayoutAlignmentX
 * @see #getLayoutAlignmentY
 * @see #removeLayoutComponent
 */
    Component center;

 

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

效果: 

表格布局Grid

源码

public GridLayout() {
    this(1, 0, 0, 0);
}
        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);

效果:


事件监听

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

源码:

public synchronized void addActionListener(ActionListener l) {
public interface ActionListener extends EventListener {

        按下按钮,触发一些事件
        因为addActionListener需要一个ActionListener,所以需要构造一个 ActionListener 

添加监听器 


        MyActionLinstener myActionLinstener = new MyActionLinstener();
        btn.addActionListener(myActionLinstener);

//事件监听
class MyActionLinstener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button is being pressed!");
    }
}

整体代码:

package lit.jxxy.rg1508.java.gui;

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 GUITest07 {
    public static void main(String[] args) {

        Frame frame = new Frame("GUITest07");
        Button btn = new Button("btn");

        //按下按钮,触发一些事件
        //因为addActionListener需要一个ActionListener,所以需要构造一个 ActionListener
        MyActionLinstener myActionLinstener = new MyActionLinstener();
        btn.addActionListener(myActionLinstener);

        frame.setSize(200,200);
        frame.add(btn,BorderLayout.CENTER);

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

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

//事件监听
class MyActionLinstener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button is being pressed!");
    }
}

 

效果:

 

 两个按钮实现一个监听事件

package lit.jxxy.rg1508.java.gui;

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

public class GUITest08 {
    public static void main(String[] args) {
        Frame frame = new Frame("Start - Stop");
        Button button1 = new Button("Start");
        Button button2 = new Button("Stop");

        button2.setActionCommand("btn-2-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());

    }
}

效果: 

输入框事件监听

创建文本框

(已经封装过)

        TextField textField = new TextField();
        add(textField);
class MyAnotherFrame extends Frame{
    public MyAnotherFrame() {
        TextField textField = new TextField();
        add(textField);

        //监听这个文本框输入的文字
        MyAnotherActionListener myAnotherActionListener = new MyAnotherActionListener();
        //按下enter就会触发
        textField.addActionListener(myAnotherActionListener);

        pack();
        setVisible(true);

    }
}

 

 获取文本框输入消息

源码

public Object getSource() {
    return source;
}
class MyAnotherActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();  //获得一些资源,返回一个对象
        System.out.println(field.getText());//获取输入框中的文本

    }
}

效果:

 //设置替换编码 

textField.setEchoChar('*');

效果(获取密码) :

 


鼠标监听

模拟画图工具

关系:

package lit.jxxy.rg1508.java.gui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class GUITest10 {
    public static void main(String[] args) {
        MyFrame03 drawing = new MyFrame03("drawing");
    }
}

class MyFrame03 extends Frame{
    //画画需要画笔,需要监听鼠标当前位置,需要集合来存储这个点
    ArrayList points = null;

    public MyFrame03(String title){
        super(title);
        setBounds(200,200,400,400);
        //存鼠标点击的点
        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 addPoints(Point point){
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter{
        //鼠标 按下,弹起,按住不放

        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame03 frame = (MyFrame03) e.getSource();
            //这时我们点击的时候就会在界面上显示一个 点.
            //这个点就是鼠标的点
            frame.addPoints(new Point(e.getX(),e.getY()));

            //每次点击鼠标都要重新画一遍
            frame.repaint();    //flush

        }

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
        }
    }
}

 

效果

 

键盘监听

源码

public static final int VK_ENTER          = '\n';
public static final int VK_BACK_SPACE     = '\b';
public static final int VK_TAB            = '\t';
public static final int VK_CANCEL         = 0x03;
public static final int VK_CLEAR          = 0x0C;
public static final int VK_SHIFT          = 0x10;
public static final int VK_CONTROL        = 0x11;
public static final int VK_ALT            = 0x12;
public static final int VK_PAUSE          = 0x13;
public static final int VK_CAPS_LOCK      = 0x14;
public static final int VK_ESCAPE         = 0x1B;
public static final int VK_SPACE          = 0x20;
public static final int VK_PAGE_UP        = 0x21;
public static final int VK_PAGE_DOWN      = 0x22;
public static final int VK_END            = 0x23;
public static final int VK_HOME           = 0x24;

……

package lit.jxxy.rg1508.java.gui;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GUITest12 {
    public static void main(String[] args) {
        KeyFrame keyFrame = new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        //super();
        setBounds(400,400,300,400);
        setVisible(true);

        //匿名内部类
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
            }

            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
            }
        });
    }
}

 

效果:

 

窗口监听

package lit.jxxy.rg1508.java.gui;

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

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

class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.GREEN);
        setBounds(100,100,400,300);
        setVisible(true);
        //addWindowListener(new MyWindowListener());

        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent e) {
                        System.out.println("WindowOpened");
                    }

                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("已关闭掉窗口");
                    }

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

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("正在关闭");
                    }
                }
        );
    }

    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);  // 隐藏窗口,通过按钮隐藏窗口
            System.exit(0); //正常退出
        }
    }
}

Swing

JFrame

源码

public class JFrame  extends Frame

init()初始化

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

        //设置文字 JLable
        JLabel label = new JLabel("欢迎来到英雄联盟");
        frame.add(label);

        //让文本居中
        label.setHorizontalAlignment(SwingConstants.CENTER);


        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

建立窗口

    public static void main(String[] args) {
        //建立一个窗口
        new JFrameTest01().init();
    }

JDialog弹窗

JDialog用来被弹出,默认有写过关闭窗口功能

package lit.jxxy.rg1508.java.gui.swing;

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

//主窗口
public class JDialogTest01 extends JFrame {

    public JDialogTest01(){
        this.setVisible(true);
        this.setBounds(200,200,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西,容器
        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) {
                //弹窗
            MyDialog myDialog = new MyDialog();
            }
        });

        container.add(button);

    }

    public static void main(String[] args) {
        JDialogTest01 jDialogTest01 = new JDialogTest01();
    }
}

//弹窗窗口
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 默认已拥有

        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);

        contentPane.add(new Label("欢迎来到祖安"));
    }
}

标签要放到容器中

效果

标签JLabel

初始化

new JLabel("xxx");

图标Icon

图标是一个接口,需要实现类,frame继承,要实现内部的方法 

    @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 JLabel(String text, Icon icon, int horizontalAlignment)

三个参数(名称,图标,位置) 

    public void init(){
        ICONTest iconTest = new ICONTest(15, 15);//(图标)
        //图标放在标签、按钮上
        new JLabel("icon",iconTest,SwingConstants.CENTER);
    }

标签放在图标中间位置

package lit.jxxy.rg1508.java.gui.swing;

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

//图标是一个接口,需要实现类,frame继承
public class ICONTest extends JFrame implements Icon {

    private int width;
    private int height;

    public ICONTest(){
    }
    public ICONTest(int width, int height){
        this.height = height;
        this.width = width;
    }

    public void init(){
        ICONTest iconTest = new ICONTest(15, 15);
        //图标放在标签、按钮上
        JLabel label = new JLabel("icon", iconTest, SwingConstants.CENTER);

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

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

效果:

将自定义图片作为图标

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/6 10:46
 */
public class ImageIconTest extends JFrame {

    public ImageIconTest() {
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconTest.class.getResource("B20041508叶子森.jpg");

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

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

面板JPanel

实现

    public JPanelTest(){
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//后面参数意思是间距

        JPanel panel1 = new JPanel(new GridLayout(1,3));

        container.add(panel1);

        panel1.add(new Button("1"));
        panel1.add(new Button("1"));
        panel1.add(new Button("1"));

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

效果:

 JScroll(滚动条)

实现

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/6 11:10
 */
public class JScrollTest extends JFrame {

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

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("欢迎来到英雄联盟");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


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

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

效果


按钮JButton

 和Button差不多

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/6 11:20
 */
public class JButtonTest01 extends JFrame {

    protected JButtonTest01(){
        //获得容器
        Container container = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonTest01.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 JButtonTest01();
    }
}

效果

单选框

        //单选框
        JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton03 = new JRadioButton("JRadioButton03");

        //由于单选框只能选一个,要分组,一个组中只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);

        container.add(radioButton01,BorderLayout.CENTER);
        container.add(radioButton02,BorderLayout.NORTH);
        container.add(radioButton03,BorderLayout.SOUTH);

效果

 

多选框

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/6 17:41
 */
public class JButtonTest02 extends JFrame{

    protected JButtonTest02(){
        //获得容器
        Container container = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonTest02.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox01 = new JCheckBox("记住密码");
        JCheckBox checkBox02 = new JCheckBox("自动登录");

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

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

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

 

效果

下拉框JComboBox

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/7 9:59
 */
public class ComboboxTest extends JFrame {

    //下拉框
    public ComboboxTest(){

        Container container = getContentPane();

        JComboBox status = new JComboBox();

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

        container.add(status);

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

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

效果

列表框

展示信息,一般是动态扩容 

package lit.jxxy.rg1508.java.gui.swing;

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

/**
 * @author 华为
 * @version 1.0
 * @description: TODO
 * @date 2022/5/7 10:09
 */
public class ComboboxAnotherTest extends JFrame{

    public ComboboxAnotherTest(){

        Container container = getContentPane();

        //生成列表内容
        String[] contents = {"1","2","3"};
        //列表中需要放入内容
        JList list = new JList(contents);

        container.add(list);

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

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

也可这样

        Vector contents = new Vector();
        //列表中需要放入内容
        JList list = new JList(contents);

        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");

效果  

 


文本框

        Container container = getContentPane();

        JTextField textField01 = new JTextField("hello");
        JTextField textField02 = new JTextField("world",20);

        container.add(textField01,BorderLayout.NORTH);
        container.add(textField02,BorderLayout.SOUTH);

效果

 

密码框

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

        container.add(passwordField);

效果

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值