2013-04-09 214 views
1

我有一個JComponent在JPanel內部沒有出現問題。JPanel沒有顯示任何東西

我使用Netbeans的Java GUI生成器部分構建GUI。 我得到了一個與GUI生成器一起構建的MainFrame類來處理菜單項。 我有一個MainPanel類,它在MainFrame類(菜單欄下)中。

然後我手動添加另一個JPanel到該MainPanel,這樣我就可以在沒有Netbeans GUI的情況下再次完全控制GUI。

但是,當我添加一個JButton到該JPanel然後沒有顯示出來。

MainFrame.java(包括Netbeans的代碼):

package gui; 

import javax.swing.JFrame; 

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

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

    private void customInit() { 
     this.setTitle("Trading Card Game"); 
     this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    } 

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

     mainPanel1 = new gui.MainPanel(); 
     jMenuBar1 = new javax.swing.JMenuBar(); 
     menuFile = new javax.swing.JMenu(); 
     menuFileQuit = new javax.swing.JMenuItem(); 
     menuPreview = new javax.swing.JMenu(); 
     menuPreviewStart = new javax.swing.JMenuItem(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout mainPanel1Layout = new javax.swing.GroupLayout(mainPanel1); 
     mainPanel1.setLayout(mainPanel1Layout); 
     mainPanel1Layout.setHorizontalGroup(
      mainPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     mainPanel1Layout.setVerticalGroup(
      mainPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 279, Short.MAX_VALUE) 
     ); 

     menuFile.setText("File"); 

     menuFileQuit.setText("Quit"); 
     menuFileQuit.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       menuFileQuitActionPerformed(evt); 
      } 
     }); 
     menuFile.add(menuFileQuit); 

     jMenuBar1.add(menuFile); 

     menuPreview.setText("Preview"); 

     menuPreviewStart.setText("Start"); 
     menuPreview.add(menuPreviewStart); 

     jMenuBar1.add(menuPreview); 

     setJMenuBar(jMenuBar1); 

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

     pack(); 
    }// </editor-fold>//GEN-END:initComponents 

    private void menuFileQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileQuitActionPerformed 
     System.exit(0); 
    }//GEN-LAST:event_menuFileQuitActionPerformed 

    /** 
    * @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MainFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify//GEN-BEGIN:variables 
    private javax.swing.JMenuBar jMenuBar1; 
    private gui.MainPanel mainPanel1; 
    private javax.swing.JMenu menuFile; 
    private javax.swing.JMenuItem menuFileQuit; 
    private javax.swing.JMenu menuPreview; 
    private javax.swing.JMenuItem menuPreviewStart; 
    // End of variables declaration//GEN-END:variables 
} 

MainPanel.java(包括Netbeans的代碼):

package gui; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import model.Game; 

/** 
* 
* @author Frank 
*/ 
public class MainPanel extends javax.swing.JPanel { 

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

    private void initPanel() { 
     this.setBorder(BorderFactory.createLineBorder(Color.yellow)); 
     this.add(new JButton("Test"), BorderLayout.CENTER); 
     revalidate(); 
    } 

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

     setLayout(new java.awt.BorderLayout()); 
    }// </editor-fold>//GEN-END:initComponents 
    // Variables declaration - do not modify//GEN-BEGIN:variables 
    // End of variables declaration//GEN-END:variables 
} 

黃色邊框示出了圍繞所述的JPanel雖然。

任何線索爲什麼與「測試」的JButton沒有顯示出來?

圖片描述的情況(外觀無按鈕!): no button http://img4.imageshack.us/img4/1466/so1l.jpg

問候。

+0

另請參見[*我如何創建屏幕截圖?*](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a -post) – trashgod 2013-04-09 17:30:42

+0

你甚至想用這個說什麼?只是連接一些東西肯定不會解決一些我看不到的問題。 – skiwi 2013-04-09 17:32:00

+0

你的桌面是不相關的,並且使用'setExtendedState()'是多餘的。 – trashgod 2013-04-09 17:42:32

回答

2

這是因爲您正在覆蓋mainPanel1的佈局。

我注意到這些行MainFrame.java和測試按鈕顯示如預期。

//  javax.swing.GroupLayout mainPanel1Layout = new javax.swing.GroupLayout(mainPanel1); 
//  mainPanel1.setLayout(mainPanel1Layout); 
//  mainPanel1Layout.setHorizontalGroup(
//   mainPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
//   .addGap(0, 400, Short.MAX_VALUE) 
//  ); 
//  mainPanel1Layout.setVerticalGroup(
//   mainPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
//   .addGap(0, 279, Short.MAX_VALUE) 
//  ); 
+0

對不起,但我不明白。如果我是正確的,那麼在我的情況下,任何MainPanel實例都應該使用BorderLayout來擴展JLabel。 MainFrame類中的代碼如何影響MainPanel中的代碼是否工作? – skiwi 2013-04-09 17:38:59