简体   繁体   English

Java:从库访问用户生成的代码

[英]Java: access user Generated Code from Library

I am making a Library for my pupils to create a GUI easy in Java, but if they press a button in the Interface it should run a method created by them. 我正在为我的学生提供一个库,使他们可以用Java轻松创建GUI,但是如果他们在界面中按下按钮,它应该运行由他们创建的方法。
Is there any way to do this? 有什么办法吗? I am not really into Java, but the curriculum wants me to:( 我不是真的很喜欢Java,但是课程要求我:(

You can use reflection to run their class without requiring they implement an interface although you would still have to provide instructions to limit the argument types. 您可以使用反射来运行它们的类,而无需它们实现接口,尽管您仍然必须提供说明来限制参数类型。 This works only for methods with no arguments. 这仅适用于不带参数的方法。

java.awt.EventQueue.invokeLater(() -> {
    JFrame frm = new JFrame();
    JPanel pnl = new JPanel();
    frm.add(pnl);
    pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
    pnl.add(new JLabel("Class"));
    JTextField classField = new JTextField();
    pnl.add(classField);
    pnl.add(new JLabel("Method"));
    JTextField methodField = new JTextField();
    pnl.add(methodField);
    JTextArea area = new JTextArea();
    area.setPreferredSize(new Dimension(300, 300));
    JButton btn = new JButton("Run");
    pnl.add(btn);
    pnl.add(area);
    System.setOut(new PrintStream(new OutputStream() {
        @Override
        public void write(int b) throws IOException {
            area.append(new String(new byte[]{(byte) b}));
        }
    }));
    btn.addActionListener(e -> {
        try {
            Class cls = Class.forName(classField.getText());
            Method m = null;
            Method ma[] = cls.getDeclaredMethods();
            String methodName = methodField.getText().trim();
            m = cls.getMethod(methodName,new Class[]{});
            Object o = cls.newInstance();
            Object mr = m.invoke(o);
            if(null != mr) {
                area.append("\nreturned "+mr.toString()+"\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            area.append("\nException "+ex.getMessage()+"\n");
        }
    });
    frm.pack();
    frm.setVisible(true);
});

given a class like: 给一个像这样的课程:

public class StudentClass {
    public void print10() {
        int sum= 0;
        for(int i = 0; i< 10; i++ ) {
            System.out.println("i = "+i);
            sum+=i;
        }
    }
}

The fields would be need to be filled with StudentClass and print10 and the compiled class needs to be on your classpath. 这些字段将需要用StudentClassprint10填充,并且已编译的类需要在您的类路径中。

Perhaps a better option would be to teach Processing ( https://processing.org/ ). 也许更好的选择是教授处理程序( https://processing.org/ )。 This is essentially java since the Processing code gets pasted into a java class behind the scenes but is much more oriented to get beginners drawing graphical sketches. 从本质上讲,这是Java,因为处理代码会被粘贴到幕后的Java类中,但是面向初学者则要绘制图形草图。 I guess you'd have to ask your administration if they would go for it and at some point the students would need to be able to write the code the processing tools are generating for them. 我猜您将不得不问您的主管部门是否愿意这样做,并且在某些时候,学生将需要能够编写处理工具为他们生成的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM