作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的玻璃板有问题。当我第一次展示我的面板时,一切正常。但是,当我想重新绘制此面板时,因为组件应该更改(方法 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();
}
然后它就像一个魅力。有人可以向我解释一下这种行为吗?
最佳答案
如果您简单地在 frame.getGlassPane().setVisible(true)
之后添加 frame.revalidate()
就可以了。
关于java - 重新喷漆后玻璃板消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945543/
我是一名优秀的程序员,十分优秀!