我的玻璃窗格有问题。当我第一次显示我的面板时一切正常。但是当我想重新绘制这个面板因为组件应该改变(方法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();
}
然后它就像一个魅力。有人能解释一下这种行为吗?
答案 0 :(得分:1)
如果您在frame.revalidate()
之后简单地添加frame.getGlassPane().setVisible(true)
,那么它就会起作用。
答案 1 :(得分:1)
应该是
frame.rootPane.setGlassPane()
将LayoutManager添加到GlassPane或覆盖JPAnel的getPreferredSize
不应该是
KeyListener to JFrame,JFrame默认为API中的missine通知符
为什么要重载void(s),将父JPanel创建为局部变量,
为什么你重载void(s),子JPanel应该只创建一次,然后只用setVisible(true / false),在setVisible(false)上的JComponents中重置值