日期:2014-05-20 浏览次数:20947 次
import java.awt.*; import javax.swing.*; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Frame1 extends JFrame { JPanel contentPane; JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); public Frame1() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(null); setSize(new Dimension(400, 300)); setTitle("Frame Title"); jButton1.setBounds(new Rectangle(37, 44, 120, 28)); jButton1.setText("学校信息录入"); jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this)); jButton2.setBounds(new Rectangle(40, 84, 117, 29)); jButton2.setText("学生信息录入"); jButton3.setBounds(new Rectangle(41, 127, 115, 29)); jButton3.setText("学校设备录入"); jButton4.setBounds(new Rectangle(40, 178, 118, 32)); jButton4.setText("操作人员录入"); contentPane.add(jButton1); contentPane.add(jButton2); contentPane.add(jButton3); contentPane.add(jButton4); } public void jButton1_actionPerformed(ActionEvent e) { Dialog1 dlg1=new Dialog1(); dlg1.setVisible(true); } } class Frame1_jButton1_actionAdapter implements ActionListener { private Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } } 这是jdialog import java.awt.*; import javax.swing.*; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; public class Dialog1 extends JDialog { JPanel panel1 = new JPanel(); JTextField jTextField1 = new JTextField(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JComboBox jComboBox1 = new JComboBox(); JLabel jLabel3 = new JLabel(); JTextField jTextField2 = new JTextField(); JLabel jLabel4 = new JLabel(); JTextField jTextField3 = new JTextField(); JLabel jLabel5 = new JLabel(); JTextField jTextField4 = new JTextField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); private String driverstr="sun.jdbc.odbc.JdbcOdbcDriver"; private String connStr="jdbc:odbc:test",url="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ= C:\\Documents and Settings\\test.mdb"; private Connection conn; private PreparedStatement pstmt; private ResultSet rs; public Dialog1(Frame owner, String title, boolean modal) { super(owner, title, modal); try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); } catch (Exception exception) { exception.printStackTrace(); } } public Dialog1() { this(new Frame(), "Dialog1", false); } private void jbInit() throws Exception { panel1.setLayout(null); this.getContentPane().setLayout(null); jTextField1.setBounds(new Rectangle(127, 41, 226, 20)); jLabel1.setText("学校名称"); jLabel1.setBounds(new Rectangle(41, 41, 82, 20)); jLabel2.setText("学校所在区"); jLabel2.setBounds(new Rectangle(41, 92, 70, 23)); jComboBox1.setBounds(new Rectangle(127, 92, 128, 23)); jComboBox1.addItem("a"); jLabel3.setText("学校地址"); jLabel3.setBounds(new Rectangle(41, 132, 72, 17)); jTextField2.setBounds(new Rectangle(127, 129, 224, 20)); jLabel4.setText("学校联系电话"); jLabel4.setBounds(new Rectangle(41, 174, 78, 15)); jTextField3.setBounds(new Rectangle(127, 173, 224, 20)); jLabel5.setText("学校校长"); jLabel5.setBounds(new Rectangle(42, 204, 70, 20)); jTextField4.setBounds(new Rectangle(127, 204, 163, 20)); jButton1.setBounds(new Rectangle(41, 248, 81, 23)); jButton1.setText("保存"); jButton1.addActionListener(new Dialog1_jButton1_actionAdapter(this)); jButton2.setBounds(new Rectangle(225, 248, 81, 23)); jButton2.setText("重置"); this.getContentPane().add(panel1, null); panel1.add(jLabel1); panel1.add(jTextField1); panel1.add(jLabel2); panel1.add(jComboBox1); panel1.add(jLabel3); panel1.add(jTextField2); panel1.add(jLabel4); panel1.add(jTextField3); panel1.add(jLabel5); panel1.add(jTextField4); panel1.add(jButton1); panel1.add(jButton2); panel1.setBounds(new Rectangle(0, 0, 400, 300)); this.pack(); //pack根据容器内的组件以最优的方式调整容器的大小 this.setVisible(true);//setVisible显示当前窗口 } public void jButton1_actionPerformed(ActionEvent e) { try{ Class.forName(driverstr); } catch(ClassNotFoundException ex) { System.out.println(ex.getMessage()); } try { String sql1="insert into schoolinfo values(?,?,?,?,?)"; String s1=jTextField1.getText().trim().toString(); String s2=(String)jComboBox1.getSelectedItem().toString(); String s3=jTextField2.getText().trim().toString(); String s4=jTextField3.getText().trim().toString(); String s5=jTextField4.getText().trim().toString(); conn = DriverManager.getConnection(url); pstmt=conn.prepareStatement(sql1); this.pstmt.setString(1,s1); this.pstmt.setString(2,s2); this.pstmt.setString(3,s3); this.pstmt.setString(4,s4); this.pstmt.setString(5,s5); this.pstmt.executeUpdate(); new JOptionPane().showMessageDialog(null,"启动"); // System.out.println("11"); } catch (SQLException ex1) { } } } class Dialog1_jButton1_actionAdapter implements ActionListener { private Dialog1 adaptee; Dialog1_jButton1_actionAdapter(Dialog1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } }