2013-04-09 2 views
1

JPanel 내부에 JComponent가 표시되지 않는데 문제가 있습니다.JPanel에 아무것도 표시되지 않습니다.

Netbeans의 Java GUI 빌더를 사용하여 부분적으로 GUI를 빌드하고 있습니다. GUI 작성기를 사용하여 메뉴 항목을 관리하는 MainFrame 클래스가 있습니다. 메뉴 바 아래 MainFrame 클래스 안에있는 MainPanel 클래스가 있습니다.

그리고 나서 수동으로 다른 JPanel을 MainPanel에 추가하여 Netbeans GUI가 나를 괴롭히지 않고 GUI를 완전히 제어 할 수있게했습니다.

그러나 JButton을 JPanel에 추가하면 아무 것도 나타나지 않습니다.

(넷빈즈 코드 포함) MainFrame.java (넷빈즈 코드 포함)

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 :

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

+0

당신도 이걸로 말하려고합니까? 그냥 뭔가를 연결하면 확실히 볼 수없는 몇 가지 문제를 해결하지 않을 것입니다. – skiwi

+0

당신의 데스크탑은 무의미하며,'setExtendedState()'를 사용하는 것은 불필요합니다. – trashgod

답변

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

관련 문제