📄 studentregistpanel.java
字号:
package cn.edu.csu.oo.gui.project.view.panel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
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.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import cn.edu.csu.oo.gui.container.GBC;
import cn.edu.csu.oo.gui.project.action.studentaction.ActionListenerOper;
import cn.edu.csu.oo.gui.project.view.common.CheckInputText;
import cn.edu.csu.oo.gui.project.view.common.DataPicker;
import cn.edu.csu.oo.gui.project.view.viewaction.FocusAction;
import cn.edu.csu.oo.gui.project.vo.StudentVo;
/**
* 学生信息注册面板
* @author ibm
*
*/
public class StudentRegistPanel extends JFrame {
protected JTextField stuId, stuName, stuSex, stuMajor, stuGrade, stuMail,
stuAddress, stuMobile;
protected JComboBox txtRegisteDate = null;
private DataPicker dataPicker = new DataPicker();
// 单选按钮
protected JRadioButton radmale, radfemale;
private ActionListener actionListener;
public StudentRegistPanel() {
this.setLayout(new BorderLayout());
this.add(getStudentInfoPanel(),"North");
this.add(getBtnPanel());
this.add(buildJScrollTablePane(),"South");
this.setVisible(true);
}
/**
* 初始化面板输入框
*
*/
public void initialText() {
this.stuId = new JTextField(10);
this.stuName = new JTextField(10);
this.stuSex = new JTextField(10);
this.stuMajor = new JTextField(10);
this.stuGrade = new JTextField(10);
this.stuMail = new JTextField(10);
this.stuAddress = new JTextField(10);
this.stuMobile = new JTextField(10);
this.radmale = new JRadioButton("男");
this.radfemale = new JRadioButton("女");
this.txtRegisteDate = dataPicker.getDataPacker();
}
String radioStr = "";
/**
* 构造单选按钮组
* @return
*/
public JPanel getSex() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
group.add(this.radmale);
group.add(this.radfemale);
panel.add(this.radmale);
panel.add(this.radfemale);
radmale.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
radioStr = e.getActionCommand();
}
});
radfemale.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
radioStr = e.getActionCommand();
}
});
//设置panel的边框
panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
return panel;
}
public JPanel getStudentInfoPanel() {
this.initialText();
JPanel panel = new JPanel();
//设置panel为GridBagLayout布局管理
panel.setLayout(new GridBagLayout());
//添加各个组件到panel上,并对每个组件采用GridBagConstraints实例进行布局管理。
panel.add(new JLabel("学生编号:"), new GBC(0, 0).setFill(GBC.WEST));
panel.add(this.stuId, new GBC(1, 0).setInset(7).setWeight(8, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("学生姓名:"), new GBC(2, 0).setFill(GBC.WEST));
panel.add(this.stuName, new GBC(3, 0).setInset(7).setWeight(8, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("学生性别:"), new GBC(4, 0).setFill(GBC.WEST));
panel.add(this.getSex(), new GBC(5, 0).setInset(3).setWeight(3, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("学生专业:"), new GBC(0, 1).setFill(GBC.WEST));
panel.add(this.stuMajor, new GBC(1, 1).setInset(7).setWeight(3, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("学生年级:"), new GBC(2, 1).setFill(GBC.WEST));
panel.add(this.stuGrade, new GBC(3, 1).setInset(7).setWeight(3, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("联系邮箱:"), new GBC(4, 1).setFill(GBC.WEST));
panel.add(this.stuMail, new GBC(5, 1).setInset(7).setWeight(3, 0).setFill(
GBC.HORIZONTAL));
panel.add(new JLabel("宿舍地址:"), new GBC(0, 2).setFill(GBC.WEST));
panel.add(this.stuAddress, new GBC(1, 2).setInset(7).setWeight(3, 0)
.setFill(GBC.HORIZONTAL));
panel.add(new JLabel("联系电话:"), new GBC(2, 2).setFill(GBC.WEST));
panel.add(this.stuMobile, new GBC(3, 2).setInset(7).setWeight(3, 0)
.setFill(GBC.HORIZONTAL));
panel.add(new JLabel("入学时间:"), new GBC(4, 2).setFill(GBC.WEST));
panel.add(this.txtRegisteDate, new GBC(5, 2).setInset(7).setWeight(3, 0).setFill(GBC.HORIZONTAL));
panel.setBorder(BorderFactory.createTitledBorder("学生注册信息"));
// System.out.println(panel.getComponentCount());
stuId.addKeyListener(new FocusAction(panel,1));
stuName.addKeyListener(new FocusAction(panel,3));
stuMajor.addKeyListener(new FocusAction(panel,7));
stuGrade.addKeyListener(new FocusAction(panel,9));
stuMail.addKeyListener(new FocusAction(panel,11));
stuAddress.addKeyListener(new FocusAction(panel,13));
stuMobile.addKeyListener(new FocusAction(panel,15));
txtRegisteDate.addKeyListener(new FocusAction(panel,17));
return panel;
}
/**
* 根据传入的参数构造各个按钮
*
* @param name
* @return
*/
public JButton buildBtn(String name) {
JButton btn = new JButton(name);
btn.setToolTipText(name);
//构造事件监听器对象
actionListener = new ActionListenerOper(this);
//添加按钮事件处理
btn.addActionListener(actionListener);
return btn;
}
/**
* 构造按钮面板
*
* @return
*/
public JPanel getBtnPanel() {
JPanel btnPanel = new JPanel();
btnPanel.add(buildBtn("确定"));
btnPanel.add(buildBtn("重置"));
btnPanel.add(buildBtn("取消"));
btnPanel.setBorder(BorderFactory.createTitledBorder("按钮组"));
return btnPanel;
}
/**
* 创建工具栏的公用方法,
*
* @param name
* @return
*/
public JButton createToolButton(String name/*, String icon*/) {
JButton btn = new JButton(name/*, new ImageIcon(icon)*/);
btn.setToolTipText(name);
btn.setBorderPainted(true);
btn.setVerticalTextPosition(JButton.BOTTOM);
btn.setHorizontalTextPosition(JButton.CENTER);
return btn;
}
/**
* 添加左侧常用工具的按钮
*
*/
public Box getBoxPanel() {
Box box = new Box(BoxLayout.Y_AXIS);
box.add(createToolButton("学生注册"));//, "icons/2003051330.gif"));
box.add(Box.createVerticalStrut(15));
box.add(createToolButton("学生查询"));// "icons/2003051345.gif"));
box.add(Box.createVerticalStrut(15));
box.add(createToolButton("学生维护"));// "icons/2003051349.gif"));
box.add(Box.createVerticalStrut(15));
box.add(createToolButton("学生就业"));//"icons/2003051829.gif"));
box.add(Box.createVerticalStrut(15));
box.add(createToolButton("学生交流"));// "icons/09macos08.gif"));
box.add(Box.createVerticalStrut(35));
box.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createEtchedBorder(), "工具"));
return box;
}
/**
* 获取界面输入信息并封装在StudentVo对象中
* @return
*/
public StudentVo getInputTextValue(){
int studentNo = Integer.parseInt(stuId.getText());
String studentName = stuName.getText();
String studentSex = radioStr;
String studentMajor = stuMajor.getText();
int studentGrade = Integer.parseInt(stuGrade.getText());
String studentMail = stuMail.getText();
String studentAddress = stuAddress.getText();
String studentMobile = stuMobile.getText();
String studentDate = txtRegisteDate.getSelectedItem().toString();
return new StudentVo(studentNo,studentName,studentSex,studentMajor,
studentGrade,studentMail,studentAddress,studentMobile,studentDate);
}
public boolean checkInputText(){
CheckInputText check = new CheckInputText();
if(check.checkInputIsNull(stuId.getText())){
JOptionPane.showMessageDialog(null,"学号不能为空","message",JOptionPane.YES_OPTION);
stuId.requestFocus();
return false;
}
if(!check.checkIsContainNumber(stuId.getText())){
JOptionPane.showMessageDialog(null,"学号只能为数字序列","message",JOptionPane.YES_OPTION);
stuId.requestFocus();
return false;
}
if(check.checkInputIsNull(stuName.getText())){
JOptionPane.showMessageDialog(null,"姓名不能为空","message",JOptionPane.YES_OPTION);
stuName.requestFocus();
return false;
}
if(check.checkInputIsNull(stuMajor.getText())){
JOptionPane.showMessageDialog(null,"专业不能为空","message",JOptionPane.YES_OPTION);
stuMajor.requestFocus();
return false;
}
if(check.checkInputIsNull(stuGrade.getText())){
JOptionPane.showMessageDialog(null,"年级不能为空","message",JOptionPane.YES_OPTION);
stuGrade.requestFocus();
return false;
}
if(check.checkInputIsNull(stuMail.getText())){
JOptionPane.showMessageDialog(null,"邮箱不能为空","message",JOptionPane.YES_OPTION);
stuMail.requestFocus();
return false;
}
if(check.checkInputIsNull(stuAddress.getText())){
JOptionPane.showMessageDialog(null,"宿舍不能为空","message",JOptionPane.YES_OPTION);
stuAddress.requestFocus();
return false;
}
if(check.checkInputIsNull(stuMobile.getText())){
JOptionPane.showMessageDialog(null,"电话号码不能为空","message",JOptionPane.YES_OPTION);
stuMobile.requestFocus();
return false;
}
return true;
}
public JScrollPane buildJScrollTablePane(){
JScrollPane js = new JScrollPane(buildJTable());
return js;
}
private JTable table;
public JTable buildJTable(){
if(table == null){
Object[][] data = {};
String[] name = {"学生编号","学生姓名","学生性别","所属专业","所属年级","联系邮箱","宿舍地址","联系电话","入学时间"};
DefaultTableModel model = new DefaultTableModel(data,name);
table = new JTable(model);
}
return table;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
// UIManager.setLookAndFeel("com.nilo.plaf.nimrod.NimRODLookAndFeel");
// UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceFieldOfWheatLookAndFeel");
// UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceMagmaLookAndFeel");
// UIManager.setLookAndFeel(new SubstanceFieldOfWheatLookAndFeel()) ;
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
}
new StudentRegistPanel();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -