⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordtypejdialog.java

📁 电子通讯录实现网上保存朋友
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * RecordTypeJDialog.java * * Created on 2008-12-23, 16:30:38 */package frame;import entity.RecordType;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.DefaultListModel;import javax.swing.JComboBox;import javax.swing.JOptionPane;import javax.swing.ListSelectionModel;import utils.DBUtils;import utils.StringUtils;/** * * @author Jason */public class RecordTypeJDialog extends javax.swing.JDialog {    /** Creates new form RecordTypeJDialog */    public RecordTypeJDialog(java.awt.Frame parent, boolean modal) {        super(parent, modal);        initComponents();        init();    }    /**     *     */    private void init(){        this.jList1.setSelectionMode(                ListSelectionModel.SINGLE_SELECTION);        this.jList1.setModel(new DefaultListModel());        RecordJFrame rj = (RecordJFrame) this.getParent();        JComboBox typeCmb = rj.getTypeComboBox();        if(typeCmb.getItemCount() == 0){            JOptionPane.showMessageDialog(this,                    "还没有任何记录分类,请添加!");        }else{            int size = typeCmb.getItemCount();            DefaultListModel dlm =                  (DefaultListModel) this.jList1.getModel();            for(int i=0;i<size;i++){                dlm.addElement(typeCmb.getItemAt(i));            }        }    }    /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    @SuppressWarnings("unchecked")    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jPanel1 = new javax.swing.JPanel();        jLabel1 = new javax.swing.JLabel();        jTextField1 = new javax.swing.JTextField();        jScrollPane1 = new javax.swing.JScrollPane();        jList1 = new javax.swing.JList();        jButton1 = new javax.swing.JButton();        jButton2 = new javax.swing.JButton();        jButton3 = new javax.swing.JButton();        jButton4 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);        setTitle("记录分类管理");        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));        jLabel1.setText("记录分类管理");        jList1.addMouseListener(new java.awt.event.MouseAdapter() {            public void mouseClicked(java.awt.event.MouseEvent evt) {                jList1MouseClicked(evt);            }        });        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {                jList1ValueChanged(evt);            }        });        jScrollPane1.setViewportView(jList1);        jButton1.setText("添 加");        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(evt);            }        });        jButton2.setText("更 新");        jButton3.setText("删 除");        jButton4.setText("退 出");        jButton4.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton4ActionPerformed(evt);            }        });        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);        jPanel1.setLayout(jPanel1Layout);        jPanel1Layout.setHorizontalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel1Layout.createSequentialGroup()                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()                        .addComponent(jLabel1)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))        );        jPanel1Layout.setVerticalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel1Layout.createSequentialGroup()                .addContainerGap()                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel1)                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jButton1))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addGroup(jPanel1Layout.createSequentialGroup()                        .addComponent(jButton2)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(jButton3)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                        .addComponent(jButton4)))                .addContainerGap(49, Short.MAX_VALUE))        );        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed        // TODO add your handling code here:        this.dispose();        RecordJFrame recordJFrame =                (RecordJFrame) this.getParent();        JComboBox typeComboBox =                recordJFrame.getTypeComboBox();        typeComboBox.removeAllItems();        DefaultListModel dlm =                (DefaultListModel) this.jList1.getModel();        int size = dlm.getSize();        for(int i=0;i<size;i++){            typeComboBox.addItem(dlm.get(i));        }            }//GEN-LAST:event_jButton4ActionPerformed    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed        // TODO add your handling code here:        String typename = this.jTextField1.getText();        if(StringUtils.isNullOrEmptyWithSpacesOnly(typename)){            JOptionPane.showMessageDialog(this, "记录类别不能为空!"                    ,"警告",JOptionPane.WARNING_MESSAGE);            return;        }        Connection con = DBUtils.getConnection();        Statement stmt = null;        try {            stmt = con.createStatement();            String sql = "insert into t_record_type (typename) " +                    "values ('" + typename + "')";            if(stmt.executeUpdate(sql) == 1){                JOptionPane.showMessageDialog(                        this, "记录分类插入成功!");                DefaultListModel dlt =                        (DefaultListModel) this.jList1.getModel();                RecordType rt = new RecordType();                rt.setTypename(typename);                sql = "select max(typeid) from t_record_type";                ResultSet rs = stmt.executeQuery(sql);                rs.next();                rt.setTypeid(rs.getInt(1));                dlt.addElement(rt);                this.jTextField1.setText("");            }else{                JOptionPane.showMessageDialog(this, "记录分类插入失败!");            }        } catch (SQLException ex) {            Logger.getLogger(RecordTypeJDialog.class.getName()).log(Level.SEVERE, null, ex);        } finally{            DBUtils.closeAll(stmt, con);        }    }//GEN-LAST:event_jButton1ActionPerformed    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked        // TODO add your handling code here:       this.getChangedListValue();    }//GEN-LAST:event_jList1MouseClicked    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged        // TODO add your handling code here:        this.getChangedListValue();    }//GEN-LAST:event_jList1ValueChanged    private void getChangedListValue(){     RecordType recordType =                 (RecordType) this.jList1                 .getSelectedValue();        this.jTextField1.setText(                recordType.toString());    }    /**    * @param args the command line arguments    */       // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JButton jButton3;    private javax.swing.JButton jButton4;    private javax.swing.JLabel jLabel1;    private javax.swing.JList jList1;    private javax.swing.JPanel jPanel1;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTextField jTextField1;    // End of variables declaration//GEN-END:variables}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -