用JAVA图形界面做一个学生管理系统,要有增删改查功能,不要连接数据库的...
发布网友
发布时间:2022-04-30 05:06
我来回答
共2个回答
热心网友
时间:2022-04-08 05:48
你试一下这个。
应该都达到你说的了。
-------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AppStudent extends JFrame {
private JTextField nameField;
private JTextField sexField;
private JTextField ageField;
private JTextField stunoField;
private JTextField clazzField;
private JTextField facultyField;
private JTable table;
private DefaultTableModel model;
private String[] columns = { "姓名", "性别", "年龄", "学号", "班级", "系别" };
private List data;
private Student tmp;
// 姓名,性别,年龄,学号,班级,系别
public AppStudent() {
data = new ArrayList();
getContentPane().setLayout(null);
JLabel lblName = new JLabel("姓名");
lblName.setBounds(12, 10, 50, 13);
getContentPane().add(lblName);
nameField = new JTextField();
nameField.setBounds(74, 7, 96, 19);
getContentPane().add(nameField);
nameField.setColumns(10);
JLabel lblSex = new JLabel("性别");
lblSex.setBounds(182, 10, 50, 13);
getContentPane().add(lblSex);
sexField = new JTextField();
sexField.setBounds(244, 7, 96, 19);
getContentPane().add(sexField);
sexField.setColumns(10);
JLabel lblAge = new JLabel("年龄");
lblAge.setBounds(352, 10, 50, 13);
getContentPane().add(lblAge);
ageField = new JTextField();
ageField.setBounds(414, 7, 96, 19);
getContentPane().add(ageField);
ageField.setColumns(10);
JLabel lblStuno = new JLabel("学号");
lblStuno.setBounds(12, 36, 50, 13);
getContentPane().add(lblStuno);
stunoField = new JTextField();
stunoField.setColumns(10);
stunoField.setBounds(74, 33, 96, 19);
getContentPane().add(stunoField);
JLabel lblClass = new JLabel("班级");
lblClass.setBounds(182, 36, 50, 13);
getContentPane().add(lblClass);
clazzField = new JTextField();
clazzField.setColumns(10);
clazzField.setBounds(244, 33, 96, 19);
getContentPane().add(clazzField);
JLabel lblFaculty = new JLabel("系别");
lblFaculty.setBounds(352, 36, 50, 13);
getContentPane().add(lblFaculty);
facultyField = new JTextField();
facultyField.setColumns(10);
facultyField.setBounds(414, 33, 96, 19);
getContentPane().add(facultyField);
JButton btnAdd = new JButton("add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add();
}
});
btnAdd.setBounds(192, 59, 67, 21);
getContentPane().add(btnAdd);
JButton btnDel = new JButton("del");
btnDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
del();
}
});
btnDel.setBounds(271, 59, 58, 21);
getContentPane().add(btnDel);
JButton btnUpdate = new JButton("update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}
});
btnUpdate.setBounds(352, 59, 77, 21);
getContentPane().add(btnUpdate);
JButton btnFind = new JButton("find");
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
find();
}
});
btnFind.setBounds(441, 59, 69, 21);
getContentPane().add(btnFind);
model = new DefaultTableModel(columns, 0);
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
nameField.setText((String) table.getValueAt(row, 0));
sexField.setText((String) table.getValueAt(row, 1));
ageField.setText((String) table.getValueAt(row, 2));
stunoField.setText((String) table.getValueAt(row, 3));
clazzField.setText((String) table.getValueAt(row, 4));
facultyField.setText((String) table.getValueAt(row, 5));
tmp = getInput();
}
});
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(12, 90, 571, 248);
getContentPane().add(scrollPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(601, 380);
setResizable(false);
setVisible(true);
}
private Student getInput() {
Student stu = new Student();
stu.name = nameField.getText();
stu.sex = sexField.getText();
stu.age = ageField.getText();
stu.stuno = stunoField.getText();
stu.clazz = clazzField.getText();
stu.faculty = facultyField.getText();
return stu;
}
private void add() {
data.add(getInput());
showTable();
}
private void del() {
for (int i = 0; i < data.size(); i++) {
if (tmp.equals(data.get(i))) {
data.remove(i);
break;
}
}
showTable();
}
private void update() {
Student stu = getInput();
for (int i = 0; i < data.size(); i++) {
if (tmp.equals(data.get(i))) {
data.remove(i);
data.add(i, stu);
break;
}
}
showTable();
}
private void find() {
removeRows();
Student stu = getInput();
for (int i = 0; i < data.size(); i++) {
Student tmp = (Student) data.get(i);
if (tmp.equals(stu)) {
model.addRow(tmp.toArray());
break;
}
}
}
private void showTable() {
removeRows();
for (int i = 0; i < data.size(); i++) {
Student stu = (Student) data.get(i);
model.addRow(stu.toArray());
}
}
private void removeRows() {
while (model.getRowCount() > 0) {
model.removeRow(0);
}
}
public static void main(String[] args) {
new AppStudent();
}
}
class Student implements Serializable {
public String name;
public String sex;
public String age;
public String stuno;
public String clazz;
public String faculty;
public boolean equals(Object obj) {
return equals((Student) obj);
}
public boolean equals(Student obj) {
boolean isName = true;
if (obj.name != null && !"".equals(obj.name)) {
isName = name.equals(obj.name);
}
boolean isSex = true;
if (obj.sex != null && !"".equals(obj.sex)) {
isSex = sex.equals(obj.sex);
}
boolean isAge = true;
if (obj.age != null && !"".equals(obj.age)) {
isAge = age.equals(obj.age);
}
boolean isStuno = true;
if (obj.stuno != null && !"".equals(obj.stuno)) {
isStuno = stuno.equals(obj.stuno);
}
boolean isClazz = true;
if (obj.clazz != null && !"".equals(obj.clazz)) {
isClazz = clazz.equals(obj.clazz);
}
boolean isfaculty = true;
if (obj.faculty != null && !"".equals(obj.faculty)) {
isfaculty = faculty.equals(obj.faculty);
}
return isName && isSex && isAge && isStuno && isClazz && isfaculty;
}
public String[] toArray() {
return new String[] { name, sex, age, stuno, clazz, faculty };
}
}
热心网友
时间:2022-04-08 07:06
登陆界面:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class zhujiemian extends JFrame implements ActionListener
{
JLabel lb1,lb2,lb3;
JPanel p1,p2,p3,p4,p5;
JTextField txtt1;
JPasswordField pwd;
JButton btn1,btn2;
Container c;
JPanel imagePanel ;
ImageIcon background;
public zhujiemian(){
super();
p1=new JPanel();
lb1=new JLabel("欢迎使用学生信息管理系统");
lb1.setFont(new Font("华文新魏", Font.BOLD, 45));
p1.add(lb1);
p2=new JPanel();
lb2=new JLabel("用户名:");
lb2.setFont(new Font("华文新魏", Font.BOLD, 26));
txtt1=new JTextField(18);
txtt1.setFont(new Font("",Font.BOLD,16));
p2.add(lb2);
p2.add(txtt1);
p3=new JPanel();
lb3=new JLabel("密码 :");
lb3.setFont(new Font("华文新魏", Font.BOLD, 26));
pwd=new JPasswordField(18);
pwd.setFont(new Font("",Font.BOLD,16));
p3.add(lb3);
p3.add(pwd);
p4=new JPanel();
btn1=new JButton("<html>"+"<font color=>登录</font>");
JRootPane rootPane = this.getRootPane();
rootPane.setDefaultButton(btn1);
btn1.setFont(new Font("华文行楷", Font.BOLD, 20));
//注册监听者
btn1.addActionListener(this);
btn2=new JButton("<html>"+"<font color=>取消</font>");
btn2.setFont(new Font("华文行楷", Font.BOLD, 20));
btn2.addActionListener(this);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e)
{
}
p4.add(btn1);
p4.add(Box.createRigidArea(new Dimension(50,0)));
p4.add(btn2);
// p5=new JPanel();
// p5.add(p1);
/// p5.add(p2);
// p5.add(p3);
// p5.add(p4);
// c=getContentPane();
//c.add(p1);
//c.add(p2);
//c.add(p3);
// c.add(p5);
// p5.setLayout(new GridLayout(0,1));
background = new ImageIcon("jpg\\c.jpg");//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
// 内容窗格默认的布局管理器为BorderLayout
// imagePanel.setLayout(new FlowLayout(FlowLayout.CENTER,35,35));
imagePanel.setLayout(new GridLayout(0,1));
// c.setLayout(new FlowLayout());
// c.setLayout(new BoxLayout(c,BoxLayout.PAGE_AXIS));
p1.setOpaque(false);
p2.setOpaque(false);
p3.setOpaque(false);
p4.setOpaque(false);
imagePanel.add(p1);
imagePanel.add(p2);
imagePanel.add(p3);
imagePanel.add(p4);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
//设置大小
this.setSize(800,660);
// this.setBackground(Color.yellow);
// p5.setBorder( BorderFactory.createLineBorder(Color.red));
this.setVisible(true);
this.setLocation(50,50);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int n = JOptionPane.showConfirmDialog(null, "确认退出吗?", "确认对话框",JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION)
System.exit(0);
}
});
}
//监听者类
public void actionPerformed(ActionEvent e){
//获得输入的用户名和密码
String user=txtt1.getText().trim();
String password=pwd.getText().trim();
String sql="select * from user where username='"+user+"' and userpswd='"+password+"'";
System.out.println(sql);
//通过创建MyConnection类的对象,来调用chaxun()方法,获得是否有当前的用户
MyConnection mc=new MyConnection();
ResultSet rs=mc.chaxun(sql);
try{
if(rs.next()){
JOptionPane.showMessageDialog(null, "*********欢迎进入*********");
//创建主界面对象,让主界面框架进行显示
Students sts=new Students();
sts.setVisible(true);
//将当前窗体隐藏并释放资源
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入");
}
if(e.getSource()==btn2)
{
txtt1.setText("");
pwd.setText("");
}
}
catch(Exception ce)
{
ce.printStackTrace();
System.out.println("查找失败");}
}
public static void main(String args[])
{
zhujiemian zjm=new zhujiemian();
zjm.setTitle("学生信息管理系统");
}
}