GUI 添加了组件但显示时显示空白 [英] GUI added components but when displayed shows blank

查看:77
本文介绍了GUI 添加了组件但显示时显示空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我在 main 中运行代码时它可以工作,但目前我只有在 JButton 时运行代码

For some reason, when I run the code in the main it works but currently I have the code only run when a JButton is

在此处输入链接描述

import java.awt.Color;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JProgressBar;


public class JavaCompilingJOP {

    private JFrame framed = new JFrame();
    private JProgressBar bar = new JProgressBar(0,100); //0 through 100 is the compiling until reaches that number
    private Color startProgress = Color.RED;
    private int loadingPercent = 0;

    public JavaCompilingJOP(){
        framed.setLayout(null);
        framed.setSize(500, 200);
        framed.setTitle("Compiling JOptionPane...");
        framed.setLocationRelativeTo(null);
        //Ok now lets make the GUI happen boys!
        framed.getContentPane().setBackground(Color.BLACK); //Because startProgress is ze theme of ze game!

        bar.setForeground(startProgress);
        bar.setStringPainted(true);
        bar.setValue(loadingPercent);
        bar.setString(loadingPercent + "%");
        bar.setBounds(50, 50, 400, 100);
        framed.add(bar);

        System.out.println("Make visible an run!");

        framed.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        framed.setResizable(false);

        framed.setVisible(true);

        doTimer();
    }

    private void doTimer(){
        Random obj = new Random();
        for(int i = 0; i < 100; i++){
            loadingPercent++;
            bar.setValue(loadingPercent); //Updates counter
            bar.setString(loadingPercent + "%"); //Updates progressbar
            startProgress = new Color(startProgress.getRed()-2,startProgress.getGreen()+2,startProgress.getBlue());
            bar.setForeground(startProgress);
            try{Thread.sleep(100 + obj.nextInt(200));}catch(Exception E){}

        }
        System.out.println("Java compiled!");
        framed.setVisible(false);
    }
}

出于某种原因,我在调用时得到的只是一个白色屏幕,它似乎忽略了关闭时的处理,因为当我点击关闭它时......它也没有.请记住,当我在 main 中运行它时,它可以工作,但是当在按钮的 ActionListener 中时,它给了我一个仍然执行进度条内容的空白屏幕.我坚持这一点.

For some reason all I am getting though when called is a white screen that seems to neglect disposing on close because when I hit to close it.. it doesn't either. Keep in mind that when I run it in the main it works but when inside the ActionListener of the button, it gives me a blank screen that still performs it's progressbar stuff. Im stuck on this.

推荐答案

您的 JButton 的 actionPerformed() 代码在 Java 事件调度线程 (EDT) 上执行,这这就是 GUI冻结"并且您看到黑屏的原因.这样做还会阻止关闭"事件以及任何其他摆动事件的执行.
重要提示:
EDT 是一个特殊的线程,应该非常小心地处理.
请花点时间阅读 EDT 以及如何正确使用它此处.

Your JButton's actionPerformed() code gets executed on the Java event dispatch thread (EDT) and this is why the GUI "freezes" and you see a black screen.
Doing so, also blocks the "close" event from being executed as well as any other swing event.
Important:
The EDT is a special thread which should be handled very carefully.
Please take the time and read on EDT and how to properly use it here.

正确方法:
doTimer() 应该永远在 Java 事件调度线程 (EDT) 上运行,因为它模拟(我希望)一些长时间运行的任务.
为了确保我们正确处理 EDT,我们使用 SwingWorker.
SwingWorkers 是一种特殊结构,旨在在后台线程中执行冗长的 GUI 交互任务.
由于这是第一次处理有点棘手,我为您添加了以下代码:

Correct method:
doTimer() should never run on Java event dispatch thread (EDT) since it simulates (I hope) some long-running task.
To make sure we handle the EDT correctly we use a SwingWorker.
SwingWorkers are special constructs designed to perform lengthy GUI-interaction tasks in a background thread.
Since this is kinda tricky to handle for the first time, I added the following code for you:

@Override
public void actionPerformed(ActionEvent e) {
    SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
        @Override
        protected Object doInBackground() throws Exception {
            // Code here will be executed in a background thread
            // This is where all background activities should happen.
            doTimer();
            return null;
        }
    };
    sw.execute();
    //  doTimer(); Bad Idea as this will execute on EDT!
}

这篇关于GUI 添加了组件但显示时显示空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆