1

我编写了一个生成测验小程序的应用程序。我找不到一种可移植的方式来自动对生成的小程序进行签名,因此它们没有被签名。但是据我所知,这段简单的代码不需要对Applet进行签名,但它在linux上抛出了关于“accessEventQueue”的accessControlException。我在 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...");

浏览了一下,我发现有关 IcedTea 上的错误的信息。我自己在 Windows 上尝试过这个小程序,它没有在那里抛出任何异常。

如果我发现的确实是一个错误,是否有任何解决方法或者我必须实现自己的确认对话框......?

有什么方法可以在不干扰 AWT 事件队列的情况下弹出 JOptionPane 对话框?

4

1 回答 1

0

正如我从开发人员那里听到的,这是一个实际的错误。

这是我对确认对话框的简单实现,可以为您节省一些编码时间:

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();
    }
}

这是它使用的回调:

public abstract class ConfirmCallback {
    public abstract void run(int arg);
}
于 2012-06-27T08:13:26.150 回答