2012-10-10 101 views
1

我剛剛在netbeans中做了一個基本的計算器項目,當我在那裏運行項目時代碼工作得很好,當我去找文檔> netbeans projects> dist> calculator。罐子,並運行它它的工作原理,但是當我拖動到桌面上,並嘗試運行它JFrame中不會打開Jframe窗口不能用我的可執行文件jar打開

這裏是下面的唯一一類:

package calculator; 

/** 
* 
* @author David 
*/ 
public class ui extends javax.swing.JFrame { 

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

     buttonGroup1 = new javax.swing.ButtonGroup(); 
     jScrollBar1 = new javax.swing.JScrollBar(); 
     jLabel1 = new javax.swing.JLabel(); 
     addBut = new javax.swing.JRadioButton(); 
     subBut = new javax.swing.JRadioButton(); 
     mulBut = new javax.swing.JRadioButton(); 
     divBut = new javax.swing.JRadioButton(); 
     intOneText = new javax.swing.JTextField(); 
     intTwoText = new javax.swing.JTextField(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     result = new javax.swing.JLabel(); 
     goButton = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); // error here 

     jLabel1.setText("Calculator"); 
     getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, -1, -1)); 

     buttonGroup1.add(addBut); 
     addBut.setText("Add"); 
     getContentPane().add(addBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 110, -1, -1)); 

     buttonGroup1.add(subBut); 
     subBut.setText("Subtract"); 
     getContentPane().add(subBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 110, -1, -1)); 

     buttonGroup1.add(mulBut); 
     mulBut.setText("Multiply"); 
     getContentPane().add(mulBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 140, -1, -1)); 

     buttonGroup1.add(divBut); 
     divBut.setText("Divide"); 
     getContentPane().add(divBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 140, -1, -1)); 
     getContentPane().add(intOneText, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 131, 83, -1)); 
     getContentPane().add(intTwoText, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 180, 83, -1)); 

     jLabel2.setText("Integer 1:"); 
     getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 134, -1, -1)); 

     jLabel3.setText("Integer 2:"); 
     getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, -1)); 

     result.setText("Solution Will Appear Here"); 
     getContentPane().add(result, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 220, -1, -1)); 

     goButton.setText("Calculate"); 
     goButton.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       goButtonMouseClicked(evt); 
      } 
     }); 
     getContentPane().add(goButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(59, 218, -1, -1)); 

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

    private void goButtonMouseClicked(java.awt.event.MouseEvent evt) {          

     if(intOneText.getText().equals("")){ 
      result.setText("Something is missing here..."); 
     } 
     if(intTwoText.getText().equals("")){ 
      result.setText("Something is missing here..."); 
     } 

     String intOne = intOneText.getText(); 
     float numOne = Float.parseFloat(intOne); 
     String intTwo = intTwoText.getText(); 
     float numTwo = Float.parseFloat(intTwo); 
     float answer = 0; 

     boolean addTrue = false; 
     boolean subTrue = false; 
     boolean mulTrue = false; 
     boolean divTrue = false; 


     if(addBut.isSelected()){ 
      addTrue = true; 
      subTrue = false; 
      mulTrue = false; 
      divTrue = false; 
     } 

     if(subBut.isSelected()){ 
      addTrue = false; 
      subTrue = true; 
      mulTrue = false; 
      divTrue = false; 
     } 

     if(mulBut.isSelected()){ 
      addTrue = false; 
      subTrue = false; 
      mulTrue = true; 
      divTrue = false; 
     } 

     if(divBut.isSelected()){ 
      addTrue = false; 
      subTrue = false; 
      mulTrue = false; 
      divTrue = true; 
     } 

     if(addTrue == false){ 
      if(subTrue == false){ 
       if(mulTrue == false){ 
        if(divTrue == false){ 
         result.setText("Something is missing here..."); 
        } 
       } 
      } 
     } 


     if(addTrue == true){ 
      answer = numOne + numTwo; 
     } 
     if(subTrue == true){ 
      answer = numOne - numTwo; 
     } 
     if(mulTrue == true){ 
      answer = numOne * numTwo; 
     } 
     if(divTrue == true){ 
      answer = numOne/numTwo; 
     } 

     result.setText(Float.toString(answer)); 

    }          

    /** 
    * @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(ui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(ui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(ui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(ui.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 ui().setVisible(true); 


      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JRadioButton addBut; 
    private javax.swing.ButtonGroup buttonGroup1; 
    private javax.swing.JRadioButton divBut; 
    private javax.swing.JButton goButton; 
    private javax.swing.JTextField intOneText; 
    private javax.swing.JTextField intTwoText; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JScrollBar jScrollBar1; 
    private javax.swing.JRadioButton mulBut; 
    private javax.swing.JLabel result; 
    private javax.swing.JRadioButton subBut; 
    // End of variables declaration     
} 

,我得到的錯誤在命令行中是:

線程「AWT-EventQueue-0」中的異常java.lang.NoC lassDefFoundError:組織/ netbeans的/ lib目錄/ awtExtra/AbsoluteLayout

在線路44 ...(以上評論)

+0

將jar複製到某個文件夾中。轉到命令行,轉到相同的文件夾並使用'java -jar calculator.jar'注意錯誤。我打敗了它缺少一個依賴庫(即擺動佈局庫,這是必需的'絕對佈局' – MadProgrammer

+0

錯誤:無法訪問jarfile calculator.jar – CodeLover

+0

對不起,我把錯誤的jar文件 – CodeLover

回答

1

你們是不是要雙擊JAR文件?因爲那不行。

你必須打開一個cmd,然後鍵入: Java的罐子calculator.jar

我認爲這是區分大小寫的,所以CALCULATOR.JAR將無法正常工作。你需要.jar擴展名。

有一種方法可以通過雙擊來執行它,但這需要額外的步驟。

祝你好運!

+0

如果配置正確,雙擊一個JAR文件應該可以正常工作,OP會詢問具體原因,爲什麼在他的系統上無法正常工作 –

+0

我得到一個錯誤,但是我的命令提示符顯得很奇怪,它表示我無法從我的用戶帳戶中找到路徑桌面 – CodeLover

+0

@CodeLover請修改您的問題以包含確切的錯誤消息。 –

相關問題