2013-06-24 43 views
2

我正在開發一個桌面應用程序,我希望Admin可以選擇刪除用戶,爲此我計劃只要Admin單擊「刪除用戶」按鈕,將打開一個新選項卡,其中的複選框將與我的數據庫中所有現有用戶的名稱應該出現(以便他可以同時刪除多個用戶);所以基本上我需要根據我的數據庫生成動態複選框。動態生成複選框,Netbeans

我使用Netbeans 7.0.1,jdk 1.6,sqlite3。

在谷歌搜索後,我得到了兩個環節,其匹配到我的問題: http://www.coderanch.com/t/345949/GUI/java/create-dynamic-checkboxes#2805277

Creating dcheckbox dynamically in Java-NetBeans

我曾試圖按照上面第一個鏈接的代碼,但它不能正常工作對我來說。我確實在NetBeans是剛剛創建新的JFrame,並呼籲內按需要其創建複選框的構造方法,方法的代碼如下:

public class Work extends javax.swing.JFrame { 

    /** Creates new form Work */ 
    public Work() { 
     initComponents(); 
     checks = new java.util.ArrayList<>(); 
     createCheckboxes(); 
    } 

    /** 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"> 
    private void initComponents() { 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void createCheckboxes(){ 
     panel = new javax.swing.JPanel(); 
     this.add(panel); 
     for(int i = 0; i<4; i++){ 
      javax.swing.JCheckBox box = new javax.swing.JCheckBox("check"+i); 
      panel.add(box); 
      checks.add(box); 
      panel.revalidate(); 
      panel.repaint(); 
     } 
     panel.setVisible(true); 
     } 

    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new Work().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    // End of variables declaration 
    private java.util.ArrayList <javax.swing.JCheckBox> checks; 
    private javax.swing.JPanel panel; 
} 

輸出僅僅是一個空白幀。請幫我知道我錯在哪裏!

是的,這段代碼還沒有連接到數據庫,一旦它將工作,那麼我可以修改它與數據庫一起工作。

而且是他們任何其他佰來完成我的任務還是很正確的道路上`

回答

1

你,如果你的框架是用一些簡單的佈局,如FlowLayout中添加新的複選框,但它不是 - 它是使用GroupLayout - 查看生成的initComponents()方法。

如果要動態地處理幀中的所有組件,可以這樣做(最好創建一個空的類文件,然後粘貼下面的代碼;不要要求NB創建JFrame,因爲它會再次創建在可視化設計器來設計一種形式;如果你仍然這樣做,則r單擊它,改變佈局,以更簡單的東西):

public class Work extends javax.swing.JFrame { 
    private java.util.List <javax.swing.JCheckBox> checks = new java.util.ArrayList<>();; 

    public Work() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new java.awt.FlowLayout()); // simply put the components next to each other 
     createCheckboxes(); 
    } 

    private void createCheckboxes(){ 
     for(int i=0; i<4; i++) { 
      javax.swing.JCheckBox box = new javax.swing.JCheckBox("check"+i); 
      add(box); 
      checks.add(box); 
     } 
     pack(); // this will tell the JFrame's panel to layout all the components 
     } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Work().setVisible(true); 
      } 
     }); 
    } 
} 

或者你可以用可視化設計,然後設計自己的框架的一部分添加複選框。在這種情況下,在設計器中添加一個空面板,將面板的佈局設置爲流或網格佈局,然後以與上面相同的方式將代碼複選框添加到代碼中。

只有當面板/框架已經可見時,您才需要調用驗證。調用包甚至可以工作,但可能會改變幀的大小。也可以在添加所有組件之後進行驗證,而不是在添加每個組件之後進行驗證。

+0

謝謝你,有一個在Jframe中動態添加複選框的方式,請參見下文。 – Shaifali

+0

我在搜索很多東西后得到了這個解決方案,但至少得到了它:) – Shaifali

+0

@ user1932362是的,您可以將複選框動態添加到JFrame,上面的代碼顯示了它,但您必須更改佈局,正如我從GroupLayout所說的那樣如果你想使用簡單的添加(組件),別的東西。 – Jirka

1

要添加複選框或動態任何其他成分在Netbeans的JFrame的一個需要管理的佈局管理器,默認情況下NetBeans的幀使用免費設計佈局,請按照下列步驟操作:

創建空白的JFrame - >添加的JPanel它 - >右鍵單擊面板,選擇setLayout並將其更改爲GridLayout。

現在我們可以在此面板上自由添加螞蟻組件。

也不要forgate添加revalidate()和repaint()方法。

這對我有效。

+0

只有當面板已經可見時,您才需要調用重新驗證,在您的代碼中,您已添加複選框之後使其可見*。在添加完所有複選框後,我還會調用重新驗證。 – Jirka

2

我想如果u調用下面的函數,只要到想創建一個新的複選框它可以幫助..

公共類的CheckBox擴展的JFrame {

//private static final long serialVersionUID = 1L; 
    public CheckBox() { 

     // set flow layout for the frame 
     this.getContentPane().setLayout(new FlowLayout(FlowLayout.TRAILING, 50, 20)); //(default) centered alignment and a default 5-unit horizontal and vertical gap. 

     JCheckBox checkBox1 = new JCheckBox("Checkbox 1"); 
     checkBox1.setSelected(true); 

     JCheckBox checkBox2 = new JCheckBox("Checkbox 2", true); 

     JCheckBox checkBox3 = new JCheckBox("Checkbox 3"); 

     // add checkboxes to frame 

     add(checkBox1); 

     add(checkBox2); 
     add(checkBox3); 

    } 

    private static void createAndShowGUI() { 

     //Create and set up the window. 

     //JFrame frame = new CreateCheckedUncheckedJCheckBox(); 

     CheckBox cb = new CheckBox(); 
     //Display the window. 

     cb.pack(); 



     cb.setVisible(true); 

     cb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public static void main(String[] args) { 

     //Schedule a job for the event-dispatching thread: 

     //creating and showing this application's GUI. 

     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 

       createAndShowGUI(); 

      } 
     }); 
    } 

}