📄 ljb_add.java~6~
字号:
package LJB;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import com.zdsoft.gui.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import com.zdsoft.datacenter.ASDatacenter;public class LJB_add extends ASDialog{ BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); LJB_show t1; 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 class_id = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField class_name = new JTextField(); JLabel jLabel4 = new JLabel(); JTextField class_teacher = new JTextField(); public LJB_add(LJB_show s) { t1 = s; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.setSize(new Dimension(262, 167));//(1)定义本对话框的大小(宽度,高度); this.getContentPane().setLayout(borderLayout1); jLabel1.setFont(new java.awt.Font("Serif", 0, 15)); jLabel1.setForeground(Color.magenta); jLabel1.setText("增加信息"); jButton1.setFont(new java.awt.Font("Serif", 0, 12)); jButton1.setText("取消"); jButton1.addActionListener(new LJB_add_jButton1_actionAdapter(this)); jButton2.setFont(new java.awt.Font("Serif", 0, 12)); jButton2.setText("确定"); jButton2.addActionListener(new LJB_add_jButton2_actionAdapter(this)); jPanel1.setLayout(gridBagLayout1); jLabel2.setFont(new java.awt.Font("Serif", 0, 12)); jLabel2.setMaximumSize(new Dimension(50, 18)); jLabel2.setMinimumSize(new Dimension(50, 18)); jLabel2.setPreferredSize(new Dimension(50, 18)); jLabel2.setHorizontalAlignment(SwingConstants.CENTER); jLabel2.setText("班级号码"); class_id.setMinimumSize(new Dimension(100, 25)); class_id.setPreferredSize(new Dimension(100, 25)); jLabel3.setFont(new java.awt.Font("Serif", 0, 12)); jLabel3.setMaximumSize(new Dimension(50, 18)); jLabel3.setMinimumSize(new Dimension(50, 18)); jLabel3.setPreferredSize(new Dimension(50, 18)); jLabel3.setHorizontalAlignment(SwingConstants.CENTER); jLabel3.setText("班级名称"); class_name.setMinimumSize(new Dimension(100, 25)); class_name.setPreferredSize(new Dimension(100, 25)); class_name.setText(""); class_name.addActionListener(new LJB_add_class_name_actionAdapter(this)); jLabel4.setFont(new java.awt.Font("Serif", 0, 12)); jLabel4.setMaximumSize(new Dimension(50, 18)); jLabel4.setMinimumSize(new Dimension(50, 18)); jLabel4.setPreferredSize(new Dimension(50, 18)); jLabel4.setHorizontalAlignment(SwingConstants.CENTER); jLabel4.setText("班主任"); class_teacher.setMinimumSize(new Dimension(100, 25)); class_teacher.setPreferredSize(new Dimension(100, 25)); class_teacher.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(0, 0, 0, 0), 0, 0)); jPanel1.add(class_id, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); jPanel1.add(jLabel3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jPanel1.add(class_name, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 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(class_teacher, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 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(); } void class_name_actionPerformed(ActionEvent e) { } void jButton2_actionPerformed(ActionEvent e) { //当点击“保存”按扭后: //(1)创建数据中心对象 ASDatacenter dc = new ASDatacenter(); //(2)连接数据库 dc.connect(); //(3)构造插入数据库数据的SQL语句 String sql = "insert into bj_dm(bj_id,bj_name,bj_teacher) values("+class_id.getText()+",'"+ class_name.getText()+"','"+class_teacher.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,"bj_id",new Integer(class_id.getText()));//调用ASDatastore类的setObjectAt方法,将增加的相应值的“对象” t1.ds.setObjectAt(row,"bj_name",class_name.getText()); t1.ds.setObjectAt(row,"bj_teacher",class_teacher.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(); } void jButton1_actionPerformed(ActionEvent e) { this.dispose(); }}class LJB_add_class_name_actionAdapter implements java.awt.event.ActionListener { LJB_add adaptee; LJB_add_class_name_actionAdapter(LJB_add adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.class_name_actionPerformed(e); }}class LJB_add_jButton2_actionAdapter implements java.awt.event.ActionListener { LJB_add adaptee; LJB_add_jButton2_actionAdapter(LJB_add adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}class LJB_add_jButton1_actionAdapter implements java.awt.event.ActionListener { LJB_add adaptee; LJB_add_jButton1_actionAdapter(LJB_add adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -