简体   繁体   English

小程序上的JOptionPane,accessEventQueue accessControlException

[英]JOptionPane on applet, accessEventQueue accessControlException

I wrote an application that generates quiz applets. 我编写了一个生成测验小程序的应用程序。 I could't find a portable way to sign the generated applets automatically so they are not signed. 我找不到一种可移植的方式来自动签名生成的applet,因此无法对其进行签名。 But this simple piece of code as far as I know doesn't require the Applet to be signed, yet it is throwing accessControlException about "accessEventQueue" on linux. 但是据我所知,这段简单的代码不需要对Applet进行签名,但是它在Linux上引发了有关“ accessEventQueue”的accessControlException。 I'm running it on IceTea7, OpenJDK7, I tried on both Chrome and Opera. 我在IceTea7,OpenJDK7上运行它,我在Chrome和Opera上都尝试过。

System.out.println("This will display...");

int r = JOptionPane.showConfirmDialog(null,"End the quiz now?",
    "Quiz",
    JOptionPane.YES_NO_OPTION,                  
    JOptionPane.INFORMATION_MESSAGE);

System.out.println("This won't...");

Surfing a little I found this info about a bug on IcedTea. 冲浪一点,我发现这个关于IcedTea的错误信息。 I've tried the applet myself on windows and it isn't throwing any Exception there. 我自己在Windows上尝试过该applet,并且在该处未引发任何异常。

If what I've found is really a bug, is there any workaround or I'll have to implement my own confirmation dialog...? 如果发现的确是一个错误,是否有任何解决方法,或者我将必须实现自己的确认对话框...?

Is there any way to popup a JOptionPane dialog without interfering with the AWT event queue? 有什么方法可以弹出JOptionPane对话框而不干扰AWT事件队列?

As I heard from the developers, it's an actual bug. 正如我从开发人员那里听到的那样,这是一个实际的错误。

Here's my naive implementation of a confirmation dialog to save you some coding time: 这是我天真的确认对话框的实现,可以节省一些编码时间:

public class ConfirmDialog implements ActionListener {
    JFrame main;
    ConfirmCallback callback;

    public ConfirmDialog(String msg,String[] opts, ConfirmCallback lc) {
        this(msg,"Selection",opts,lc);
    }

    public ConfirmDialog(String msg,String title ,String[] opts,  ConfirmCallback lc) {
        main = new JFrame();
        main.setTitle(title);
        this.callback = lc;
        main.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagLayout layout = new GridBagLayout();

        JPanel panel = new JPanel();
        panel.setLayout(layout);
        panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0; gbc.gridy =0;
        gbc.gridwidth = opts.length; gbc.gridheight = 1;
        gbc.insets = new Insets(3,3,3,3);

        JLabel mainLabel = new JLabel(msg);

        layout.setConstraints(mainLabel, gbc);
        panel.add(mainLabel);

        gbc.gridy = 1;
        gbc.gridwidth= 1;

        int cnt = 0;
        for (String s: opts) {
            JButton submitButton = new JButton(s);
            submitButton.setActionCommand(Integer.toString(cnt++));
            submitButton.addActionListener(this);   
            gbc.gridx = cnt;
            layout.setConstraints(submitButton, gbc);
            panel.add(submitButton);
        }
        main.add(panel);
        main.pack();
        main.setLocationRelativeTo(null);
        main.setVisible(true);

    }

    public ConfirmDialog() {}

    @Override
    public void actionPerformed(ActionEvent e) {
        callback.run(Integer.decode(e.getActionCommand()));
        main.dispose();
    }

    public void Test() {
        ConfirmCallback cb = new ConfirmCallback(){
            @Override
            public void run(int arg) {
                JOptionPane.showMessageDialog(null, "The user just entered: "+arg);
            }
        };
        new ConfirmDialog("Please choose",new String[] {"a","b","c"},cb);
    }

    public static void main(String args[]) {
        new ConfirmDialog().Test();
    }
}

And here is the callback it uses: 这是它使用的回调:

public abstract class ConfirmCallback {
    public abstract void run(int arg);
}

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

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