我正在開發一個桌面應用程序,我希望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;
}
輸出僅僅是一個空白幀。請幫我知道我錯在哪裏!
是的,這段代碼還沒有連接到數據庫,一旦它將工作,那麼我可以修改它與數據庫一起工作。
而且是他們任何其他佰來完成我的任務還是很正確的道路上`
謝謝你,有一個在Jframe中動態添加複選框的方式,請參見下文。 – Shaifali
我在搜索很多東西后得到了這個解決方案,但至少得到了它:) – Shaifali
@ user1932362是的,您可以將複選框動態添加到JFrame,上面的代碼顯示了它,但您必須更改佈局,正如我從GroupLayout所說的那樣如果你想使用簡單的添加(組件),別的東西。 – Jirka