代码如下:
问题我备注
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class My extends JFrame implements ActionListener{
JLabel a4,a5,a6,a8;
JTextField a3;
JButton a1,a2;
JPasswordField a7;
My(){
a5=new JLabel("用户名");
a3=new JTextField(6);
a3.addActionListener(this);
a6=new JLabel("密码");
a7=new JPasswordField(6);
a7.addActionListener(this);
a1=new JButton("登录");
a1.addActionListener(this);
a2=new JButton("取消");
a2.addActionListener(this);
a8=new JLabel("欢迎进入信息录入系统");
a4=new JLabel(new ImageIcon("a.jpg"));
a8.setFont(new Font("楷体",Font.BOLD,28));
this.setLayout(new FlowLayout());
this.add(a8);this.add(a4);
this.add(a5);this.add(a3);
this.add(a6);this.add(a7);
this.add(a1);this.add(a2);
add(a8,BorderLayout.SOUTH);
add(a4,BorderLayout.CENTER);
add(a5,BorderLayout.NORTH);add(a3,BorderLayout.NORTH);
add(a6,BorderLayout.NORTH);add(a7,BorderLayout.NORTH);
add(a1,BorderLayout.NORTH);add(a2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(400,340);
this.setLocationRelativeTo(null);
this.setTitle("登陆界面");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getRootPane().setDefaultButton(a1);
}
private void registerKeyListener(){
a7.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent event){
if (KeyEvent.getKeyText(event.getKeyCode()).compareToIgnoreCase("Enter")==0){
a1.doClick();
}
}
});//我想键盘按回车实现登陆不用点击登陆按钮
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==a1) {
String s=a3.getText();
if(s.trim().equals("")) {
JOptionPane.showMessageDialog(null, "输入内容不能为空","不合法提示",JOptionPane.INFORMATION_MESSAGE);
}//内容为空的时候他会有俩个弹窗麻烦让他内容为空的时候只弹出一个
}
if(ae.getSource()==a2){
}
if(ae.getSource()==a1) {
String q=a3.getText();
String w= String.valueOf(a7.getPassword());
if((q.equals("123")) && (w.equals("123"))) {
JOptionPane.showMessageDialog(null, "登陆成功","确认登陆",JOptionPane.INFORMATION_MESSAGE);
this.dispose();
new My1();
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码错误","不合法提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
public class Test1 {
public static void main(String[] args) {
new My();
}
}