2016-04-21 39 views
1

我剛剛在NetBeans上創建了一個簡單項目並創建了一個GUI。當我運行該項目時,出現一個窗口,但它只是一個空框。我沒有改變代碼中的任何東西,只是使用拖放在NetBeans中創建GUI。JFrame顯示但爲空

package twittersearch; 

public class TwitterSearchUI extends javax.swing.JFrame { 

public TwitterSearchUI() { 
    initComponents(); 
} 

@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jButton1 = new javax.swing.JButton(); 
    jTextField1 = new javax.swing.JTextField(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTextArea1 = new javax.swing.JTextArea(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jButton1.setText("Search"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jTextField1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jTextField1ActionPerformed(evt); 
     } 
    }); 

    jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Results")); 

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

    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() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jButton1))) 
      .addContainerGap(26, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jButton1) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(19, Short.MAX_VALUE)) 
    ); 

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

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           

}           

public static void main(String args[]) { 
    //<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(TwitterSearchUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TwitterSearchUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TwitterSearchUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TwitterSearchUI.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 TwitterSearchUI().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTextArea jTextArea1; 
private javax.swing.JTextField jTextField1; 
// End of variables declaration     
} 
+0

我剛剛捕獲的同一圖像包括以下杜林的Derb。代碼中沒有任何更改會生成運行您的類的GUI。 – RubioRic

回答

2

用的正是這種代碼,我得到這個:

enter image description here

+0

我必須將其設置不正確,我將我的代碼複製到一個新項目中,並且工作正常。謝謝你的提示 :) – Alfie

-1

您是否設置了一些尺寸(寬度,高度)?