java中怎么把两个JTextfield中的数字相加的值放到另一个JTextfield?,Java Swing-如何从另一个类获取TextField中的文本值...

Okay, beginner Java coder here.

I'm trying to make a multi-purpose math utility application with Java Swing. One of the things I want it to do is be able to solve basic logarithms. I have the logic all down, but I'm having trouble with the outputting itself. I have a class (named "LogTab") and within it is a nested static class (named "LogPanel") for the actual input area. The button is outside the LogPanel class, and when I press it I want it to be able to take the values of the TextFields inside the LogTab (named "logBase" and "logNum"), calculate them, and send them to an output class. I have everything fine and working except for the part where I take the values from the TextFields.

Here's my code.

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

}

}

When I try to compile this (through Eclipse, by the way), I get an error saying that "logBase/logNum can not be resolved or is not a field". How would I change this so that my ActionListener can get the text from the TextFields?

Thanks

P.S. This is my first question on Stack Overflow, so if I messed something up, tell me :)

解决方案

Make logBase and logNum instance fields...

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

Now add some getters...

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

}

Now you can access the values of logBase and logNum from any instance of LogInput

About this point, I'm thinking that either a JSpinner or JTextField would be a better idea, as they have the ability to validate the input themselves. See How to Use Spinners and How to Use Formatted Text Fields for more details

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值