-2

私はJavaの初心者で、最初のプロジェクトの二次方程式ソルバーを作成しました。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 に答える