package zuoye0517;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class DENGLU extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;
private JMenuBar mb;
private JMenu col,ext;
private JMenuItem gre,yel,blu,clo;
public DENGLU(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
public void userInit(){
this.setSize(500,320);
this.setTitle("简易变色登陆界面");
mb=new JMenuBar();
col=new JMenu("颜色选择");
ext=new JMenu("退出");
gre=new JMenuItem("绿色");
yel=new JMenuItem("黄色");
blu=new JMenuItem("蓝色");
clo=new JMenuItem("退出系统");
gre.addActionListener(new Handler1());
yel.addActionListener(new Handler1());
blu.addActionListener(new Handler1());
clo.addActionListener(new Handler1());
mb.add(col);
mb.add(ext);
col.add(gre);
col.add(yel);
col.add(blu);
ext.add(clo);
setJMenuBar(mb);
this.pnluser.setLayout(null);
this.pnluser.setBackground(null);
this.lbluserLogIn.setText("用户登录");
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));
this.lbluserLogIn.setForeground(Color.RED);
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e); } } );
this.btnReset.setBounds(155,120,60,20);
this.pnluser.add(lbluserLogIn);
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);
this.setVisible(true);
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}
else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}
else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
return;
}
}
public class Handler1 implements ActionListener{
public void actionPerformed(ActionEvent e){
JMenuItem mi=(JMenuItem) e.getSource();
if(mi==gre) pnluser.setBackground(Color.green);
if(mi==yel) pnluser.setBackground(Color.yellow);
if(mi==blu) pnluser.setBackground(Color.blue);
if(mi==clo) System.exit(0);
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new DENGLU();
}
}
将两个单独的程序写成一个程序,可能麻烦一点 不过简单易懂。首先是编辑选项栏,定义变量,然后是一个简单登录界面的编写, 需要注意的是对部分区域的监听