2012-12-12 46 views
0

當我編寫一個用於顯示文本字段的代碼時,標籤等事件說按鈕單擊它的工作正常,如果我編寫整個代碼並且不起作用,如果我拖放任何來自netbeans的組件。從IDE構建時顯示動態組件:Java

我該如何解決這個問題?

(用代碼更新)


Netbeans的代碼

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

import javax.swing.JLabel; 

/** 
* 
* @author chiyaIDE 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     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">//GEN-BEGIN:initComponents 
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     panel = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

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

     javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); 
     panel.setLayout(panelLayout); 
     panelLayout.setHorizontalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 
     panelLayout.setVerticalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(117, 117, 117) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jButton1)) 
       .addContainerGap(183, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(71, 71, 71) 
       .addComponent(jButton1) 
       .addGap(26, 26, 26) 
       .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(80, Short.MAX_VALUE)) 
     ); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed 
     // TODO add your handling code here: 

     panel.add(new JLabel("Hello")); 
     panel.revalidate(); 
     System.out.println(i++); 
    }//GEN-LAST:event_jButton1ActionPerformed 

    /** 
    * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    public static int i; 
    // Variables declaration - do not modify//GEN-BEGIN:variables 
    private javax.swing.JButton jButton1; 
    private javax.swing.JPanel panel; 
    // End of variables declaration//GEN-END:variables 
} 

手冊代碼

public static void paste() 
{ 

    frame=new JFrame(); 
    panel=new JPanel(); 
    frame.setSize(400,400); 
    frame.setVisible(true); 


    panel.setSize(400,400); 

    Container cp=frame.getContentPane(); 
    cp.add(panel); 

    JButton button=new JButton("Click HERE"); 
    panel.add(button); 

    button.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 

      panel.add(new JLabel("Hello")); 
      panel.revalidate(); 
     } 
    }); 

} 
+0

你應該更具體嗎? – Equo

+0

您嘗試過的任何代碼...顯示某些內容不起作用? – acostache

+0

從NetBeans中 [鏈接] http://imageshack.us/photo/my-images/13/idew.jpg/ ... 作品手動 [鏈接] http://imageshack.us/photo/ my-images/4/manual1v.jpg/ 。 [鏈接] http://imageshack.us/photo/my-images/822/manual2k.jpg/ –

回答

1
問題

在佈局管理器。我不喜歡netbeans的GUI設計師,我更喜歡做自己的設計,這是爲什麼很多原因之一:

你用設計器創建了一些基本的GUI。稍後,您想要添加下一個組件,但由於佈局而不「可見」。默認爲「免費設計」,即GroupLayout。將其更改爲其他適當的圖層或爲JLabel指定位置。

+0

謝謝大家:) 它的工作。 - > panel.setLayout(new FlowLayout()); –