从一个类调用静态方法到jframe类

时间:2016-06-06 20:28:22

标签: java swing jframe

嗨Java专家,               我正在努力学习Java。所以我在练习一些基本问题。在这种情况下,我的想法是从用户获取两个数字,然后根据用户需求(加/减)计算这些数字。我可以轻松地做,但我试图在JFrame中实现该问题,但未能实现我想做的事情!当我试图通过window.number1和window.number2传递这些数字时,我收到错误。 错误消息:

window cannot be resolved to a variable

所以任何人都可以帮我弄清楚我需要改变我的程序。

Calculate.java

package day1.examples;

public class Calculate {
    double number1;
    double number2;
    public double[] calculateNumber() {
        double plus = number1+number2;
        double minus = number1-number2;
        return new double[] {plus,minus};

    }

}

TestCalculate.java

package day1.examples;

import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class TestCalculate {

    private JFrame frame;
    private JTextField textFieldNum1;
    private JTextField textFieldNum2;
    private JTextField textFieldTotRes;
    private JLabel lblNewTlabelTotNum;
    private JLabel lblNewLabelNum1;
    private JLabel lblNewLabelNum2;
    private JComboBox<Object> comboBoxNum;
    private double totalres;
    private String itemselect;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestCalculate window = new TestCalculate();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestCalculate() {
        initialize();

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textFieldNum1 = new JTextField();
        textFieldNum1.setBounds(10, 37, 106, 25);
        frame.getContentPane().add(textFieldNum1);
        textFieldNum1.setColumns(10);

        textFieldNum2 = new JTextField();
        textFieldNum2.setBounds(194, 37, 106, 25);
        frame.getContentPane().add(textFieldNum2);
        textFieldNum2.setColumns(10);

        JButton btnNewsButtonEnter = new JButton("Enter");
        btnNewsButtonEnter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final double num1;
                final double num2;
                try {
                    num1 = Double.parseDouble(textFieldNum1.getText());
                    num2 =Double.parseDouble(textFieldNum2.getText());
                    window.number1 = num1;
                    window.number2 = num2;
                    comboBoxNum.addActionListener(new ActionListener() {

                    if ((comboBoxNum.getSelectedItem()) == "Plus") {
                        totalres = window.calculateTotalPay()[0];
                        itemselect ="Plus";
                    } else if ((comboBoxNum.getSelectedItem()) == "Minus") {
                        totalres = window.calculateTotalPay()[1];
                        itemselect ="Minus";
                    }
                    if (itemselect == "Plus" || itemselect == "Minus"){
                        textFieldTotRes.setText(Double.toString(totalres));
                    } else {
                        JOptionPane.showMessageDialog(null, "Enter valid option");
                    }



                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Enter valid number");
                }
            }
        });

        JButton btnNewButtonClear = new JButton("Clear");
        btnNewButtonClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }

        });

        btnNewsButtonEnter.setBounds(20, 74, 84, 25);
        frame.getContentPane().add(btnNewsButtonEnter);
        btnNewButtonClear.setBounds(204, 74, 84, 25);
        frame.getContentPane().add(btnNewButtonClear);

        textFieldTotRes = new JTextField();
        textFieldTotRes.setBounds(198, 155, 200, 50);
        frame.getContentPane().add(textFieldTotRes);
        textFieldTotRes.setColumns(10);

        lblNewTlabelTotNum = new JLabel("Result");
        lblNewTlabelTotNum.setFont(new Font("Garuda", Font.BOLD | Font.ITALIC, 14));
        lblNewTlabelTotNum.setBounds(29, 162, 106, 34);
        frame.getContentPane().add(lblNewTlabelTotNum);

        lblNewLabelNum1 = new JLabel("Enter 1st Number");
        lblNewLabelNum1.setBounds(10, 0, 106, 34);
        frame.getContentPane().add(lblNewLabelNum1);

        lblNewLabelNum2 = new JLabel("Enter 2nd Number");
        lblNewLabelNum2.setBounds(194, 0, 97, 40);
        frame.getContentPane().add(lblNewLabelNum2);

        comboBoxNum = new JComboBox<Object>();
        comboBoxNum.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(comboBoxNum.getSelectedItem());
            }
        });
        comboBoxNum.setModel(new DefaultComboBoxModel<Object>(new String[] {"--------------", "Plus", "Minus"}));
        comboBoxNum.setBounds(331, 37, 58, 40);
        frame.getContentPane().add(comboBoxNum);



    }

    public void reset() {
        textFieldNum1.setText("");
        textFieldNum2.setText("");
        textFieldTotRes.setText("");

    }
}

1 个答案:

答案 0 :(得分:3)

  • 您永远不会实例化Calculate类。
  • 您将TestCalculate实例命名为“window”,但随后引用Calculate中的成员变量(例如,window.number1),这些变量不在TestCalculate中。
  • 您正在comboBoxNum ActionListener的actionPerformed方法内向btnNewsButtonEnter添加ActionListener。将嵌套的ActionListener移出该方法。