为什么每次我从 JButton 调用它时我的框架都是不可见的?

发布于 2025-01-18 03:23:01 字数 2224 浏览 2 评论 0原文

我正在为我们的小组项目创建一个加载页面,但是每次我调用加载页面类时,它都是不可见的。

这是我的主类:

public static void main(String[] args) {
    
    new caller();
    
}

这是一个按钮,当您单击该按钮时,您将进入加载页面类。

JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");

caller(){
    
    btn.setBounds(20, 30, 120, 50);
    btn.setFocusable(false);
    btn.addActionListener(this);
    
    
    
    frame.setSize(420, 420);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(null);
    frame.add(btn);
    
}
@Override
public void actionPerformed(ActionEvent e) {
    
    if(e.getSource()==btn) {
        
        frame.dispose();
        new loading_page();
        
        
    }
}

这是加载页面类,

 JLabel lablab = new JLabel();
 JProgressBar bar = new JProgressBar();
 JFrame frame = new JFrame();

loading_page(){
    
    lablab.setBackground(Color.WHITE);
    lablab.setHorizontalAlignment(SwingConstants.CENTER);
    lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
    lablab.setBounds(65, 40, 367, 215);
    
    bar.setForeground(Color.cyan);
    bar.setBounds(10, 300, 480, 17);
    bar.setValue(0);
    bar.setStringPainted(true);
    bar.setFont(new Font("MV Boli", Font.BOLD, 15));
    bar.setForeground(Color.CYAN);
    bar.setBackground(Color.BLACK);
    
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(400, 200, 500, 335);
    frame.setVisible(true);
    frame.setLayout(null);
    
    frame.getContentPane().setBackground(Color.BLACK);
    frame.getContentPane().add(lablab);
    frame.getContentPane().add(bar);
    
    
    int counter =0;
    
    while(counter<=100) {
        
        bar.setValue(counter);
        try {
            Thread.sleep(50);
            if(counter==100) {
                frame.dispose();
            }
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        }
        counter +=1;
    
}
    {
        new caller();
    }
}

我认为它位于 while(counter<==100) 语句中。 我使用frame dispose来处理前一个类中的框架,但是当系统调用第三个类(即加载页面类)时,它是不可见的。

I'm creating a Loading Page for our group project, but every time I call the loading page class, it is invisible.

Here is my Main class:

public static void main(String[] args) {
    
    new caller();
    
}

Here is the button that when you click you will go into the loading page class.

JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");

caller(){
    
    btn.setBounds(20, 30, 120, 50);
    btn.setFocusable(false);
    btn.addActionListener(this);
    
    
    
    frame.setSize(420, 420);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(null);
    frame.add(btn);
    
}
@Override
public void actionPerformed(ActionEvent e) {
    
    if(e.getSource()==btn) {
        
        frame.dispose();
        new loading_page();
        
        
    }
}

And here is the loading page class

 JLabel lablab = new JLabel();
 JProgressBar bar = new JProgressBar();
 JFrame frame = new JFrame();

loading_page(){
    
    lablab.setBackground(Color.WHITE);
    lablab.setHorizontalAlignment(SwingConstants.CENTER);
    lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
    lablab.setBounds(65, 40, 367, 215);
    
    bar.setForeground(Color.cyan);
    bar.setBounds(10, 300, 480, 17);
    bar.setValue(0);
    bar.setStringPainted(true);
    bar.setFont(new Font("MV Boli", Font.BOLD, 15));
    bar.setForeground(Color.CYAN);
    bar.setBackground(Color.BLACK);
    
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(400, 200, 500, 335);
    frame.setVisible(true);
    frame.setLayout(null);
    
    frame.getContentPane().setBackground(Color.BLACK);
    frame.getContentPane().add(lablab);
    frame.getContentPane().add(bar);
    
    
    int counter =0;
    
    while(counter<=100) {
        
        bar.setValue(counter);
        try {
            Thread.sleep(50);
            if(counter==100) {
                frame.dispose();
            }
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        }
        counter +=1;
    
}
    {
        new caller();
    }
}

I think it's in the while(counter<==100) statement.
I used the frame dispose to dispose the frame from the previous class, but when the system call the 3rd class which is the loading page class, it's invisible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文