Java Swing - 如何从另一个 TextField 中获取文本值 class

Java Swing - How to get the value of text in a TextField from another class

好的,这里是 Java 编码初学者。 我正在尝试使用 Java Swing 制作一个多用途数学实用程序。我想让它做的一件事是能够求解基本对数。我的逻辑全都失败了,但我在输出本身时遇到了麻烦。我有一个 class(名为 "LogTab"),其中有一个用于实际输入区域的嵌套静态 class(名为 "LogPanel")。该按钮位于 LogPanel class 之外,当我按下它时,我希望它能够获取 LogTab 内的 TextFields 的值(名为 "logBase" 和 "logNum"),计算它们,并将它们发送到输出 class。除了 从 TextFields 中获取值的部分,我一切正常并且可以正常工作。

这是我的代码。

public class LogTab extends JPanel {

@SuppressWarnings("serial")
static class LogInput extends JComponent {

    public LogInput() {
        JComponent logInputPanel = new JPanel();
        setLayout(new GridBagLayout());

        JTextField logLbl = new JTextField();
        logLbl.setText("Log");
        logLbl.setEditable(false);
        JTextField logBase = new JTextField(1);
        JTextField logNum = new JTextField(5);

        GridBagConstraints lgc = new GridBagConstraints();

        lgc.weightx = 0.5;
        lgc.weighty = 0.5;

        lgc.gridx = 0;
        lgc.gridy = 0;
        add(logLbl,lgc);

        lgc.gridx = 1;
        lgc.gridy = 1;
        add(logBase,lgc);

        lgc.gridx = 2;
        lgc.gridy = 0;
        add(logNum,lgc);
    }
}

public LogTab() {
    // Set Layout
    setLayout(new GridBagLayout());

    // Create components
    JLabel promptLabel = new JLabel("Enter Logarithm: ");
    JButton solveButton = new JButton("Solve");
    final LogInput logInput = new LogInput();
    solveButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                double base = Double.valueOf(logInput.logBase.getText());
                double num = Double.valueOf(logInput.logNum.getText());
                OutputPanel.outputField.setText(String.valueOf(solve(base,num)));
            } finally {
            }

        }

    });



    // Code that adds the components, bla bla bla




}

public double solve(double base, double num) {
    return (Math.log(base)/Math.log(num));
}
}

当我尝试编译它时(顺便说一句,通过 Eclipse),我收到一个错误提示 "logBase/logNum can not be resolved or is not a field"。我该如何更改它以便我的 ActionListener 可以从 TextFields 中获取文本?

谢谢

P.S。这是我关于 Stack Overflow 的第一个问题,所以如果我搞砸了,请告诉我:)

问题是 logBaselogNum 的范围仅限于构造函数,因此其他方法无法访问。

使 logBaselogNum 字段成为 LogInput:

static class LogInput extends JComponent {
    JTextField logBase, logNum;

并在构造函数中,删除 JTextField 标识符:

logBase = new JTextField(1);
logNum = new JTextField(5);

创建 logBaselogNum 实例字段...

static class LogInput extends JComponent {

    private JTextField logBase;
    private JTextField logNum;

    public LogInput() {
        JComponent logInputPanel = new JPanel();
        setLayout(new GridBagLayout());

        JLabel logLbl = new JLabel("Log");
        logBase = new JTextField(1);
        logNum = new JTextField(5);

现在添加一些吸气剂...

public double getBase() {
    String text = logBase.getText();
    if (text.trim().isEmpty()) {
        text = "0";
    }
    return Double.parseDouble(text);
}

public double getNumber() {
    String text = logNum.getText();
    if (text.trim().isEmpty()) {
        text = "0";
    }
    return Double.parseDouble(text);
}

现在您可以从 LogInput

的任何实例访问 logBaselogNum 的值

关于这一点,我认为 JSpinnerJTextField 会更好,因为它们有能力自行验证输入。有关详细信息,请参阅 How to Use Spinners and How to Use Formatted Text Fields