2012-12-03 202 views
1

我在NetBeans擴展JPanel類。該課程正在與「表單設計師」合作,您可以在屏幕上拖放GUI元素。我還有另一個類JFrame,它創建了這個類的一個實例並將其添加到其內容框架中。添加GUI設計的JPanel到JFrame中

JPanel類:

/** 
* Creates new form OpenPanel 
*/ 
public OpenPanel() { 
    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(); 
    jLabel2 = new javax.swing.JLabel(); 

    jLabel2.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N 
    jLabel2.setText("Jungle Tracks"); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
      .addContainerGap(87, Short.MAX_VALUE) 
      .addComponent(jLabel2) 
      .addGap(86, 86, 86)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
      .addContainerGap(21, Short.MAX_VALUE) 
      .addComponent(jLabel2) 
      .addGap(20, 20, 20)) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(0, 225, Short.MAX_VALUE)) 
    ); 
}// </editor-fold> 

// Variables declaration - do not modify 
private javax.swing.JLabel jLabel2; 
private javax.swing.JPanel jPanel1; 
// End of variables declaration 
} 

主要JFrame類(成我想要添加的JPanel),如下:

public class MainClass extends JFrame { 

static final int WIDTH = 800; 
static final int HEIGHT = 600; 

public MainClass() throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException { 
    super("MainClass"); 

    setSize(WIDTH, HEIGHT); 
    setBackground(Color.WHITE); 

    OpenPanel open = new OpenPanel(); 
    ((OpenPanel)open).setFocusable(true); 

      getContentPane().add(open); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    setVisible(true); 
} 

public static void main(String[] args) throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException { 
    MainClass mc = new MainClass(); 
} 

} 

但JPanel的,它是創建的UI元素在NetBean的設計視圖中未顯示,或者根據我的知識 - 甚至添加到JFrame。我究竟做錯了什麼?

+1

*「我有延長的JPanel ..延伸的JFrame ..另一個類的類」 *不要延長或者除非添加功能。只需使用一個實例並添加組件。 –

回答

3

我猜你的paint(...)方法不會調用它內部的super.paint(...)方法,所以JPanel沒有很好地繪製它自己的組件。如果是這樣的話,那麼我建議:

  • 不要覆蓋paint(...)因爲這是很少需要,以及如果這樣做,必須特別小心,因爲這種方法是負責畫不僅組成部分,也是它的邊界和其子組件 - 您似乎遇到的問題。
  • 而是覆蓋paintComponent(...)但一定要調用super.paintComponent(...)在這種方法中,通常作爲方法的第一行。