Java实训小结

今天完成的窗口如下:

1.浏览学生窗口BrowseStudentsFrame

package net.lwf.student.gui;

import net.lwf.student.bean.Student;
import net.lwf.student.service.StudentService;
import net.lwf.student.service.impl.StudentServiceImpl;

import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

/**

功能:浏览学生记录

作者:王凌宇
日期:2019年6月22日
/
public class BrowseStudentsFrame extends JFrame {
/*

声明面板
/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/*
声明标签
/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/*
声明文本框
/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/*
声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
/**

当前记录行号
/
private int currentRow;
/*
学生列表
/
private List students;
/*
创建学生服务对象
*/
StudentService studentService;
/**

构造方法
@param title
*/
public BrowseStudentsFrame(String title) {
super(title);
initGUI();
}
/**

初始化用户界面
*/
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();

pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();

pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));

lblId = new JLabel(“学号:”);
lblName = new JLabel(“姓名:”);
lblSex = new JLabel(“性别:”);
lblAge = new JLabel(“年龄:”);
lblDepartment = new JLabel(“系部:”);
lblClass = new JLabel(“班级:”);
lblTelephone = new JLabel(“电话:”);

txtId = new JTextField(40);
txtName = new JTextField(40);
txtSex = new JTextField(40);
txtAge = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);

btnTop = new JButton(“第一条[T]”);
btnPrevious = new JButton(“上一条[P]”);
btnNext = new JButton(“下一条[N]”);
btnBottom = new JButton(“最后一条[B]”);
btnExit = new JButton(“退出[X]”);
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);

// 添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlSouth, BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7, 1));

pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);

pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);

pnlSouth.add(btnTop);
pnlSouth.add(btnPrevious);
pnlSouth.add(btnNext);
pnlSouth.add(btnBottom);
pnlSouth.add(btnExit);

// 设置窗口属性
setSize(500, 300);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 判断是否有学生记录
if (students.size() > 0) {
// 设置当前记录号
currentRow = 1;
// 设置窗口标题
setTitle(“浏览学生表记录” + " && 当前记录:" + currentRow);
// 填充窗口各文本框数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, “表中没有记录!”, “浏览学生表记录”, JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}

// 【第一条】按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});

// 【上一条】按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow > 1) {
// 设置当前记录号
currentRow–;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, “已到第一条记录!”, “浏览学生表记录”, JOptionPane.WARNING_MESSAGE);
}
}
});

// 【下一条】按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() - 1) {
// 设置当前记录号
currentRow++;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, “已到最后一条记录!”, “浏览学生表记录”, JOptionPane.WARNING_MESSAGE);
}
}
});

// 【最后一条】按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = students.size() - 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});

// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
--------------------- 
作者:Because� 
来源:CSDN 
原文:https://blog.csdn.net/FenggegeBOK/article/details/93367913 
版权声明:本文为博主原创文章,转载请附上博文链接!
上一篇:java-为什么我的组件没有包装在Swing的FlowLayout中?


下一篇:Java Swing – JTable没有显示