1. 编写一程序,运行效果如图所示(或单击T1.jar查看效果),文件保存为T1.java。 要求: (1)在界面上点击【登录】按钮,弹出一对话框,输出的文本框中的用户名和密码信息; (2)点击界面【重置】按钮,将界面的
时间: 2024-05-25 14:15:34 浏览: 95
文本框清空。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class T1 extends JFrame implements ActionListener{
private JTextField userName;
private JPasswordField password;
private JButton login, reset;
public T1(){
super("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,150);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel(new GridLayout(3,2,5,5));
JLabel label1 = new JLabel("用户名:");
userName = new JTextField(10);
JLabel label2 = new JLabel("密码:");
password = new JPasswordField(10);
panel.add(label1);
panel.add(userName);
panel.add(label2);
panel.add(password);
login = new JButton("登录");
login.addActionListener(this);
reset = new JButton("重置");
reset.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(login);
buttonPanel.add(reset);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == login){
String user = userName.getText();
String pass = new String(password.getPassword());
JOptionPane.showMessageDialog(null, "用户名: " + user + "\n密码: " + pass);
}
else if(e.getSource() == reset){
userName.setText("");
password.setText("");
}
}
public static void main(String[] args){
T1 login = new T1();
login.setVisible(true);
}
}
阅读全文