?? 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 + -