Java代码的简单登录界面
package loginwindow;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.xml.transform.Source;
public class MyWindow<JButtonListener> extends JFrame{
Button jbLogin,jbExit;//登录,退出按钮
JLabel a1,a2,a3;//标签
JTextField b;//文本框
JPasswordField pw;//密码框
public MyWindow(){
Container con=super.getContentPane();//顶级容器
con.setLayout(null);//空布局
a1=new JLabel("登录");
a1.setBounds(225,10,100,100);
a2=new JLabel("用户名:");
a2.setBounds(100,85,100,60);//设置组件的位置,大小(x,y,width, height)
b=new JTextField(20);
b.setBounds(180,100,200,30);
b.setBackground(Color.CYAN);//设置颜色
a3=new JLabel("密 码:");
a3.setBounds(100,145,100,60);
pw=new JPasswordField(20);
pw.setBounds(180,160,200,30);
pw.setBackground(Color.CYAN);
jbLogin=new JButton("登录");
jbLogin.setBounds(200,250,60,40);
jbLogin.setBackground(Color.CYAN);
jbExit=new JButton("退出");
jbExit.setBounds(300,250,60,40);
jbExit.setBackground(Color.CYAN);
con.add(a1);
con.add(a2);//把组件添加到界面上
con.add(b);
con.add(a3);
con.add(pw);
con.add(jbLogin);
con.add(jbExit);
super.setBackground(Color.BLUE);
super.setSize(500, 400);//设置窗口大小
super.setVisible(true);//设置窗口为可见的
super.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭按钮
ButtonListener lis=new ButtonListener();
jbExit.addActionListener( lis);//注册监听事件
jbLogin.addActionListener(lis);
}
//main函数
public static void main(String [] args){
MyWindow mw=new MyWindow();
}
//事件监听器接口
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source instanceof JButton ){
JButton jb=(JButton)source;//按钮
String txt=jb.getText();
String userName=b.getText();//用户名
String password=pw.getText();//密码
if(txt.equals("登录")){
if(userName.equals("123")&&password.equals("123456")){
JOptionPane.showMessageDialog(null, "登陆成功!!");//提示
}else{
JOptionPane.showMessageDialog(null, "用户名或密码错误!!");
}
}else if(txt.equals("退出")){
System.exit(0);
}
}
}
}
}