面板仅在调整大小后可见 [Java]

huangapple go评论67阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年8月19日 05:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63476874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定