Java项目7之学生信息管理界面
程序员文章站
2022-07-13 13:36:43
...
设计一个学生信息管理界面,如图所示。
要求系统的使用要进行身份验证,即在开始显示主界面之前,先显示登录窗口,进行用户名和密码的检验,只有通过验证的才能看到主界面,否则3次机会后程序结束不允许使用该系统。
package 实验七;
/**
* (1)实现了登录后关闭登录窗口,但程序不退出
* (2)错误三次自动退出
* (3)实现了录入信息功能,录入时显示全部人信息
* (4)实现了查询功能,选中单个条件或者多个条件时都能查询
* (5)当录入的信息不全时,有窗口提示
* (6)当点查询时,没有选中条件,有窗口提示
*/
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Management extends JFrame implements ActionListener{
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JLabel jl1=new JLabel("账号");
JLabel jl2=new JLabel("密码");
JTextField jtf=new JTextField(10);//账号:root
JPasswordField jpf=new JPasswordField(10);//密码:root
JButton jb=new JButton("登录");
int erro=0;//记录错误次数
private String password;//密码
public Management() {
//设置布局管理
this.setLayout(new GridLayout(3,1));
//加入组件
jp1.add(jl1);
jp1.add(jtf);
jp2.add(jl2);
jp2.add(jpf);
jp3.add(jb);
jb.addActionListener(this);
//加入到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);
//属性
this.setSize(300,200);
this.setTitle("登录");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
password=String.valueOf(jpf.getPassword());
if(e.getSource().equals(jb)&&erro<3)
{
if(!jtf.getText().equals("root")||!password.equals("root"))
{
JOptionPane.showMessageDialog(null, "账号或者密码错误", "提示", JOptionPane.INFORMATION_MESSAGE);
erro++;
if(erro==3)
System.exit(0);
}
else
{
this.dispose();//关闭窗口,但程序不退出
new NewSystem();
}
}
}
public static void main(String[] args) {
new Management();
// new NewSystem();//测试
}
}
class NewSystem extends JFrame implements ActionListener
{
JPanel jp1=new JPanel();
JPanel jp11=new JPanel();
JPanel jp12=new JPanel();
JPanel jp13=new JPanel();
JPanel jp14=new JPanel();
JPanel jp15=new JPanel();
JPanel jp16=new JPanel();
JPanel jp2=new JPanel();
JPanel jp21=new JPanel();
JPanel jp22=new JPanel();
JPanel jp23=new JPanel();
JPanel jp24=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JPanel jp5=new JPanel();
JLabel jl1=new JLabel("学号");
JLabel jl1s=new JLabel("学号");
JLabel jl2=new JLabel("姓名");
JLabel jl3=new JLabel("出生日期");
JLabel jl4=new JLabel("系别");
JLabel jl4s=new JLabel("系别");
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JTextField jtf3=new JTextField(10);
String []birth= {"1998-01-01","1999-01-01","2000-01-01"};
JComboBox jcb1=new JComboBox(birth);
String []department={"计算机","软件","物联"};
JComboBox jcb2=new JComboBox(department);
JComboBox jcb3=new JComboBox(department);
JRadioButton jrb1=new JRadioButton("男");
JRadioButton jrb2=new JRadioButton("女");
ButtonGroup bg1=new ButtonGroup();
JRadioButton jrb3=new JRadioButton("男");
JRadioButton jrb4=new JRadioButton("女");
ButtonGroup bg2=new ButtonGroup();
JButton jb1=new JButton("确认输入");
JButton jb2=new JButton("查询");
JButton jb4=new JButton("清除");
JButton jb3=new JButton("显示所有记录");
JTextArea jta=new JTextArea(35,60);
Student s[]=new Student[50];
static int number=0;//学生人数
public NewSystem()
{
//jpanel加入边框线
jp1.setBorder(BorderFactory.createTitledBorder("信息录入"));
jp2.setBorder(BorderFactory.createTitledBorder("查询"));
//信息录入部分
jp1.setLayout(new GridLayout(6,1));
jp11.add(jl1);
jp11.add(jtf1);
jp12.add(jl2);
jp12.add(jtf2);
jp13.add(jp3);
jp13.add(jl3);
jp13.add(jcb1);
bg1.add(jrb1);
bg1.add(jrb2);
jp14.add(jrb1);
jp14.add(jrb2);
jp14.setBorder(BorderFactory.createTitledBorder("性别"));
jp15.add(jl4);
jp15.add(jcb2);
jp16.add(jb1);
//加入信息录入jp1
jp1.add(jp11);
jp1.add(jp12);
jp1.add(jp13);
jp1.add(jp14);
jp1.add(jp15);
jp1.add(jp16);
//查询部分
jp2.setLayout(new GridLayout(4,1));
jp21.add(jl1s);
jp21.add(jtf3);
bg2.add(jrb3);
bg2.add(jrb4);
jp22.add(jrb3);
jp22.add(jrb4);
jp22.setBorder(BorderFactory.createTitledBorder("性别"));
jp23.add(jl4s);
jp23.add(jcb3);
jp24.add(jb2);
jp24.add(jb4);
//加入查询jp2中
jp2.add(jp21);
jp2.add(jp22);
jp2.add(jp23);
jp2.add(jp24);
//显示记录部分
jp5.setLayout(new FlowLayout());
jp5.add(jb3);
jp5.add(jta);
//临时加入的panel
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(2,1));;
jp.add(jp1);
jp.add(jp2);
//布局
this.setLayout(new BorderLayout());
//加入组件
this.add(jp,BorderLayout.WEST);
this.add(jp5,BorderLayout.CENTER);
//设置下拉框初选值为空
jcb1.setSelectedItem(null);
jcb2.setSelectedItem(null);
jcb3.setSelectedItem(null);
//加入事件
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
//属性
this.setSize(880,750);
this.setTitle("学生信息系统");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
//初始化,不然后面出现指针异常
for(int i=0;i<50;i++)
{
s[i]=new Student();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(jb1))//录入
{
if(number<=50)
{
if(jtf1.getText().isEmpty()||jtf1.getText().isEmpty()||jcb1.getSelectedIndex()==-1
||(!jrb1.isSelected()&&!jrb2.isSelected())||jcb2.getSelectedIndex()==-1)
JOptionPane.showMessageDialog(null, "请输入完整信息", "提示", JOptionPane.INFORMATION_MESSAGE);
else
{
s[number].id=jtf1.getText();
s[number].name=jtf2.getText();
switch (jcb1.getSelectedIndex()) {
case 0:s[number].birth="1998-01-01";break;
case 1:s[number].birth="1999-01-01";break;
case 2:s[number].birth="2000-01-01";break;
default:break;
};
if(jrb1.isSelected())
{
s[number].sex="男";
}
if(jrb2.isSelected())
{
s[number].sex="女";
}
switch (jcb2.getSelectedIndex()) {
case 0:s[number].subject="计算机";break;
case 1:s[number].subject="软件";break;
case 2:s[number].subject="物联";break;
default:break;
};
number++;
}
}
print();
}
if(e.getSource().equals(jb2))//查询
{
jta.setText("学号\t"+"姓名\t"+"出生日期\t"+"性别\t"+"系别\n");
find();
}
if(e.getSource().equals(jb3))//显示所有
{
print();
}
if(e.getSource().equals(jb4))//清除
{
jtf3.setText("");
bg2.clearSelection();
jcb3.setSelectedIndex(-1);
}
}
void find()//查询
{
String ids=null;
String sexs=null;
String subjects=null;
if(jtf3.getText().isEmpty()&&(!jrb3.isSelected()&&!jrb4.isSelected())&&jcb3.getSelectedIndex()==-1)
JOptionPane.showMessageDialog(null, "请输入要查询的相关信息", "提示", JOptionPane.INFORMATION_MESSAGE);
else
{
if(!jtf3.getText().isEmpty())
ids=jtf3.getText();
if(jrb3.isSelected())
{
sexs="男";
}
if(jrb4.isSelected())
{
sexs="女";
}
switch (jcb3.getSelectedIndex())
{
case -1:;break;
case 0:subjects="计算机";break;
case 1:subjects="软件";break;
case 2:subjects="物联";break;
};
}
//七种情况
if(ids!=null&&sexs==null&&subjects==null)//1
{
for(int i=0;i<number;i++)
{
if(s[i].id.equals(ids))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids==null&&sexs!=null&&subjects==null)//2
{
for(int i=0;i<number;i++)
{
if(s[i].sex.equals(sexs))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids==null&&sexs==null&&subjects!=null)//3
{
for(int i=0;i<number;i++)
{
if(s[i].subject.equals(subjects))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids!=null&&sexs!=null&&subjects==null)//4
{
for(int i=0;i<number;i++)
{
if(s[i].id.equals(ids)&&s[i].sex.equals(sexs))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids!=null&&sexs==null&&subjects!=null)//5
{
for(int i=0;i<number;i++)
{
if(s[i].id.equals(ids)&&s[i].subject.equals(subjects))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids==null&&sexs!=null&&subjects!=null)//6
{
for(int i=0;i<number;i++)
{
if(s[i].sex.equals(sexs)&&s[i].subject.equals(subjects))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
else if(ids!=null&&sexs!=null&&subjects!=null)//7
{
for(int i=0;i<number;i++)
{
if(s[i].id.equals(ids)&&s[i].sex.equals(sexs)&&s[i].subject.equals(subjects))
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
}
void print()//显示函数
{
jta.setText("学号\t"+"姓名\t"+"出生日期\t"+"性别\t"+"系别\n");
for(int i=0;i<number;i++)
{
jta.append(s[i].id+"\t"+s[i].name+"\t"+s[i].birth+"\t"+s[i].sex+"\t"+s[i].subject+"\n");
}
}
class Student
{
String id;
String name;
String birth;
String sex;
String subject;
}
}
- ????联系交流QQ:2283093518
上一篇: SM4加密解密
下一篇: ThinkPhp5.0模型的使用