Jframe 窗口无法使用我的可执行 jar 打开



我刚刚在 NetBytes 中创建了一个基本的计算器项目,当我在那里运行该项目时,代码工作正常,当我转到 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 行...(以上评论)

您是否正在尝试双击 JAR 文件? 因为那行不通。

您必须打开一个cmd并键入:Java -jar 计算器.jar

我认为它区分大小写,所以计算器。JAR 将不起作用。 您需要.jar扩展。

有一种方法可以通过双击使其可执行,但这需要额外的步骤。

祝你好运!

最新更新