2013-04-09 178 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