📄 class_info_mod.java~3~
字号:
package BJXX;import com.zdsoft.datastore.ASDatastore; //引入本包中的类,因为我们要用到的ASDatastore类(数据仓库)位于本包中import javax.swing.*;import java.awt.*;import java.awt.event.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class class_Info_mod extends ASDialog { BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JPanel jPanel3 = new JPanel(); JLabel jLabel1 = new JLabel(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); GridBagLayout gridBagLayout1 = new GridBagLayout(); JLabel jLabel2 = new JLabel(); JTextField xh = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField name = new JTextField(); Class_Info_Show t1; JLabel jLabel4 = new JLabel(); JTextField sex = new JTextField(); JLabel jLabel5 = new JLabel(); JTextField age = new JTextField();//定义一个test1类(即弹出本对话框的那个页面)的引用,用来接收构造方法中传过来的test1类的对象 public class_Info_mod(Class_Info_Show s) { t1=s;//将创建本类的对象时传入的test1类的对象赋给本类中定义的test1类的引用t1,z这样,当本对话框被创建起来后, //就可以使用t1对象(即主窗体对象)中定义的成员变量和成员方法。 try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //本页面(对话框)的初始化要注意两件事: //(1):设置本对话框的大小; //(2):将本对话框居中; private void jbInit() throws Exception { this.setSize(new Dimension(277, 239));//(1)定义本对话框的大小(宽度,高度); this.getContentPane().setLayout(borderLayout1); jLabel1.setFont(new java.awt.Font("Serif", 0, 15)); jLabel1.setText("增加班级信息"); jButton1.setFont(new java.awt.Font("Serif", 0, 12)); jButton1.setText("取消"); jButton1.addActionListener(new mzxxadd_jButton1_actionAdapter(this)); jButton2.setFont(new java.awt.Font("Serif", 0, 12)); jButton2.setText("确定"); jButton2.addActionListener(new mzxxadd_jButton2_actionAdapter(this)); jPanel1.setLayout(gridBagLayout1); jLabel2.setFont(new java.awt.Font("Serif", 0, 12)); jLabel2.setText("班级编号"); xh.setPreferredSize(new Dimension(80, 22)); xh.setText(""); jLabel3.setFont(new java.awt.Font("Serif", 0, 12)); jLabel3.setText("班级名称"); name.setPreferredSize(new Dimension(80, 22)); name.setText(""); jLabel4.setFont(new java.awt.Font("Serif", 0, 12)); jLabel4.setText("班主任"); sex.setSelectionStart(11); sex.setText("");// jLabel5.setFont(new java.awt.Font("Serif", 0, 12));// jLabel5.setText("年龄");// age.setText(""); this.getContentPane().add(jPanel1, BorderLayout.CENTER); jPanel1.add(jLabel2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0)); jPanel1.add(xh, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0)); jPanel1.add(jLabel3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0)); jPanel1.add(name, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0)); jPanel1.add(jLabel4, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jPanel1.add(sex, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 0, 0, 0), 0, 0)); jPanel1.add(jLabel5, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jPanel1.add(age, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 0, 0, 0), 0, 0)); this.getContentPane().add(jPanel2, BorderLayout.SOUTH); jPanel2.add(jButton2, null); jPanel2.add(jButton1, null); this.getContentPane().add(jPanel3, BorderLayout.NORTH); jPanel3.add(jLabel1, null); this.setWindowCenter();//(2)将本对话框居中; } void jButton1_actionPerformed(ActionEvent e) { this.dispose();//关闭本对话框; } void jButton2_actionPerformed(ActionEvent e) { //当点击“保存”按扭后: //(1)创建数据中心对象 ASDatacenter dc = new ASDatacenter(); //(2)连接数据库 dc.connect(); //(3)构造插入数据库数据的SQL语句 String sql = "insert into bj_info(id,name,teacher) values("+xh.getText()+",'"+ name.getText()+"','"+sex.getText()+"')"; System.out.println(sql); //(4)调用数据中心对象dc中的update方法,执行插入数据库数据操作, String[] msg = dc.update(sql); //本方法的返回值为一个两个元素的字符串数组。用来表示本次插入数据库数据操作是否成功; //其中,当msg[0]的值为“true”时,表示本次插入数据库数据操作成功,此时,msg[1]的值为“”; //当msg[0]的值为“false”时,表示本次插入数据库数据操作失败,此时,msg[1]的值为操作失败的原因说明; if(msg[0].equals("true")){//当msg[0]的值为“true”时,表示本次插入数据库数据操作成功 //本次插入数据库数据操作成功后,还需要做两件事: //(1):将父窗体中的数据仓库对象(即用来显示数据用的ASDatastore 类的对象ds)增加一行并填入相应的数据 int row = t1.ds.addRow();//父窗体中的数据仓库对象ds增加一行,并返回所增加行的行数 //从本对话框中取得增加的数据,填入父窗体中的数据仓库对象ds的相应行列中 //因为ASDatastore类的setObjectAt可能会抛出异常,所以要将包含setObjectAt方法的语句写到try{}catch(){}语句块中 try { t1.ds.setObjectAt(row,"id",new Integer(xh.getText()));//调用ASDatastore类的setObjectAt方法,将增加的相应值的“对象” t1.ds.setObjectAt(row,"name",name.getText()); t1.ds.setObjectAt(row,"teacher",sex.getText());// t1.ds.setObjectAt(row,"age",new Integer(age.getText())); //填入ds的相应行和列 //通知显示ds数据的表格,ds的数据已发生改变 t1.jTable1.ASTableRepant(); } catch (Exception ex) { ex.printStackTrace(); } //(2)关闭本本对话框 this.dispose(); } else{//数据库增加失败 //弹出错误信息提示框 dc.disconnect(); JOptionPane.showMessageDialog(this,"数据库增加失败!","错误",JOptionPane.ERROR_MESSAGE); this.dispose(); } //关闭数据库连接 dc.disconnect(); }}class class_Info_mod_jButton1_actionAdapter implements java.awt.event.ActionListener { class_Info_mod adaptee; class_Info_mod_jButton1_actionAdapter(class_Info_mod adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class mzxxadd_jButton2_actionAdapter implements java.awt.event.ActionListener { bjadd adaptee; mzxxadd_jButton2_actionAdapter(bjadd adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -