换种写法:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class test2 extends JFrame implements ActionListener { private JLabel titleLabel, usernameLabel, passwordLabel, infoLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton, cancelButton; public test2(String title) { super(title); Container contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 2, 10, 10)); titleLabel = new JLabel("用户登录"); titleLabel.setFont(new Font("仿宋", Font.BOLD, 20)); titleLabel.setHorizontalAlignment(JLabel.CENTER); usernameLabel = new JLabel("用户名:"); passwordLabel = new JLabel("密码:"); usernameField = new JTextField(10); passwordField = new JPasswordField(10); loginButton = new JButton("登录"); cancelButton = new JButton("取消"); loginButton.addActionListener(this); cancelButton.addActionListener(this); infoLabel = new JLabel(); panel.add(usernameLabel); panel.add(usernameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(loginButton); panel.add(cancelButton); panel.add(new JLabel()); panel.add(infoLabel); contentPane.add(titleLabel, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); this.setSize(300, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText().trim(); String password = new String(passwordField.getPassword()).trim(); if (username.isEmpty() || password.isEmpty()) { infoLabel.setText("请输入用户名和密码"); } else { infoLabel.setText("用户名: " + username + " 密码: " + password); } } else if (e.getSource() == cancelButton) { usernameField.setText(""); passwordField.setText(""); infoLabel.setText("用户登录"); } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { new test2("登录界面"); }); } }
时间: 2024-03-08 08:45:16 浏览: 31
这段代码是一个简单的Java GUI程序,实现了一个用户登录界面。程序继承了JFrame类,并实现了ActionListener接口,用于监听登录按钮和取消按钮的事件。程序使用了Swing组件库,包括JLabel、JTextField、JPasswordField、JButton等组件,用于显示和输入用户名和密码。当用户点击登录按钮时,程序会获取输入框中的用户名和密码,并判断是否为空,如果为空则提示用户输入,否则显示输入的用户名和密码。当用户点击取消按钮时,程序会清空输入框,并将提示信息恢复为默认值。程序的入口方法为main方法,通过SwingUtilities类的invokeLater方法在事件分发线程中启动程序。
相关问题
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
这是Java中用于创建图形用户界面(GUI)的相关类和接口。其中:
- javax.swing.*:提供了创建Swing组件的类和接口,如JFrame、JButton等。
- java.awt.*:提供了创建AWT组件的类和接口,如Frame、Button等。
- java.awt.event.ActionEvent:表示动作事件,由组件生成并传递给侦听器的事件对象。
- java.awt.event.ActionListener:动作事件侦听器接口,用于处理动作事件的回调方法。
解释以下这段代码import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
这段代码导入了 javax.swing 和 java.awt 两个包,这两个包都是 Java 中用于 GUI 编程的常用包。其中 javax.swing 包提供了一些用于创建图形用户界面的类和接口,而 java.awt 包则提供了一些用于创建基本图形界面组件的类和接口。此外,这段代码还导入了 ActionEvent 和 ActionListener 两个事件处理类,用于处理用户界面中的事件。