📄 actionexam.java
字号:
package ch8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionExam extends JFrame implements ActionListener
{
String name="张台山";
String pass="zhang790123";
JTextField tName=new JTextField();
JPasswordField tPass=new JPasswordField();
JButton button1=new JButton("重置");
JButton button2=new JButton("提交");
public ActionExam() //构造方法
{
setTitle("用户登录");
setSize(200,150);
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(3,2));//设置组件的摆放布局为3行2列
contentPane.add(new Label("用户名")); //将标签摆放到窗口上
contentPane.add(tName); //将tName摆放到窗口上
contentPane.add(new Label("密码")); //将标签摆放到窗口上
contentPane.add(tPass); //将tPass摆放到窗口上
contentPane.add(button1);
contentPane.add(button2);
button1.addActionListener(this); //委派监听对象监听按钮事件
button2.addActionListener(this); //委派监听对象监听按钮事件
tPass.setEchoChar('*');
setVisible(true); //设置框架窗口是可见的
this.setDefaultCloseOperation(3);
}
public void actionPerformed(ActionEvent e) //实现ActionListeren接口的方法
{
Object obj=e.getSource();
if(obj==button1)
{
tName.setText("");
tPass.setText("");
tName.requestFocus(true);
}
else if(obj==button2)
{
if(!(name.equals(tName.getText().trim())))
{
JOptionPane.showMessageDialog(null,"用户名错误!请校核!!!");
tName.requestFocus(true);
return;
}
if(!(pass.equals(new String(tPass.getPassword()))))
{
JOptionPane.showMessageDialog(null,"密码错误!请重新输入!!!");
tPass.setText("");
tPass.requestFocus(true);
return;
}
JOptionPane.showMessageDialog(null,"ok!登录成功!!!");
System.exit(0);
}
}
public static void main(String args[])
{
new ActionExam();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -