2012-10-18 35 views
1

嗯,我似乎在這裏遇到了一個奇怪的問題,我相信我曾經解決,但對我的生活不記得了。我有一個JFrame,其中包含一個JPanel,並且我試圖將另一個JPanel添加到現有的JPanel。但是,什麼也沒有顯示我已經調用setVisible()& repaint(),但仍然是不行。JFrames和JPanels

有人在乎這件事嗎?

這裏是我的代碼:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package bakarangerx; 
import javax.swing.*; 
import java.awt.*; 
/** 
* 
* @author Ryan 
*/ 
public class MainForm extends javax.swing.JFrame { 

    /** 
    * Creates new form MainForm 
    */ 
    public MainForm() { 
     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() { 

     FileChooser_Open = new javax.swing.JFileChooser(); 
     FileChooser_Save = new javax.swing.JFileChooser(); 
     Panel_Main = new javax.swing.JPanel(); 
     jMenuBar1 = new javax.swing.JMenuBar(); 
     jMenu1 = new javax.swing.JMenu(); 
     MenuItem_New = new javax.swing.JMenuItem(); 
     MenuItem_Open = new javax.swing.JMenuItem(); 
     MenuItem_Save = new javax.swing.JMenuItem(); 
     jMenu2 = new javax.swing.JMenu(); 

     FileChooser_Open.setDialogTitle("Open BakaRangerX File..."); 
     FileChooser_Open.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       FileChooser_OpenMouseClicked(evt); 
      } 
     }); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     Panel_Main.setLayout(new java.awt.GridLayout()); 

     jMenu1.setText("File"); 

     MenuItem_New.setText("New"); 
     MenuItem_New.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       MenuItem_NewMouseClicked(evt); 
      } 
     }); 
     jMenu1.add(MenuItem_New); 

     MenuItem_Open.setText("Open"); 
     MenuItem_Open.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       MenuItem_OpenMouseClicked(evt); 
      } 
     }); 
     MenuItem_Open.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       MenuItem_OpenActionPerformed(evt); 
      } 
     }); 
     jMenu1.add(MenuItem_Open); 

     MenuItem_Save.setText("Save"); 
     MenuItem_Save.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       MenuItem_SaveMouseClicked(evt); 
      } 
     }); 
     jMenu1.add(MenuItem_Save); 

     jMenuBar1.add(jMenu1); 

     jMenu2.setText("Edit"); 
     jMenuBar1.add(jMenu2); 

     setJMenuBar(jMenuBar1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(Panel_Main, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(Panel_Main, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 

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

    private void MenuItem_OpenActionPerformed(java.awt.event.ActionEvent evt) {            
     //FileChooser_Open.showOpenDialog(this); 
     ExaminerPanel newPanel = new ExaminerPanel(); 
     newPanel.setMinimumSize(new Dimension(200, 200)); 
     newPanel.setVisible(true); 
     Panel_Main.setVisible(true); 
     Panel_Main.add(newPanel); 
     newPanel.repaint(); 
     Panel_Main.repaint(); 
     this.repaint(); 

    }            

    private void MenuItem_NewMouseClicked(java.awt.event.MouseEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void MenuItem_SaveMouseClicked(java.awt.event.MouseEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void MenuItem_OpenMouseClicked(java.awt.event.MouseEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void FileChooser_OpenMouseClicked(java.awt.event.MouseEvent evt) {            
     if(!FileChooser_Open.getSelectedFile().equals(null)) 
     { 



     } 
    }            

    /** 
    * @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(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(MainForm.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 MainForm().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JFileChooser FileChooser_Open; 
    private javax.swing.JFileChooser FileChooser_Save; 
    private javax.swing.JMenuItem MenuItem_New; 
    private javax.swing.JMenuItem MenuItem_Open; 
    private javax.swing.JMenuItem MenuItem_Save; 
    private javax.swing.JPanel Panel_Main; 
    private javax.swing.JMenu jMenu1; 
    private javax.swing.JMenu jMenu2; 
    private javax.swing.JMenuBar jMenuBar1; 
    // End of variables declaration 
} 




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

/** 
* 
* @author Ryan 
*/ 
public class ExaminerPanel extends javax.swing.JPanel { 

    /** 
    * Creates new form Examiner 
    */ 
    public ExaminerPanel() { 
     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() { 

     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     Button_Previous = new javax.swing.JButton(); 
     Button_Next = new javax.swing.JButton(); 
     Button_Check = new javax.swing.JButton(); 
     jLabel3 = new javax.swing.JLabel(); 
     jLabel4 = new javax.swing.JLabel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     TextArea_CorrectAnswer = new javax.swing.JTextArea(); 
     jScrollPane2 = new javax.swing.JScrollPane(); 
     TextArea_Question = new javax.swing.JTextArea(); 
     jLabel5 = new javax.swing.JLabel(); 
     jScrollPane3 = new javax.swing.JScrollPane(); 
     TextArea_Answer = new javax.swing.JTextArea(); 
     jLabel6 = new javax.swing.JLabel(); 
     jLabel7 = new javax.swing.JLabel(); 
     jLabel8 = new javax.swing.JLabel(); 
     jLabel9 = new javax.swing.JLabel(); 
     jLabel10 = new javax.swing.JLabel(); 
     jLabel11 = new javax.swing.JLabel(); 
     jLabel12 = new javax.swing.JLabel(); 
     jLabel13 = new javax.swing.JLabel(); 
     jLabel14 = new javax.swing.JLabel(); 
     jLabel15 = new javax.swing.JLabel(); 

     jLabel1.setText("Question"); 

     jLabel2.setText("Correct Answer"); 

     Button_Previous.setText("Previous"); 

     Button_Next.setText("Next"); 

     Button_Check.setText("Check"); 

     jLabel3.setText("Wrong"); 

     jLabel4.setText("Right"); 

     TextArea_CorrectAnswer.setColumns(20); 
     TextArea_CorrectAnswer.setRows(5); 
     jScrollPane1.setViewportView(TextArea_CorrectAnswer); 

     TextArea_Question.setColumns(20); 
     TextArea_Question.setRows(5); 
     jScrollPane2.setViewportView(TextArea_Question); 

     jLabel5.setText("Answer"); 

     TextArea_Answer.setColumns(20); 
     TextArea_Answer.setRows(5); 
     jScrollPane3.setViewportView(TextArea_Answer); 

     jLabel6.setText("Total Time"); 

     jLabel7.setText("Time"); 

     jLabel8.setText("jLabel8"); 

     jLabel9.setText("jLabel9"); 

     jLabel10.setText("jLabel10"); 

     jLabel11.setText("jLabel11"); 

     jLabel12.setText("jLabel12"); 

     jLabel13.setText("Grade"); 

     jLabel14.setText("jLabel14"); 

     jLabel15.setText("%"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jLabel2) 
          .addComponent(jLabel1)) 
         .addGap(18, 18, 18) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jScrollPane2) 
          .addComponent(jScrollPane1))) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(0, 0, Short.MAX_VALUE) 
         .addComponent(jLabel13) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel12) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel9) 
         .addGap(18, 18, 18) 
         .addComponent(jLabel4) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel11) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel7) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel10)) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
           .addComponent(jLabel5) 
           .addGap(57, 57, 57) 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
            .addGroup(layout.createSequentialGroup() 
             .addComponent(Button_Previous) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
             .addComponent(Button_Next)) 
            .addComponent(jScrollPane3))) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(0, 0, Short.MAX_VALUE) 
           .addComponent(Button_Check))) 
         .addGap(19, 19, 19)) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
         .addComponent(jLabel6) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel8) 
         .addGap(76, 76, 76))) 
       .addContainerGap()) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(318, 318, 318) 
       .addComponent(jLabel15) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jLabel14) 
       .addContainerGap(430, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel6) 
        .addComponent(jLabel8)) 
       .addGap(16, 16, 16) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel14) 
        .addComponent(jLabel15)) 
       .addGap(17, 17, 17) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(jLabel4) 
        .addComponent(jLabel7) 
        .addComponent(jLabel9) 
        .addComponent(jLabel10) 
        .addComponent(jLabel11) 
        .addComponent(jLabel12) 
        .addComponent(jLabel13)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jLabel2) 
          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(22, 22, 22) 
         .addComponent(Button_Check) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jLabel5) 
          .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(Button_Next) 
          .addComponent(Button_Previous))) 
        .addComponent(jLabel1)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
    }// </editor-fold>       
    // Variables declaration - do not modify      
    private javax.swing.JButton Button_Check; 
    private javax.swing.JButton Button_Next; 
    private javax.swing.JButton Button_Previous; 
    private javax.swing.JTextArea TextArea_Answer; 
    private javax.swing.JTextArea TextArea_CorrectAnswer; 
    private javax.swing.JTextArea TextArea_Question; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel10; 
    private javax.swing.JLabel jLabel11; 
    private javax.swing.JLabel jLabel12; 
    private javax.swing.JLabel jLabel13; 
    private javax.swing.JLabel jLabel14; 
    private javax.swing.JLabel jLabel15; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel jLabel5; 
    private javax.swing.JLabel jLabel6; 
    private javax.swing.JLabel jLabel7; 
    private javax.swing.JLabel jLabel8; 
    private javax.swing.JLabel jLabel9; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    // End of variables declaration     
} 
+3

嘗試使用導入而不是全名。還要刪除IDE生成的評論。 – quantum

+2

如果你將這段代碼削減到最小的例子來展示你的問題,如果你在代碼片段中加入一點點的努力,你會得到一個更快更好的答案。 – sjr

+0

這是超過200行代碼。任何人都不會閱讀它。如果你可以提供一個你正在做什麼和問題的簡明代碼,我們可以嘗試... – Nivas

回答

2

嘗試使用Component#validate代替

//FileChooser_Open.showOpenDialog(this); 
ExaminerPanel newPanel = new ExaminerPanel(); 
Panel_Main.add(newPanel); 
Panel_Main.validate(); 
Panel_Main.repaint(); 
+0

好吧,現在向我解釋爲什麼這是有效的。 ;-) – user978122

+0

Swing有時可能會自我調整,有時需要一點點「微調」。向容器添加組件會使該容器「無效」,但除非該容器觸發某種級聯備份容器層次結構,否則可能不會觸發該組件的重新驗證。 'validate' *驗證容器意味着佈置其子組件*和'repaint'只是要求重繪管理器爲此容器安排一個重繪事件 – MadProgrammer

1

究竟是什麼你想達到兩個板是什麼?我在eclipse中複製了你的代碼,只是因爲它被打開了,正如他們所說的那樣,代碼很長。我看到的是帶有菜單的單個框架+面板。我知道你綁定顯示面板到「打開」菜單命令,所以也許你需要添加一個驗證到你的第二次面板調用,如果你想把它放在第一個面板上?或者如果你做一些更復雜的事情,可以使用佈局。

+0

老實說,我試圖做的只是將一個面板載入另一個面板。我並不知道需要任何欺騙手段才能讓它顯示出來,但我爲什麼不這麼做。 – user978122

+0

我認爲MadProgrammer爲重新驗證組件提供了一個非常好的解釋。我只是問你對這個小組有什麼意圖,因爲在我看來,佈局是處理這個問題的更好的方式。 – seninp