当我从一个单独的班级按下按钮时,如何调用方法?
例如,当点击事件在Save
按钮上触发时,我想从另一个名为setRDF
的类中调用GenerateRDF
方法
这是我的代码:
public class PersonalInfo extends JPanel {
private void initialize() {
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// call the `setRDF` method here
}
});
}
}
public class GenerateRDF extends Object {
public void setRDF() {
String personURI = "http://localhost/amitkumar";
String givenName = "Amit";
String familyName = "Kumar";
String fullName = givenName+familyName;
Model model = ModelFactory.createDefaultModel();
Resource node = model.createResource(personURI)
.addProperty(VCARD.FN, fullName)
.addProperty(VCARD.N,
model.createResource()
.addProperty(VCARD.Given, givenName)
.addProperty(VCARD.Family, familyName));
model.write(System.out);
}
}
答案 0 :(得分:1)
您将创建一个新的GenerateRDF
对象并在其上调用该方法。例如:
public class PersonalInfo extends JPanel {
private void initialize() {
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GenerateRDF generator = new GenerateRDF();
generator.setRDF();
}
});
}
}
附注:你不需要写extends Ojbect
;一切都默认为Object
。