如何使用Java从另一个类调用方法

时间:2017-03-22 19:48:02

标签: java

当我从一个单独的班级按下按钮时,如何调用方法?

例如,当点击事件在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);
    }
}

1 个答案:

答案 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