2016-04-20 101 views
0

gui windows不會在java中顯示。我有java,netbeans,java jdk。我測試jdk和java打開.jar文件,它的工作原理..這裏是完整的代碼gui窗口不會在netbeans中顯示java

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author swipeales 
*/ 
public class gui extends javax.swing.JPanel { 

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

     jPanel1 = new javax.swing.JPanel(); 
     labelTittle = new javax.swing.JLabel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jPasswordField1 = new javax.swing.JPasswordField(); 
     jLabel1 = new javax.swing.JLabel(); 

     labelTittle.setText("Title"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(labelTittle, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(525, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(labelTittle) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     jLabel1.setText("Password: "); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jLabel1) 
       .addGap(57, 57, 57) 
       .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(232, 232, 232)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() 
       .addContainerGap(81, Short.MAX_VALUE) 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel1)) 
       .addGap(37, 37, 37)) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(0, 286, Short.MAX_VALUE)) 
     ); 
    }// </editor-fold>       

    // ... 


    public static void main(String args[]) { 

     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new gui().setVisible(true); 
      } 
     } 
     ); 
    } 


    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JPasswordField jPasswordField1; 
    private javax.swing.JLabel labelTittle; 
    // End of variables declaration     
} 

回答

1

你定義的是一個JPanel。 JPanel不是頂級組件。它不能單獨顯示,但必須添加到JFrame中。

通過

gui gui = new gui(); 
JFrame f = new JFrame(); 
f.add(gui); 
f.pack(); 
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
f.setVisible(true); 

更換你的run方法的身體和請給比gui一個更好的名字到類(類似PasswordPanel,例如),並尊重的Java命名約定:類以大寫信。

+0

謝謝我應該創建JFrame表格 – swipeales

1

取而代之的是:

public void run() { 
    new gui().setVisible(true); 
} 

把它放在一個JFrame第一,的JPanel本身並不能顯示。例如:

public void run() { 
    JFrame frame = new JFrame("your window"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(new gui()); 
    frame.pack(); 
    frame.setVisible(true); 
} 

順便說一句,類名應該以大寫字母爲。因此,您可能需要將gui類別命名爲GuiGUI

+0

謝謝我應該創建JFrame表單 – swipeales