1

私はnetbeansで基本的な電卓プロジェクトを作成しました.そこでプロジェクトを実行すると、コードは正常に動作し、docs> netbeans projects>dist>calculator.jarに移動してそこで実行すると動作しますが、それをドラッグするとデスクトップに移動してそこで実行しようとすると、JFrame が開きません

以下は唯一のクラスです。

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.NoClassDefFoundError: org/netbeans/lib/awtExtra/AbsoluteLayout

44行目...(上記のコメント)

4

1 に答える 1

1

JAR ファイルをダブルクリックしようとしていますか? それはうまくいかないからです。

コマンドを開いて次のように入力する必要があります: java -jar calculator.jar

大文字と小文字が区別されると思うので、CALCULATOR.JAR は機能しません。.jar 拡張子が必要です。

ダブルクリックで実行可能にする方法はありますが、追加の手順が必要です。

幸運を!

于 2012-10-10T03:59:26.210 に答える