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

Java Swing-如何从另一个类获取TextField中文本的值,java,swing,scope,jtextfield,Java,Swing,Scope,Jtextfield,好的,这里是Java初学者。 我正在尝试用JavaSwing制作一个多用途的数学实用程序。我想让它做的一件事就是能够解基本对数。我把逻辑都搞定了,但输出本身有问题。我有一个类(名为“LogTab”),其中有一个嵌套的静态类(名为“LogPanel”),用于实际的输入区域。该按钮位于LogPanel类之外,当我按下它时,我希望它能够获取LogTab中文本字段的值(名为“logBase”和“logNum”),计算它们,并将它们发送到输出类。除了从文本字段获取值的部分,我的一切都很好并且正常工作 这是

好的,这里是Java初学者。 我正在尝试用JavaSwing制作一个多用途的数学实用程序。我想让它做的一件事就是能够解基本对数。我把逻辑都搞定了,但输出本身有问题。我有一个类(名为“LogTab”),其中有一个嵌套的静态类(名为“LogPanel”),用于实际的输入区域。该按钮位于LogPanel类之外,当我按下它时,我希望它能够获取LogTab中文本字段的值(名为“logBase”和“logNum”),计算它们,并将它们发送到输出类。除了从文本字段获取值的部分,我的一切都很好并且正常工作

这是我的密码

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不能被解析或者不是一个字段”。我该如何更改它,以便我的ActionListener可以从文本字段获取文本

谢谢


另外,这是我关于堆栈溢出的第一个问题,所以如果我搞砸了什么,请告诉我:)

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


LogInput
logBase
logNum
字段设置为:

static class LogInput extends JComponent {
    JTextField logBase, logNum;
在构造函数中,删除
JTextField
标识符:

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

生成
logBase
logNum
实例字段

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);
现在添加一些getter

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
的任何实例访问
logBase
logNum
的值


关于这一点,我认为无论是
JSpinner
还是
JTextField
都是一个更好的主意,因为它们能够自己验证输入。有关更多详细信息,请参见和

我明白您对实例字段的意思,但当我运行getter时,我想到了一个NullPointerException。具体来说,在
String text=logBase.getText()行。我不知道,这可能是代码中其他部分的问题。这可能是因为您在构造中重新声明了文本字段,请参见我的示例的外观。接下来!解决了这个问题。谢谢