gpt4 book ai didi

java - JFrames 方法 setVisible(true) 或 setVisible(false) 不在循环中工作。框架无法正确显示

转载 作者:行者123 更新时间:2023-12-02 07:01:00 34 4
gpt4 key购买 nike

JFrame 在循环中无法正确显示。代码:-

import javax.swing.*;    
import java.awt.event.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class SwingDemo {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable(){
public void run()
{
final JFrame jfrm= new JFrame("A Simple Swing Application");

final JFrame jfrm2= new JFrame("A Simple Swing Application 2");
jfrm.setSize(275,100);

jfrm.setLocation(100,100);
jfrm2.setLocation(50,50);
jfrm2.setSize(275,100);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jfrm2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel("Swing means powerful GUIs.");
jfrm.add(jlab);
JButton button0= new JButton("loop");
jfrm.add(button0);
jfrm.setLayout(new FlowLayout());
JLabel jlab2 = new JLabel("Swing means powerful GUIs again");
jfrm2.add(jlab2);
//jfrm2.setVisible(true);

jfrm.setVisible(true);

button0.addActionListener(new ActionListener() {

private boolean confirmAction;

@Override
public void actionPerformed(ActionEvent e) {

confirmAction = true;
if (confirmAction) {
try {
while(true)
{
jfrm.setVisible(false);
jfrm2.setVisible(true);

try{
Thread.sleep(15000);
}
catch(InterruptedException ie)
{
System.out.println("nothing");
}
jfrm2.setVisible(false);
jfrm.setVisible(true);
}
} catch (Throwable t) {
t.printStackTrace(System.out);
}
}
}
});
}

});

}

}

最佳答案

如果问题是它们没有正确更新(这是我唯一的想法,因为你没有解释这个问题),你应该尝试在循环中包含这些方法:

validate();
repaint();

如果您的问题不同,请通知我们。

关于java - JFrames 方法 setVisible(true) 或 setVisible(false) 不在循环中工作。框架无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16670667/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com