单击按钮时如何获取输入字段的值?
例如,我需要textName
类的PersonalInfo
输入值,以便在另一个名为GenerateRDF
的类中使用
这是我的代码:
public class PersonalInfo extends JPanel {
private void initialize() {
....
JTextPane textName = new JTextPane();
textName.setBounds(95, 36, 302, 20);
panel.add(textName);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// send the value of `textName` to GenerateRDF
GenerateRDF generator = new GenerateRDF();
generator.setRDF();
}
});
}
}
public class GenerateRDF {
public void setRDF() {
String personURI = "http://localhost/amitkumar";
String fullName = textName;
// print here the value received from the `PersonalInfo` class
System.out.println(fullName);
Model model = ModelFactory.createDefaultModel();
Resource node = model.createResource(personURI)
.addProperty(VCARD.FN, fullName);
model.write(System.out);
}
}
答案 0 :(得分:0)
您可以使用按钮actionListener中的getText()
方法在单击变量时将输入存储在变量中。然后将此变量传递给GenerateRDF
类
答案 1 :(得分:0)
单击按钮时如何获取输入字段的值?
存在一种名为 getText() 的方法,可让您检索textName
变量的文本。
示例:强>
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// send the value of `textName` to GenerateRDF
String getName = textName.getText(); // retrieve the value
// do something with it
}
});
答案 2 :(得分:0)
public void actionPerformed(ActionEvent arg0) {
// send the value of `textName` to GenerateRDF
String input = texName.getText();
}
答案 3 :(得分:0)
您可以这样做:
public class PersonalInfo extends JPanel {
private void initialize() {
....
JTextPane textName = new JTextPane();
textName.setBounds(95, 36, 302, 20);
panel.add(textName);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// send the value of `textName` to GenerateRDF
GenerateRDF generator = new GenerateRDF();
generator.setRDF(textName.getText());
}
});
}
}
public class GenerateRDF {
public void setRDF(String fullName) {
String personURI = "http://localhost/amitkumar";
// print here the value received from the `PersonalInfo` class
System.out.println(fullName);
Model model = ModelFactory.createDefaultModel();
Resource node = model.createResource(personURI)
.addProperty(VCARD.FN, fullName);
model.write(System.out);
}
}