JAVA学习日记每天进步一点点之登陆界面操作、判断账户名和添加下拉框内容代码

public  MyPasswordFrame() {
	 DramIcon icon =new DramIcon(50,50);
	 JLabel jl1=new JLabel("如何打开我的心?",icon,JLabel.CENTER);
	 JLabel jl2=new JLabel("                       账号:");
	 JLabel jl3=new JLabel("                       密码:");
	 JTextField jt1=new JTextField(" ",20);
	 JPasswordField jt2=new JPasswordField("",20);
	 JPanel jp1=new JPanel(new GridLayout(1,2,4,4));
	 JPanel jp2=new JPanel(new GridLayout(1,2,4,4));
	 jp1.add(jl2);jp1.add(jt1);
	 jp2.add(jl3);jp2.add(jt2);
	 JButton jb1=new JButton("提交");
	 JButton jb2=new JButton("重置");
	 JButton jb3=new JButton("点我打开提示");
	 jb1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(jt1.getText().trim().length()==0||new String(jt2.getPassword()).trim().length()==0){
					JOptionPane.showMessageDialog(null, "请输入账号和密码");
					return;
				}
				if(jt1.getText().trim().equals("zhangkeran")&&new String(jt2.getPassword()).trim().equals("20180910")){
					JOptionPane.showMessageDialog(null, "你好我是xx的心!请问想问的问题!");
				}
				else{
					JOptionPane.showMessageDialog(null, "账号或密码错误");
				}
			}
		});
	 jb2.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			jt1.setText("");
			jt2.setText("");
		}
	});
	 jb3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null,"能拔出紫青宝剑的那个人和江湖初相见的那一天");
			}
		});
	 JPanel jp3=new JPanel(new GridLayout(1,3,30,30));

实现问题:开发一个登陆窗体,包括用户名、密码以及提交按钮和重置按钮,当用户输入正确的账户名和密码,弹出登录成功对话框。
主要代码部分如上,功能包括:所有按钮的显示、按钮监听、对账户和密码的判断等。

         DramIcon icon=new DramIcon(50,50);
		JButton jb=new JButton("添加内容", icon);
		JComboBox jc=new JComboBox();
		jb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jc.setModel(new MyComboBox());
			}
		});
		public class MyComboBox extends AbstractListModel implements ComboBoxModel {
	String selecteditem=null;
	String[]test= {"钱包卡","学生卡","老人卡"};
	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return test.length;
	}
	@Override
	public String getElementAt(int index) {
		// TODO Auto-generated method stub
		return test[index];
	}
	@Override
	public void setSelectedItem(Object anItem) {
		// TODO Auto-generated method stub
		selecteditem=(String)anItem;
	}
	@Override
	public Object getSelectedItem() {
		// TODO Auto-generated method stub
		return selecteditem;
	}

实现问题:创建合适的窗体,并在窗体中设置下拉列表框, 初始状态没有内容,创建按钮,在点击按钮时下拉框出现相应的内容。

你可能感兴趣的:(JAVA学习日记每天进步一点点之登陆界面操作、判断账户名和添加下拉框内容代码)