Java Swing-如何从另一个类获取TextField中的文本值 [英] Java Swing - How to get the value of text in a TextField from another class
问题描述
好的,这里是初学者Java编码器. 我正在尝试使用Java Swing制作一个多功能的数学实用程序.我要它做的一件事是能够求解基本对数.我的逻辑全部消失了,但是输出本身遇到了麻烦.我有一个类(名为"LogTab"),其中有一个用于实际输入区域的嵌套静态类(名为"LogPanel").该按钮位于LogPanel类的外部,当我按下该按钮时,我希望它能够获取LogTab内部的TextFields的值(分别命名为"logBase"和"logNum"),进行计算并将其发送到输出类.除了我从TextFields中获取值的部分之外,我一切正常,工作正常.
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.
这是我的代码.
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中获取文本?
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?
谢谢
P.S.这是我关于堆栈溢出的第一个问题,所以如果我搞砸了,请告诉我:)
P.S. This is my first question on Stack Overflow, so if I messed something up, tell me :)
推荐答案
设置logBase
和logNum
实例字段...
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);
}
现在您可以从任何LogInput
实例访问logBase
和logNum
的值
Now you can access the values of logBase
and logNum
from any instance of LogInput
关于这一点,我认为JSpinner
或JTextField
会是一个更好的主意,因为他们有能力自己验证输入.请参见如何使用微调器和
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
这篇关于Java Swing-如何从另一个类获取TextField中的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!