【问题标题】:Java Swing - How to get the value of text in a TextField from another classJava Swing - 如何从另一个类中获取 TextField 中的文本值
【发布时间】:2015-02-22 02:39:12
【问题描述】:

好的,这里是初学者 Java 编码器。 我正在尝试使用 Java Swing 制作一个多功能数学实用程序。我希望它做的一件事是能够求解基本对数。我的逻辑已经全部关闭,但我在输出本身时遇到了问题。我有一个类(名为“LogTab”),其中有一个用于实际输入区域的嵌套静态类(名为“LogPanel”)。该按钮位于 LogPanel 类之外,当我按下它时,我希望它能够获取 LogTab 内的 TextFields 的值(名为“logBase”和“logNum”),计算它们,并将它们发送到输出类.除了 我从 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 无法解析或不是字段”。我将如何更改它以便我的 ActionListener 可以从 TextFields 中获取文本?

谢谢

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

【问题讨论】:

    标签: java swing scope jtextfield


    【解决方案1】:

    制作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);
    

    现在添加一些 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 的任何实例访问logBaselogNum 的值

    关于这一点,我认为JSpinnerJTextField 会是一个更好的主意,因为它们能够自己验证输入。请参阅How to Use SpinnersHow to Use Formatted Text Fields 了解更多详情

    【讨论】:

    • 我明白你对实例字段的意思,但是当我运行 getter 时,我想出了一个 NullPointerException。具体来说,在String text = logBase.getText(); 行。这可能是代码其他部分的问题,我不知道。
    • 这可能是因为你在结构中重新声明了文本字段,看看我的例子是什么样子
    • 好了!解决了这个问题。谢谢。
    【解决方案2】:

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

    制作LogInputlogBaselogNum字段:

    static class LogInput extends JComponent {
        JTextField logBase, logNum;
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多