JFrame的GUI不显示

时间:2019-09-08 12:08:58

标签: java swing jframe jpanel

我希望我的程序运行循环,直到单击JPanel按钮为止。 我编写的代码确实运行了循环。问题在于,JPanel按钮甚至没有显示在屏幕上。 THX有任何建议!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Test extends JFrame implements ActionListener {       

    private static final long serialVersionUID = 1L;
    private static boolean endLoop = false;     

    @Override
    public void actionPerformed(ActionEvent e) {
        endLoop = true;     
    }

    private void initFrame() {
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        add(panel);
        JButton button1 = new JButton("Stop");
        button1.addActionListener(this);
        add(button1);
    }   

    public void startBot() {
        initFrame();        
        while (!endLoop) {  
                    System.out.println("looping...");           
        }
        System.out.println("Loop teminated!");
    }   
}

0 个答案:

没有答案