0

我的玻璃板有问题。当我第一次展示我的面板时,一切都很好。但是当我想重新粉刷这个面板时,因为组件应该改变(方法createPanel())然后玻璃面板消失。当我调整框架大小时,它再次可见。我不知道为什么会发生这种情况,我的面板看起来像:

package GUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TurnajPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private GridBagConstraints c = new GridBagConstraints();
    private JFrame frame;

    public TurnajPanel(JFrame frame) throws Exception {
        this.frame = frame;
        createPanel();
    }

    public void createPanel() throws Exception {
        removeAll();
        setLayout(new GridBagLayout());

        addMenu();

        revalidate();
        repaint();
    }

    private void addMenu() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 20));
        panel.setBorder(BorderFactory.createTitledBorder("aaaaaaaaaaaaaa"));
        JPanel innerPanelTlacidiel = new JPanel(new GridBagLayout());
        panel.add(innerPanelTlacidiel);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.weighty = 0;
        c.anchor = GridBagConstraints.FIRST_LINE_END;
        c.insets = new Insets(0, 5, 0, 0);
        final JPanel panelTlacidiel = new JPanel(new GridLayout(0, 1, 5, 5));
        innerPanelTlacidiel.add(panelTlacidiel, c);
        frame.setGlassPane(panel);
        frame.getGlassPane().setVisible(true);

    }

    public static void main(String[] args) throws Exception {

        JFrame frame = new JFrame();
        final TurnajPanel turnaj = new TurnajPanel(frame);
        JScrollPane scrolovaciPanel = new JScrollPane(turnaj);
        frame.add(scrolovaciPanel);

        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("????????????????");
                try {
                    turnaj.createPanel();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub

            }
        });
    }
}

当我在方法中添加一行时createPanel()

public void createPanel() throws Exception {
    removeAll();
    FrameFactory.turnajFrame.getGlassPane().setVisible(false); // new line
    setLayout(new GridBagLayout());

    addMenu();

    revalidate();
    repaint();
}

然后它就像一个魅力。有人可以向我解释这种行为吗?

4

2 回答 2

1
  • 应该

    1. frame.rootPane.setGlassPane()

    2. 将 LayoutManager 添加到 GlassPane 或覆盖 JPanel 的 getPreferredSize

  • 不应该

    1. KeyListener to JFrame, JFrame 默认为 KeyEvents API 中的missine 通知器

    2. 为什么要重载 void(s),创建父 JPanel 作为局部变量,

    3. 为什么你重载 void(s),子 JPanel 应该只创建一次,然后只使用 setVisible(true/false),在 setVisible(false) 上重置 JComponents 中的值

  • 看我的问题

于 2013-04-11T11:28:19.973 回答
1

如果你简单地添加frame.revalidate()之后frame.getGlassPane().setVisible(true)它会起作用。

于 2013-04-11T10:50:58.773 回答