2013-05-26 47 views
0

我是相當新的擺動,所以我相信這可能是簡單的。希望你們中的一些人能幫我一把。 這是一個更復雜的情況的一部分,但我設法把它放在一個簡單的例子。java swing組件不顯示內部面板

我正在使用Netbeans,我試圖創建一個包含一些複選框(由框架動態添加)的面板。

如果我使用可視化編輯器創建具有本地組件的整個結構,它可以很好地工作,但事情是我需要一個自定義面板,這是它的中斷點。

當我執行應用程序時,我只看到一個沒有內容的空白幀。

代碼如下

框架:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package contacorrente; 

import java.util.HashSet; 
import java.util.Set; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 

/** 
* 
* @author afsilva 
*/ 
public class Test extends javax.swing.JFrame { 

    /** 
    * Creates new form Test 
    */ 
    public Test() { 
     initComponents(); 
     this.testPanel1.add(new JCheckBox("ergcdf")); 
     this.testPanel1.add(new JCheckBox("ergcdf1")); 
     this.testPanel1.add(new JCheckBox("ergcdf2")); 
     this.testPanel1.add(new JCheckBox("ergcdf3")); 
     this.testPanel1.add(new JCheckBox("ergcdf4")); 
     this.testPanel1.validate(); 
     this.validate(); 
    } 

    /** 
    * 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() { 

     testPanel1 = new contacorrente.TestPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setBackground(new java.awt.Color(51, 204, 255)); 

     testPanel1.setBackground(new java.awt.Color(255, 51, 204)); 
     testPanel1.setName("testPanel1"); // NOI18N 

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

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

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

    /** 
    * @param args the command line arguments 
    */ 
    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(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Test.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 Test().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private contacorrente.TestPanel testPanel1; 
    // End of variables declaration 
} 

我不會粘貼板代碼,因爲它只是一個空的面板(沒有自定義代碼)

幫助,請..

在此先感謝

回答

0

JCheckBox ES尺寸爲0,0

您需要設置一個大小爲您JCheckBox ES

你舉的例子沒有顯示,但如果你改變你的構造函數是:

public Test() { 
    initComponents(); 
    JCheckBox x = new JCheckBox("ergcdf"); //create the component, 
    x.setSize(100,30);      //size the component 
    this.testPanel1.add(x);    //and then add it to the panel 
    this.testPanel1.add(new JCheckBox("ergcdf1")); 
    this.testPanel1.add(new JCheckBox("ergcdf2")); 
    this.testPanel1.add(new JCheckBox("ergcdf3")); 
    this.testPanel1.add(new JCheckBox("ergcdf4")); 
    this.testPanel1.validate(); 
    this.validate(); 
} 

它會顯示一個複選框。如果您將尺寸設置爲每個複選框,您將會看到所有這些尺寸

+0

您絕對是對的!這種行爲很奇怪,組件的默認大小不應該爲零! – cattox

+0

@cattox不,默認的組件大小是'0,0',如果你想看到它們,你需要設置它們的大小 – BackSlash

0

看看下面,它的工作原理。我不完全確定你錯過了什麼,因爲你做的有點不同,但你可以通過它看。我在想的是你沒有正確地聲明組件。如果你還沒有,我會建議Netbeans IDE做搖擺。 (Netbeans.org)

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package swingtest; 

/** 
* 
* @author 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 

    /** 
    * 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() { 

     jPanel1 = new javax.swing.JPanel(); 
     jCheckBox1 = new javax.swing.JCheckBox(); 
     jCheckBox2 = new javax.swing.JCheckBox(); 
     jCheckBox3 = new javax.swing.JCheckBox(); 
     jCheckBox4 = new javax.swing.JCheckBox(); 
     jCheckBox5 = new javax.swing.JCheckBox(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jCheckBox1.setText("ergcdf"); 

     jCheckBox2.setText("ergcdf1"); 

     jCheckBox3.setText("ergcdf2"); 

     jCheckBox4.setText("ergcdf3"); 

     jCheckBox5.setText("ergcdf4"); 

     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.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addContainerGap() 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jCheckBox1) 
          .addComponent(jCheckBox2)) 
         .addGap(18, 18, 18) 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jCheckBox4) 
          .addComponent(jCheckBox3))) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGap(38, 38, 38) 
         .addComponent(jCheckBox5))) 
       .addContainerGap(250, 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(jCheckBox1) 
        .addComponent(jCheckBox3)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jCheckBox2) 
        .addComponent(jCheckBox4)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jCheckBox5) 
       .addContainerGap(221, Short.MAX_VALUE)) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

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

    /** 
    * @param args the command line arguments 
    */ 
    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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JCheckBox jCheckBox1; 
    private javax.swing.JCheckBox jCheckBox2; 
    private javax.swing.JCheckBox jCheckBox3; 
    private javax.swing.JCheckBox jCheckBox4; 
    private javax.swing.JCheckBox jCheckBox5; 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration     
}