编写一个登录窗口, 密码输入采用密码框,输入密码显示为“*”,当输入用户名admin密码123的时候点击确定跳转到学生信息录入窗口界面,其他输入显示用户名密码错误。点击取消退出运行。

//这部分是登陆界面
package Test7_1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Register extends JFrame
{
    public Register()//构造函数,类被创建时调用
    {
        setBounds(300,300,300,300);//设置窗体大小
        setVisible(true);
        setTitle("登录窗口");
        Container c=getContentPane();//获取容器以装载组件
        JLabel id1=new JLabel("用户名");
        JLabel key1=new JLabel("密 码");
        JTextField id2=new JTextField(15);
        JPasswordField key2=new JPasswordField(15);
        key2.setEchoChar('*');//将输入的密码以*显示
        JButton confirm=new JButton("确认");
        JButton cancle=new JButton("取消");
        //将组件添加到容器内
        c.add(id1);
        c.add(key1);
        c.add(id2);
        c.add(key2);
        c.add(confirm);
        c.add(cancle);
        //组件位置
        id1.setBounds(10,40,50,20);
        key1.setBounds(10,80,50,20);
        id2.setBounds(60,40,200,20);
        key2.setBounds(60,80,200,20);
        confirm.setBounds(30,120,60,30);
        cancle.setBounds(190,120,60,30);
        //对各个按钮添加监听事件
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(id2.getText().trim().equals("admin")&&new String(key2.getPassword()).equals("1234"))//因为类型不兼容问题,要将key2强转为string类型,trim()清除字符串中的空格字符
                {
                    new Enter();//进入信息录入界面
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"用户名密码错误,请重新输入");
                    id2.setText("");
                    key2.setText("");
                }
            }
        });
        cancle.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);//退出程序
            }
        });
    }

    public static void main(String args[])
    {
        new Register();
    }
}

//这部分是账号密码正确是进入的信息录入界面
package Test7_1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Enter extends JFrame {
   //private TextArea resume
    Enter() {
        setBounds(100,100,500,400);
        setTitle("学生信息录入窗口");
        Container c = getContentPane();
        setVisible(true);
        setAlwaysOnTop(true);
        c.setLayout(null);
        JLabel id = new JLabel("学号");
        JLabel name = new JLabel("姓名");
        //单选按钮
        JLabel sex = new JLabel("性别");
        JRadioButton mm = new JRadioButton("男");
        JRadioButton fm = new JRadioButton("女");
        //
        JLabel date = new JLabel("出生日期");
        //复选框
        JLabel member = new JLabel("团员");
        JCheckBox member1 = new JCheckBox();
        //
        JLabel profession = new JLabel("专业");
        //组合框
        String str[] = {"计算机", "自动化", "电子信息"};
        JComboBox profession1 = new JComboBox(str);
        //
        JLabel address = new JLabel("家庭地址");
        JLabel resume = new JLabel("简历");
        JTextArea resume1=new JTextArea(20,30);
        JButton save = new JButton("保存");
        JButton cancel = new JButton("取消");
        JTextField id1 = new JTextField(15);
        JTextField name1 = new JTextField(15);
        JTextField date1 = new JTextField(15);
        JTextField address1 = new JTextField(15);
        c.add(id);
        c.add(id1);
        c.add(name);
        c.add(name1);
        c.add(sex);
        c.add(mm);
        c.add(fm);
        c.add(member);
        c.add(member1);
        c.add(profession);
        c.add(profession1);
        c.add(date);
        c.add(date1);
        c.add(address);
        c.add(address1);
        c.add(resume);
        c.add(resume1);
        c.add(save);
        c.add(cancel);
        //设置各个组件大小
        id.setBounds(20, 40, 50, 18);
        id1.setBounds(70, 40, 80, 18);
        name.setBounds(20, 60, 50, 18);
        name1.setBounds(70, 60, 80, 18);
        sex.setBounds(20, 80, 50, 18);
        mm.setBounds(80, 80, 50, 18);
        fm.setBounds(140, 80, 50, 18);
        member.setBounds(20, 100, 50, 18);
        member1.setBounds(70, 100, 20, 18);
        profession.setBounds(20, 120, 50, 18);
        profession1.setBounds(80, 120, 100, 18);
        date.setBounds(20, 140, 60, 18);
        date1.setBounds(90,140,80,18);
        address.setBounds(20,160,80,18);
        address1.setBounds(110,160,150,18);
        resume.setBounds(20,180,50,18);
        resume1.setBounds(80,180,250,120);
        save.setBounds(20,320,80,18);
        cancel.setBounds(110,320,80,18);
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resume1.setText("");
                String id=id1.getText();
                resume1.append("学生学号为:"+id+"\n");
                String name=name1.getText();
                resume1.append("学生姓名为:"+name+"\n");
                if(mm.isSelected())
                    resume1.append("学生性别为男\n");
                else
                    resume1.append("学生性别为女\n");
                String date=date1.getText();
                resume1.append("学生姓名为:"+date+"\n");
                if(member1.isSelected())
                {
                    resume1.append("是否是团员:是\n");
                }
                else
                    resume1.append("是否是团员:否\n");
                String profession=profession1.getSelectedItem().toString();
                resume1.append("专业为:"+profession+"\n");
                String address=address1.getText();
                resume1.append("家庭地址:"+address+"\n");
            }
        });
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }
}

笨方法仅供参考
编写一个登录窗口, 密码输入采用密码框,输入密码显示为“*”,当输入用户名admin密码123的时候点击确定跳转到学生信息录入窗口界面,其他输入显示用户名密码错误。点击取消退出运行。_第1张图片编写一个登录窗口, 密码输入采用密码框,输入密码显示为“*”,当输入用户名admin密码123的时候点击确定跳转到学生信息录入窗口界面,其他输入显示用户名密码错误。点击取消退出运行。_第2张图片

你可能感兴趣的:(Java)