如果有需要带数据库的,请小伙伴移步至:java+mysql学生管理系统(附源码,无套路)
事先声明: 程序简易,只是为了记录学习过程,大佬们勿喷,要实在忍不住,麻烦各位大佬轻点喷就是!
但是如果有任何问题,欢迎大家评论,望共同进步!
密码在提示里
这是第一段代码
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Gradation {
public static void main(String[] args) {
JFrame jf = new JFrame("登录");
jf.setBounds(300,200,300,150);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//绝对布局
JPanel panel = new JPanel(null);
JLabel jl = new JLabel("账户");
jl.setBounds(10,10,200,18);
final JTextField name = new JTextField();
name.setBounds(80,10,150,18);
JLabel jl2 = new JLabel("密码");
jl2.setBounds(10,50,200,18);
final JPasswordField password = new JPasswordField();
password.setBounds(80,50,150,18);
panel.add(jl);
panel.add(name);
panel.add(jl2);
panel.add(password);
//创建一个按钮,点击后获取密码框中输入的密码
JButton jb = new JButton("登录");
jb.setBounds(80,80,60,18);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(name.getText().trim().length() == 0||
new String (password.getPassword()).trim().length() == 0)
{
JOptionPane.showMessageDialog(null,"信息错误");
}
else if(name.getText().trim().equals("66")&&
new String(password.getPassword()).trim().equals("six"))
{
new Demo();
jf.dispose(); //先关闭第一个窗口
}
else
JOptionPane.showMessageDialog(null,"登录失败,账户或密码错误");
}
});
panel.add(jb);
JButton jb2 = new JButton("提示");
jb2.setBounds(170,80,60,18);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"账号:66\n密码:six");
}
});
panel.add(jb2);
jf.setContentPane(panel);
jf.setVisible(true);
}
}
.
.
.
.
.
这是第二段代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Demo {
private JFrame jf2;
private JTextArea jTextArea; //显示信息的文本框
private File file;
private JPanel contentPane,found_panel,panel2; //面板
private JLabel j1,j2,j3,j4; //四个名称标签
private JTextField jT_1,jT_2,jT_3,jT_4; //对应的四个输入框
private JButton jb_1,jb_2,found_back,found_reserve;
private JMenuBar menuBar;
private JMenu inforMenu,aboutMenu;
private JMenuItem newMenItem,deleteMenItem,foundMenItem,changeMenItem,about_son;
public Demo() {
//设置第二个窗口
jf2 = new JFrame("LS");
jf2.setSize(500, 300);
jf2.setLocationRelativeTo(null);
jf2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 创建底部面板
contentPane = new JPanel();
// 创建“查询”所需面板
found_panel = new JPanel();
// 创建上层面板
panel2 = new JPanel(new GridLayout(5,2));
// 上层面板中的“新建”内容
j1 = new JLabel("姓名");
jT_1 = new JTextField();
j2 = new JLabel("学号");
jT_2 = new JTextField();
j3 = new JLabel("年龄");
jT_3 = new JTextField();
j4 = new JLabel("专业");
jT_4 = new JTextField();
panel2.add(j1);
panel2.add(jT_1);
panel2.add(j2);
panel2.add(jT_2);
panel2.add(j3);
panel2.add(jT_3);
panel2.add(j4);
panel2.add(jT_4);
jb_1 = new JButton("提交");
jb_2 = new JButton("返回");
panel2.add(jb_1);
panel2.add(jb_2);
// “查询"的页面
jTextArea = new JTextArea();
//jTextArea.setBounds(10,10,30,30);
// 返回开始界面按钮
found_back = new JButton("返回");
//found_back.setBounds(20,100,20,20);
// 保存更改信息按钮
found_reserve = new JButton("保存");
found_panel.add(jTextArea);
found_panel.add(found_back);
found_panel.add(found_reserve);
// 信息存入txt文件中
file = new File("d:\\学生信息.txt");
if(!(file.exists())) //如果不存在此txt文件,就新建一个
{
try
{
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
catch (IOException el)
{
el.printStackTrace();
}
}
try
{
BufferedReader read = new BufferedReader(new FileReader(file));
String str = null;
while((str = read.readLine()) != null)
{
jTextArea.append(str+ "\n");
}
read.close();
}
catch (FileNotFoundException el)
{
el.printStackTrace();
}
catch (IOException el)
{
el.printStackTrace();
}
/*
* 创建一个菜单栏
*/
menuBar = new JMenuBar();
/*
* 创建一级菜单
*/
inforMenu = new JMenu("信息管理");
aboutMenu = new JMenu("关于");
// 一级菜单添加到菜单栏
menuBar.add(inforMenu);
menuBar.add(aboutMenu);
newMenItem = new JMenuItem("新建");
deleteMenItem = new JMenuItem("全部删除");
changeMenItem = new JMenuItem("修改"); //没做这块
foundMenItem = new JMenuItem("查询");
about_son = new JMenuItem("再关于");
// 向“信息管理”中添加子菜单
inforMenu.add(newMenItem);
inforMenu.add(deleteMenItem);
inforMenu.add(changeMenItem);
inforMenu.add(foundMenItem);
aboutMenu.add(about_son);
Event_do();
contentPane.add(menuBar); //将整个JMenuBar加到panel2上
JLabel label = new JLabel("欢迎使用学生管理系统");
label.setFont(new Font(Font.DIALOG,Font.BOLD,23)); //设置字体大小
label.setForeground(Color.orange); //设置字体颜色
contentPane.add(label);
jf2.setContentPane(contentPane);
jf2.setVisible(true);
}
public void output()
{
String str = jTextArea.getText();
try
{
FileOutputStream out = new FileOutputStream(file);
byte[] Byte = str.getBytes(); //将字符串转化成字节数组
out.write(Byte,0,Byte.length);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void Event_do()
{
// 设置 "新建" 子菜单被点击的监听器
newMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(panel2);
contentPane.validate();
contentPane.repaint();
jT_1.setText("");
jT_2.setText("");
jT_3.setText("");
jT_4.setText("");
}
});
// 设置 "删除" 子菜单被点击的监听器
deleteMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.setText("");
output();
}
});
// 设置 "修改" 子菜单被点击的监听器
changeMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"想改?做梦!自己滚去改");
}
});
// 设置 "查询" 子菜单被点击的监听器
foundMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(found_panel);
contentPane.validate();
contentPane.repaint();
}
});
// “查询”界面的返回 “按钮” 的监听器
found_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// “查询”界面的返回 “保存” 的监听器
found_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//output();
}
});
// 新建的中的“添加”的监听事件
jb_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.append(jT_1.getText()+"\t"+jT_2.getText()+"\t"+jT_3.getText()+"\t"+jT_4.getText()+"\n");
output();
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// 新建的中的“返回”的监听事件
jb_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
about_son.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"仅供点击,请勿当真!");
}
});
}
}