图形用户界面GUI编程:Swing JFrame窗体 、弹窗、标签 、面板 、按钮、列表、文本框、密码框、文本域

GUI编程学习笔记-3,根据目录食用~

1、JFrame窗体

JFrame窗口关闭:jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

跟Frame差不多,基础例子:

在这里插入图片描述
文本标签位置,例如居中,方法:
法一:

JLabel label = new JLabel("hello jlabel", SwingConstants.CENTER);

法二:

 JLabel label = new JLabel("hello jlabel";
 label.setHorizontalAlignment(SwingConstants.CENTER);

2、JDialog弹窗

默认有关闭事件

// 主窗口
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);

        // 点击按钮弹出一个弹窗(监听事件)
        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);
        // 这里重复了,会报错(弹窗是绝对布局)
	    // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  

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

        container.add(new Label("java。。。。。"));
    }
}

在这里插入图片描述

3、标签

Icon标签

在这里插入图片描述

ImageIcon标签

在这里插入图片描述

4、面板 JPanel

		Container container = this.getContentPane();  // 获得一个容器
		container.setLayout(new GridLayout(2,1,10,10));    
		
		JPanel panel1 = new JPanel(new GridLayout(1,3));  
		
		panel1.add(new JButton("1"));
		panel1.add(new JButton("1"));
		panel1.add(new JButton("1"));
		
		container.add(panel1);

在这里插入图片描述

文本域面板 JScrollPanel (带滚动条)

		Container container = this.getContentPane();
		
		// 文本域
		JTextArea jTextArea = new JTextArea(20, 50);
		jTextArea.setText("hello 欢迎回来");
		
		// Scroll面板
		JScrollPane scrollPane = new JScrollPane(jTextArea);
		container.add(scrollPane);

在这里插入图片描述

5、按钮

图片按钮 ImageIcon

		Container container = this.getContentPane();
		
		// 将一个图片编程图标
		URL url = JButtonDemo.class.getResource("./6.png");// 获取图片地址
		ImageIcon icon = new ImageIcon(url);
		
		// 将该图标放在按钮上
		JButton button = new JButton();
		button.setIcon(icon);
		button.setToolTipText("图片按钮");

单选按钮 JRadioButton

        // 单选框
        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);

        container.add(jRadioButton1, BorderLayout.NORTH);
        container.add(jRadioButton2, BorderLayout.CENTER);
        container.add(jRadioButton3, BorderLayout.SOUTH);

在这里插入图片描述

复选按钮 JCheckBox

        // 多选框
        JCheckBox checkbox1 = new JCheckBox("checkbox1");
        JCheckBox checkbox2 = new JCheckBox("checkbox2");

        container.add(checkbox1, BorderLayout.NORTH);
        container.add(checkbox2, BorderLayout.SOUTH);

在这里插入图片描述

6、下拉框 JComboBox

        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("男");
        jComboBox.addItem("女");

在这里插入图片描述

7、列表框 JList

列表框一般用于展示信息,动态扩容

内容静态初始化

        Container container = this.getContentPane();

        // 生成列表的内容,静态初始化
        String[] contents = {"苹果", "香蕉", "西瓜"};
        // 列表中需要放入内容
        JList jList = new JList(contents);

        container.add(jList);

内容动态初始化

        Container container = this.getContentPane();

        Vector contents = new Vector();	
        contents.add("香蕉");
        contents.add("西瓜");
        contents.add("苹果");
		
		JList jList = new JList(contents);
		container.add(jList);

8、文本框 JTextField

        Container container = this.getContentPane();

        JTextField jTextField1 = new JTextField("hello 你好");
        JTextField jTextField2 = new JTextField("world 世界",20);

        container.add(jTextField1,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);

在这里插入图片描述

9、密码框 JPasswordField

        Container container = this.getContentPane();

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

        container.add(jPasswordField);

监听事件:

jPasswordField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(jPasswordField.getPassword());
            }
        });

10、文本域 JTextArea

		JTextArea jTextArea = new JTextArea(20, 50);
		jTextArea.setText("hello 欢迎回来");
  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,您可以参考以下代码实现一个简单的用户登录: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoginFrame extends JFrame implements ActionListener { private JLabel userLabel; private JLabel passwordLabel; private JTextField userField; private JPasswordField passwordField; private JButton loginButton; public LoginFrame() { super("用户登录"); this.setSize(300, 200); this.setLocationRelativeTo(null); // 居中显示 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建组件 userLabel = new JLabel("用户名:"); passwordLabel = new JLabel("密 码:"); userField = new JTextField(20); passwordField = new JPasswordField(20); loginButton = new JButton("登录"); // 添加组件 JPanel panel = new JPanel(new GridLayout(3, 1)); panel.add(userLabel); panel.add(userField); panel.add(passwordLabel); panel.add(passwordField); panel.add(loginButton); // 添加事件监听器 loginButton.addActionListener(this); passwordField.addActionListener(this); // 设置内容面板 this.setContentPane(panel); } public void actionPerformed(ActionEvent e) { // 处理登录事件 if (e.getSource() == loginButton || e.getSource() == passwordField) { String username = userField.getText(); String password = new String(passwordField.getPassword()); if (username.equals("admin") && password.equals("123456")) { JOptionPane.showMessageDialog(this, "登录成功!"); this.dispose(); // 关闭 } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } } public static void main(String[] args) { LoginFrame frame = new LoginFrame(); frame.setVisible(true); } } ``` 上述代码中,我们使用了 `JLabel`、`JTextField`、`JPasswordField` 和 `JButton` 组件来构建登录,使用 `GridLayout` 布局将它们排列在一列中,并为 `loginButton` 和 `passwordField` 组件添加了事件监听器,以响应用户的登录操作。当用户点击登录按钮或在密码框中按下回车键时,程序会从文本框中获取用户名和密码,并进行简单的验证操作,如果验证通过,则出登录成功的提示框并关闭,否则出错误提示框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值