📄 insertstuframe.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InsertStuFrame extends JFrame
{
final private int CNT = 8;
final private String stuInfoLableText[] = {
"学号: ",
"姓名: ",
"性别: ",
"年龄: ",
"政治面貌:",
"专业: ",
"民族: ",
"籍贯: "
};
private JLabel stuInfoLabel[] = new JLabel[CNT] ;
private JTextField stuInfoText[] = new JTextField[CNT] ;
private JPanel content[] = new JPanel[CNT] ;
//使用数组而不使用单个对象的优点:
/* 1.程序更加紧凑,简洁易读
*2.可扩展性更好,
/*private JLabel stuIDLabel;
private JLabel nameLabel ;
private JLabel sexLabel ;
private JLabel ageLabel ;
private JLabel visageLabel ;
private JLabel specialityLabel;
private JLabel nationLabel;
private JLabel homeLabel;
private JTextField stuIDText;
private JTextField nameText;
private JTextField sexText;
private JTextField ageText;
private JTextField visageText;
private JTextField specialityText;
private JTextField nationText;
private JTextField homeText;
*/
private JButton okBtn;
private JButton cancleBtn;
private JPanel confirmPanel = new JPanel();
private static final InsertStuFrame InsertStu = new InsertStuFrame();
public static InsertStuFrame singleInsertStuFrame()
{
return InsertStu;
}
private InsertStuFrame()
{
//初始化窗口
initInsertStuFrame();
//为button注册监听器
ClickActionListener cal = new ClickActionListener();
okBtn.addActionListener(cal);
cancleBtn.addActionListener(cal);
}
private void initInsertStuFrame()
{
this.setTitle("添加学生信息");
for (int i=0;i<CNT;i++)
{
stuInfoLabel[i] = new JLabel(stuInfoLableText[i] );
stuInfoText[i] = new JTextField(10);
content[i] = new JPanel();
content[i].add(stuInfoLabel[i]);
content[i].add(stuInfoText[i]);
this.add(content[i]);
}
okBtn = new JButton("确定");
cancleBtn=new JButton("清除");
confirmPanel.add(okBtn);
confirmPanel.add(cancleBtn);
this.add(confirmPanel);
this.setLayout(new FlowLayout());
this.setSize(380,420);
/*
content = new JPanel();
stuIDLabel = new JLabel("学号: ");
nameLabel = new JLabel("姓名: ");
sexLabel = new JLabel("性别: ");
ageLabel = new JLabel("年龄: ");
visageLabel = new JLabel("政治面貌: ");
specialityLabel = new JLabel("专业: ");
nationLabel = new JLabel("民族: ");
homeLabel = new JLabel("籍贯: ");
stuIDText= new JTextField(10);
nameText = new JTextField(10);
sexText = new JTextField(10);
ageText = new JTextField(10);
visageText = new JTextField(10);
specialityText = new JTextField(10);
nationText = new JTextField(10);
homeText = new JTextField(10);
okBtn = new JButton("确定");
cancleBtn=new JButton("清除");
content.add(stuIDLabel);
content.add(stuIDText);
content.add(nameLabel);
content.add(nameText);
content.add(sexLabel);
content.add(sexText);
content.add(ageLabel);
content.add(ageText);
content.add(visageLabel);
content.add(visageText);
content.add(specialityLabel);
content.add(specialityText);
content.add(nationLabel);
content.add(nationText);
content.add(homeLabel);
content.add(homeText);
content.add(okBtn);
content.add(cancleBtn);
content.setLayout(new FlowLayout());
this.add(content);
this.setSize(300,300);*/
}
class ClickActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String stuInfo[] = new String[CNT];
/*String stuID = null;
String name= null;
String sex= null;
String age= null;
String visage = null;
String speciality = null;
String nation = null;
String home = null;
*/
System.out.println("insert button ");
if (e.getActionCommand() == "确定")
{
System.out.println("insert button is 确定");
for(int i = 0;i<CNT;i++)
{
stuInfo[i] = stuInfoText[i].getText();
}
/*
stuID = stuIDText.getText();
name = nameText.getText();
sex = sexText.getText();
age = ageText.getText();
visage = visageText.getText();
speciality =specialityText.getText();
nation = nationText.getText();
home = homeText.getText();
*/
String sqlInsert = null;
sqlInsert = "insert into 学生信息 values (" +
"\'" + stuInfo[0] + "\'" + "," +
"\'" + stuInfo[1] + "\'" + "," +
"\'" + stuInfo[2] + "\'" + "," +
"\'" + stuInfo[3] + "\'" + "," +
"\'" + stuInfo[4] + "\'" + "," +
"\'" + stuInfo[5] + "\'" + "," +
"\'" + stuInfo[6] + "\'" + "," +
"\'" + stuInfo[7] + "\'" + ")" ;
System.out.println("sqlInsert = " + sqlInsert);
SMISDB insertStuInfoDB = new SMISDB();
insertStuInfoDB.getTable(sqlInsert,new JFrame(),true);
//ClearAllTextField();
}
else if (e.getActionCommand() == "清除")
{
System.out.println("insert button is 清除");
ClearAllTextField();
}
}
private void ClearAllTextField()
{
/*stuIDText.setText("");
nameText.setText("");
sexText.setText("");
ageText.setText("");
visageText.setText("");
specialityText.setText("");;
nationText.setText("");
homeText.setText("");
*/
for (int i=0;i<CNT;i++)
{
stuInfoText[i].setText("");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -