2012-08-08 80 views
0

我正在嘗試使用Netbeans IDE 7.1構建一個Java項目。NetBeans Java項目不會顯示GUI窗口儘管沒有錯誤

我以某種方式無法看到或查看我創建的GUI窗口。

請提醒。

在我的課:

package rmiSimpleCalc; 

public class RMISimpleCalculatorMain { 

    public static void main(String[] args) { 
     MainCalculator calc = new MainCalculator(); 
     calc.setVisible(true); 
    } 
} 

MainCalculator是GUI窗口我想運行。它以某種方式不會顯示。

有一個在我的控制檯艱難NO錯誤信息..

這裏是MainCalculator代碼:

package rmiSimpleCalc; 

import java.rmi.*; 

public class MainCalculator extends javax.swing.JPanel { 

    public MainCalculator() { 
     initComponents(); 
     ComboBoxOperator.addItem("+"); 
     ComboBoxOperator.addItem("-"); 
     ComboBoxOperator.addItem("/"); 
     ComboBoxOperator.addItem("*");    
    } 

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

     txtFirstDigit = new javax.swing.JTextField(); 
     txtSecondDigit = new javax.swing.JTextField(); 
     btnCalculate = new javax.swing.JButton(); 
     lblFirstDigit = new javax.swing.JLabel(); 
     lblSecondDigit = new javax.swing.JLabel(); 
     ComboBoxOperator = new javax.swing.JComboBox(); 
     lblOperator = new javax.swing.JLabel(); 
     lblResult = new javax.swing.JLabel(); 
     lblHeader = new javax.swing.JLabel(); 
     btnConfigureServer = new javax.swing.JButton(); 
     txtResult = new javax.swing.JTextField(); 

     btnCalculate.setText("Calculate"); 
     btnCalculate.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btnCalculateActionPerformed(evt); 
      } 
     }); 

     lblFirstDigit.setText("First Digit"); 

     lblSecondDigit.setText("Second Digit"); 

     ComboBoxOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); 

     lblOperator.setText("Operator"); 

     lblResult.setText("Result"); 

     lblHeader.setText("RMI Simple Calculator"); 

     btnConfigureServer.setText("Configure Server"); 
     btnConfigureServer.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btnConfigureServerActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(lblFirstDigit, javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(lblSecondDigit, javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(lblOperator, javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(lblResult, javax.swing.GroupLayout.Alignment.TRAILING)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(lblHeader) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(btnCalculate)) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(txtSecondDigit) 
         .addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(btnConfigureServer))) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(12, 12, 12) 
       .addComponent(lblHeader) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(lblFirstDigit)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(txtSecondDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(lblSecondDigit)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(lblOperator) 
        .addComponent(btnCalculate)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(lblResult) 
        .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(btnConfigureServer) 
       .addContainerGap(24, Short.MAX_VALUE)) 
     ); 
    }      

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

    }             

    private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {            
     double firstdigit; 
     double seconddigit; 
     String operator; 

     firstdigit = Double.valueOf(txtFirstDigit.getText()); 

     seconddigit = Double.valueOf(txtSecondDigit.getText()); 

     operator = ComboBoxOperator.getSelectedItem().toString(); 

     try 
     { 
      CoreInterface coreobj = (CoreInterface) Naming.lookup("localhost/Core"); 
      double result = (coreobj.calc(firstdigit,seconddigit,operator)); 
      txtResult.setText(Double.toString(result)); 
     } 

     catch(Exception e) 
     { 
      txtResult.setText("e"); 
     } 
    }            

    private javax.swing.JComboBox ComboBoxOperator; 
    private javax.swing.JButton btnCalculate; 
    private javax.swing.JButton btnConfigureServer; 
    private javax.swing.JLabel lblFirstDigit; 
    private javax.swing.JLabel lblHeader; 
    private javax.swing.JLabel lblOperator; 
    private javax.swing.JLabel lblResult; 
    private javax.swing.JLabel lblSecondDigit; 
    private javax.swing.JTextField txtFirstDigit; 
    private javax.swing.JTextField txtResult; 
    private javax.swing.JTextField txtSecondDigit; 
    // End of variables declaration     
} 

任何建議表示讚賞。

+1

來自MainCalculator類的代碼會很有幫助。 – ChadNC 2012-08-08 12:43:36

+0

確保'RMISimpleCalculatorMain'被定義爲主類。 – EyalAr 2012-08-08 12:46:12

+0

當然,我已經把它定義爲主類.... – rofans91 2012-08-08 12:47:18

回答

2

MainCalculator類是JPanel。 A JPanel不能像這樣顯示,它必須是Window的一部分。將它添加到JFrame並在JFrame上致電setVisible(true)

此外,@ npinti的建議非常好:在EDT線程中執行與GUI相關的代碼。

1

問題是最有可能在本節:

public static void main(String[] args) { 
     MainCalculator calc = new MainCalculator(); 
     calc.setVisible(true); 
    } 

您需要將事件分派線程(EDT)內作出任何 GUI相關的任務。

嘗試像這樣:

public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       MainCalculator calc = new MainCalculator(); 
       calc.setVisible(true); 
      } 
     }); 
    } 
+0

我試過了,但它產生了錯誤..任何想法? – rofans91 2012-08-08 12:53:54

+0

@rofansmanao:什麼是錯誤?我的代碼有一個錯字。您可能還需要在末尾添加一個'.start()'。 – npinti 2012-08-08 13:02:03

+0

java.lang.Runnable是抽象的,不能實現抽象方法。 – rofans91 2012-08-08 13:13:55