-2

我是 Java 的初学者,并制作了我的第一个项目 Quadratic Equations 求解器。它在 Windows(我开发它的平台)上运行良好,但它也应该在其他平台上运行,(不是吗?)。好吧,这个在Linux(BackBox,PuppyLinux)上不起作用,我试过但它表明它不是可执行文件。此失败的可能原因是什么?

PS我真的是“跨平台”的初学者,尤其是Java。

这是代码:

        /*
 * QuadraticEquations.java
 *
 * Created on Mar 17, 2012, 8:37:16 PM
 */
/**
 *
 * @author Afaan
 */
public class QuadraticEquations extends javax.swing.JFrame {

    /** Creates new form QuadraticEquations */
    public QuadraticEquations() {
        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">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPanel = new javax.swing.JPanel();
        HeaderLabel = new javax.swing.JLabel();
        AuthorLabel = new javax.swing.JLabel();
        SubHeaderLabel = new javax.swing.JLabel();
        a_Label = new javax.swing.JLabel();
        b_Label = new javax.swing.JLabel();
        c_Label = new javax.swing.JLabel();
        a_Value = new javax.swing.JTextField();
        b_Value = new javax.swing.JTextField();
        c_Value = new javax.swing.JTextField();
        calc_Button = new javax.swing.JButton();
        ResultLabel = new javax.swing.JLabel();
        Root1_Label = new javax.swing.JLabel();
        Root2_Label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quadratic Equations Roots Calculator");

        HeaderLabel.setFont(new java.awt.Font("Lucida Fax", 1, 18)); // NOI18N
        HeaderLabel.setForeground(new java.awt.Color(0, 0, 255));
        HeaderLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        HeaderLabel.setText("Quadratic Equations - Roots Calculator");

        AuthorLabel.setFont(new java.awt.Font("Lucida Fax", 1, 14)); // NOI18N
        AuthorLabel.setForeground(new java.awt.Color(0, 165, 0));
        AuthorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        AuthorLabel.setText("Afaan Bilal");

        SubHeaderLabel.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        SubHeaderLabel.setText("Enter the values of 'a', 'b', 'c' : ");

        a_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        a_Label.setForeground(new java.awt.Color(25, 22, 200));
        a_Label.setText("\" a \" :");

        b_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        b_Label.setForeground(new java.awt.Color(25, 22, 200));
        b_Label.setText("\" b \" :");

        c_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        c_Label.setForeground(new java.awt.Color(25, 22, 200));
        c_Label.setText("\" c \" :");

        a_Value.setText("1");

        b_Value.setText("2");

        c_Value.setText("1");

        calc_Button.setText("Calculate Roots!");
        calc_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CalculateRoots(evt);
            }
        });

        ResultLabel.setFont(new java.awt.Font("Lucida Fax", 1, 14)); // NOI18N
        ResultLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        Root1_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        Root1_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        Root2_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        Root2_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
        MainPanel.setLayout(MainPanelLayout);
        MainPanelLayout.setHorizontalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MainPanelLayout.createSequentialGroup()
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, MainPanelLayout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(b_Label)
                            .addComponent(a_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(c_Label))
                        .addGap(18, 18, 18)
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(c_Value)
                            .addComponent(b_Value)
                            .addComponent(a_Value, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
                        .addGap(69, 69, 69))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, MainPanelLayout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ResultLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(SubHeaderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Root2_Label, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(Root1_Label, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(HeaderLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                .addGap(41, 41, 41))
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addGap(167, 167, 167)
                .addComponent(AuthorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(184, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MainPanelLayout.createSequentialGroup()
                .addContainerGap(176, Short.MAX_VALUE)
                .addComponent(calc_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(148, 148, 148))
        );
        MainPanelLayout.setVerticalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(HeaderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AuthorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(SubHeaderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(a_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(a_Label))
                .addGap(11, 11, 11)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b_Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(c_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(c_Label))
                .addGap(18, 18, 18)
                .addComponent(calc_Button)
                .addGap(33, 33, 33)
                .addComponent(ResultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Root1_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Root2_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void CalculateRoots(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CalculateRoots

    double a = (double)(Double.parseDouble(a_Value.getText()));
    double b = (double)(Double.parseDouble(b_Value.getText()));
    double c = (double)(Double.parseDouble(c_Value.getText()));
    double D = 0.0;
    double r1 = 0.0;
    double r2 = 0.0;

    if (a == 0)
    {
        ResultLabel.setText("Error: The value of \"a\" cannot be zero.");
        Root1_Label.setText("");
        Root2_Label.setText("");
    }
    else
    {
        D = (b * b) - 4 * a * c;
        if (D < 0)
        {
            ResultLabel.setText("Error: The value of \"D\" is less than zero.");
            Root1_Label.setText("Therefore, Real roots do not exist.");
            Root2_Label.setText("");
        }
        else
        {
            r1 = ( - b + Math.sqrt(D) ) / 2 * a;
            r2 = ( - b - Math.sqrt(D) ) / 2 * a;
            ResultLabel.setText("The value of \"D\" is " + D + "");
            Root1_Label.setText("The first root is " + r1 + "");
            Root2_Label.setText("The second root is " + r2 + "");
        }
    }
}//GEN-LAST:event_CalculateRoots

    /**
     * @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(QuadraticEquations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuadraticEquations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuadraticEquations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuadraticEquations.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 QuadraticEquations().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AuthorLabel;
    private javax.swing.JLabel HeaderLabel;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JLabel ResultLabel;
    private javax.swing.JLabel Root1_Label;
    private javax.swing.JLabel Root2_Label;
    private javax.swing.JLabel SubHeaderLabel;
    private javax.swing.JLabel a_Label;
    private javax.swing.JTextField a_Value;
    private javax.swing.JLabel b_Label;
    private javax.swing.JTextField b_Value;
    private javax.swing.JLabel c_Label;
    private javax.swing.JTextField c_Value;
    private javax.swing.JButton calc_Button;
    // End of variables declaration//GEN-END:variables
}

我安装了 JVM,并将其编译为一个简单的 JAR 可执行文件。其指定的主要类是QuadraticEquations. 在 Windows 中执行和运行良好,但在 Linux 中却不行。

4

1 回答 1

4

您的示例同时在 Mac OS X/AppleJDK 和 Ubuntu/OpenJDK 上运行。作为参考,GroupLayout它是在 Java 6 中引入的,因此您可以检查您安装的版本。

作为初学者,你可以暂时放下 GUI 编辑器,尝试更简单的布局,如下GridLayout图所示。作为练习,使用嵌套JPanel的必须Flowlayout将标签和输入字段保持在同一行。

对于未来的学习,您可能还会喜欢这种寻找多项式根的替代方法。

附录:我添加了一个使用 Ubuntu 10/OpenJDK 的屏幕截图。

在此处输入图像描述

public class QuadraticEquations extends javax.swing.JFrame {

    /**
    * Creates new form QuadraticEquations
    */
    public QuadraticEquations() {
        initComponents();
    }

    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        headerLabel = new javax.swing.JLabel();
        authorLabel = new javax.swing.JLabel();
        subHeaderLabel = new javax.swing.JLabel();
        a_Label = new javax.swing.JLabel();
        b_Label = new javax.swing.JLabel();
        c_Label = new javax.swing.JLabel();
        a_Value = new javax.swing.JTextField();
        b_Value = new javax.swing.JTextField();
        c_Value = new javax.swing.JTextField();
        calc_Button = new javax.swing.JButton();
        resultLabel = new javax.swing.JLabel();
        root1_Label = new javax.swing.JLabel();
        root2_Label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quadratic Equations Roots Calculator");

        headerLabel.setFont(new java.awt.Font("Lucida Fax", 1, 18)); // NOI18N
        headerLabel.setForeground(new java.awt.Color(0, 0, 255));
        headerLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        headerLabel.setText("Quadratic Equations - Roots Calculator");

        authorLabel.setFont(new java.awt.Font("Lucida Fax", 1, 14)); // NOI18N
        authorLabel.setForeground(new java.awt.Color(0, 165, 0));
        authorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        authorLabel.setText("Afaan Bilal");

        subHeaderLabel.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        subHeaderLabel.setText("Enter the values of 'a', 'b', 'c' : ");

        a_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        a_Label.setForeground(new java.awt.Color(25, 22, 200));
        a_Label.setText("\" a \" :");

        b_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        b_Label.setForeground(new java.awt.Color(25, 22, 200));
        b_Label.setText("\" b \" :");

        c_Label.setFont(new java.awt.Font("Lucida Fax", 1, 12)); // NOI18N
        c_Label.setForeground(new java.awt.Color(25, 22, 200));
        c_Label.setText("\" c \" :");

        a_Value.setText("1");
        b_Value.setText("2");
        c_Value.setText("1");

        calc_Button.setText("Calculate Roots!");
        calc_Button.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CalculateRoots(evt);
            }
        });

        resultLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        root1_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        root2_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        mainPanel.setLayout(new java.awt.GridLayout(0,1));
        mainPanel.add(headerLabel);
        mainPanel.add(authorLabel);
        mainPanel.add(subHeaderLabel);
        mainPanel.add(a_Label);
        mainPanel.add(a_Value);
        mainPanel.add(b_Label);
        mainPanel.add(b_Value);
        mainPanel.add(c_Label);
        mainPanel.add(c_Value);
        mainPanel.add(calc_Button);
        mainPanel.add(resultLabel);
        mainPanel.add(root1_Label);
        mainPanel.add(root2_Label);

        add(mainPanel);
        pack();
        setLocationRelativeTo(null);
    }

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

        double a = Double.parseDouble(a_Value.getText());
        double b = Double.parseDouble(b_Value.getText());
        double c = Double.parseDouble(c_Value.getText());
        double d = 0.0;
        double r1 = 0.0;
        double r2 = 0.0;

        if (a == 0) {
            resultLabel.setText("Error: The value of \"a\" cannot be zero.");
            root1_Label.setText("");
            root2_Label.setText("");
        } else {
            d = (b * b) - 4 * a * c;
            if (d < 0) {
                resultLabel.setText("Error: The value of \"D\" is negative.");
                root1_Label.setText("Therefore, Real roots do not exist.");
                root2_Label.setText("");
            } else {
                r1 = (-b + Math.sqrt(d)) / 2 * a;
                r2 = (-b - Math.sqrt(d)) / 2 * a;
                resultLabel.setText("The value of \"D\" is " + d);
                root1_Label.setText("The first root is " + r1);
                root2_Label.setText("The second root is " + r2);
            }
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        /*
        * Create and display the form
        */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new QuadraticEquations().setVisible(true);
            }
        });
    }
    // Variables declaration
    private javax.swing.JLabel authorLabel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JLabel root1_Label;
    private javax.swing.JLabel root2_Label;
    private javax.swing.JLabel subHeaderLabel;
    private javax.swing.JLabel a_Label;
    private javax.swing.JTextField a_Value;
    private javax.swing.JLabel b_Label;
    private javax.swing.JTextField b_Value;
    private javax.swing.JLabel c_Label;
    private javax.swing.JTextField c_Value;
    private javax.swing.JButton calc_Button;
    // End of variables declaration
}
于 2012-06-02T16:32:14.727 回答