java 的简单应用--设计一个简单的变色登录界面

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();
	}
 
}

将两个单独的程序写成一个程序,可能麻烦一点  不过简单易懂。首先是编辑选项栏,定义变量,然后是一个简单登录界面的编写, 需要注意的是对部分区域的监听
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值