创建阻止玻璃窗格显示方法

时间:2013-03-15 08:25:46

标签: java blocking glasspane

我使用玻璃窗格在JFrame中创建了一个模态对话框。我的显示方法非常简单:它创建一个JPanel作为具有一些alpha背景的玻璃窗格,并添加JLabel和一个ok和关闭按钮。然后通过frame.getGlassPane().setVisible(true);设置并显示玻璃窗格。

一切正常:如果我调用方法,则会显示窗格,我可以单击“确定”或取消,并隐藏玻璃窗格。但是该方法在显示玻璃窗格后直接返回。但是我希望它的行为类似于JOptionPane方法:它们会阻塞,直到对话框关闭。

但每次我试图在show方法结束时插入任何类型的忙等待时,如果单击打开按钮,GUI将被冻结。我也尝试从JDialog#show()获得机制,但这对我来说有点复杂。

那么如何在玻璃窗可见时阻止show方法?

这是一个简单的例子:

public class GlassPaneSSCE extends JPanel {

    private JFrame parentFrame;

    public GlassPaneSSCE(JFrame parent) {
        parentFrame = parent;
        addKeyListener(new KeyAdapter() {});
        addMouseListener(new MouseAdapter() {});
        setBackground(new Color(0, 0, 0, 100));
        initGui();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }

    private void initGui() {
        setLayout(new FlowLayout(FlowLayout.CENTER));
        setOpaque(false);

        final JPanel content = new JPanel(new BorderLayout(4, 4));
        content.setOpaque(true);
        content.setBorder(new EmptyBorder(8, 8, 8, 8));

        JLabel top = new JLabel("Title of this little modal dialog");
        content.add(top, BorderLayout.NORTH);

        JPanel inner = new JPanel(new BorderLayout());
        content.add(inner, BorderLayout.CENTER);

        inner.add(new JScrollPane(new JList(new String[] {
            "Item 1                                        ", 
            "Item 2", "Item 3"
        })));

        Box ctrlButtons = Box.createHorizontalBox();
        ctrlButtons.setBorder(new EmptyBorder(0, 4, 4, 4));
        ctrlButtons.add(Box.createHorizontalGlue());
        ctrlButtons.add(new JButton(new AbstractAction("OK") {

            @Override
            public void actionPerformed(ActionEvent e) {
                parentFrame.getGlassPane().setVisible(false);
                parentFrame.setGlassPane(new JPanel());
            }
        }));
        content.add(ctrlButtons, BorderLayout.SOUTH);

        add(content);
    }

    public void display() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                parentFrame.setGlassPane(GlassPaneSSCE.this);
                parentFrame.getGlassPane().setVisible(true);

                // Set the focus on the glass pane
                requestFocus();
                setFocusCycleRoot(true);
            }
        });

        // The next line should be executed only if
        // the ok button is clicked and not before
        System.out.println("End of display()");
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame();
        f.setLayout(new FlowLayout(FlowLayout.CENTER));
        JTextArea tp = new JTextArea(10, 10);

        for (int i = 0; i < 20; i++) {
            JButton b = new JButton("Open");
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    GlassPaneSSCE gp  = new GlassPaneSSCE(f);
                    gp.display();
                }
            });
            f.add(b);

            tp.append("Item " + (i+1) + "\n");
        }
        f.add(new JScrollPane(tp));
        f.setSize(600, 600);
        f.setVisible(true);
    }

}

0 个答案:

没有答案