英文:
Panel visible only after resizing [Java]
问题
以下是翻译好的内容:
我在我的代码中遇到了奇怪的问题。我之前遇到过同样的问题,所以我从头开始做了这个项目,但是同样的问题又发生了。
我将面板设置为不可见,但是我不知道为什么在不调整应用程序大小的情况下无法显示新的面板。
以下是我遇到错误的代码部分:
public void classAttitudes(){
variables.classesTitleLabel.setVisible(false);
variables.classesTitlePanel.setVisible(false);
variables.continueClassPanel.setVisible(false);
variables.titleAttitudePanel = new JPanel();
variables.titleAttitudePanel.setBounds(50,50,100,100);
variables.con.add(variables.titleAttitudePanel);
}
英文:
I got weird problem with my code. I got same problem earlier so I did the project from scratch, but same problem happens again.
I set the panel to not visible, but I do not know why I cannot display a new panels without resizing a app.
Main.java : https://pastebin.com/SPt5bqpF
UI.java : https://pastebin.com/HawM8CTd
Variables.java : https://pastebin.com/QvPPALkU
Code where I have a bug:
public void classAttitudes(){
variables.classesTitleLabel.setVisible(false);
variables.classesTitlePanel.setVisible(false);
variables.continueClassPanel.setVisible(false);
variables.titleAttitudePanel = new JPanel();
variables.titleAttitudePanel.setBounds(50,50,100,100);
variables.con.add(variables.titleAttitudePanel);
}
答案1
得分: -1
public void classAttitudes() {
variables.classesTitleLabel.setVisible(false);
variables.classesTitlePanel.setVisible(false);
variables.continueClassPanel.setVisible(false);
variables.titleAttitudePanel = new JPanel();
variables.titleAttitudePanel.setBounds(50, 50, 100, 100);
variables.con.add(variables.titleAttitudePanel);
// ADD MANUAL REPAINT TO THE FRAME !!!
variables.window.repaint();
}
P.S. 当调整主窗口大小(即 JFrame
)时,会触发一个动作,在此动作结束时会调用 repaint()
,以便使用 UI 线程中的所有内容重新绘制此窗口(我不记得它的名称了)。
英文:
public void classAttitudes() {
variables.classesTitleLabel.setVisible(false);
variables.classesTitlePanel.setVisible(false);
variables.continueClassPanel.setVisible(false);
variables.titleAttitudePanel = new JPanel();
variables.titleAttitudePanel.setBounds(50, 50, 100, 100);
variables.con.add(variables.titleAttitudePanel);
// ADD MANUAL REPAINT TO THE FRAME !!!
variables.window.repaint();
}
P.S. When you resize you manin window (i.e. JFrame
) an action is thrown and at the end of this action repaint()
is invoke to repaint this frame with all content from UI thread (I do not remember it's name)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论