我的玻璃板有问题。当我第一次展示我的面板时,一切都很好。但是当我想重新粉刷这个面板时,因为组件应该改变(方法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();
}
然后它就像一个魅力。有人可以向我解释这种行为吗?