有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建块玻璃窗格显示方法

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

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

但每次我试图在show方法的末尾插入任何类型的繁忙等待时,如果我单击open按钮,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) 个答案