Fos*_*los 6 java user-interface swing
我正在使用扩展JFrame的自定义类,但有时它什么也没显示.我从来没有遇到过任何错误,所以我很好奇这是一个可以帮我打印东西的java命令.我环顾四周寻找其他问题,但没有发现任何相似之处.不是真的做了太疯狂的事情,但好奇为什么会这样.我想纠正这个问题,以避免将来出现问题.
空白

GUI

public MemberPanel(int i) throws IOException {
Container contentPane = getContentPane();
GridLayout layout = new GridLayout(2, 1);
contentPane.setLayout(layout);
setVisible(true);
setLocation(0, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(640, 170);
setResizable(false);
greenStatus = new JButton("Non-Critical");
yellowStatus = new JButton("Important");
redStatus = new JButton("Mission Critical");
greenStatus.setFont(fontTextOne);
yellowStatus.setFont(fontTextOne);
redStatus.setFont(fontTextOne);
greenStatus.addActionListener(this);
yellowStatus.addActionListener(this);
redStatus.addActionListener(this);
buttonPanel.add(greenStatus);
buttonPanel.add(yellowStatus);
buttonPanel.add(redStatus);
statusLabel = new JLabel("In 75 letters or less... What are you working on?");
statusLabel.setVerticalAlignment(JLabel.CENTER);
statusLabel.setHorizontalAlignment(JLabel.CENTER);
statusLabel.setFont(fontTextTwo);
textFieldPanel.add(statusLabel);
textFieldPanel.add(statusMessage);
contentPane.add(buttonPanel);
contentPane.add(textFieldPanel);
}
Run Code Online (Sandbox Code Playgroud)
Hov*_*els 10
您在调用setVisible(true)JFrame 后添加了一堆组件:
public MemberPanel(int i) throws IOException {
Container contentPane = getContentPane();
GridLayout layout = new GridLayout(2, 1);
contentPane.setLayout(layout);
setVisible(true); // ****** here
// .....
// only now do you add components...
contentPane.add(buttonPanel);
contentPane.add(textFieldPanel);
}
Run Code Online (Sandbox Code Playgroud)
因此,组件可能显示也可能不显示,具体取决于GUI是否重新绘制(请参阅重新调整空gui大小时会发生什么).修复:setVisible(true)仅在添加所有内容后调用.
| 归档时间: |
|
| 查看次数: |
2062 次 |
| 最近记录: |