热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JPanel直到代码之后才显示-JPaneldoesnotshowuntilafterthecode

WhatIwanttodoistoshowanewpanelwithaprogressbarwhiletheprocesstakesplace.我想要做的是在

What I want to do is to show a new panel with a progress bar while the process takes place.

我想要做的是在进程发生时显示带有进度条的新面板。

My code is as follows:

我的代码如下:

case "GoButton":
    cards.show(this, "pp");
    this.test();
    cards.show(this, "panel1");
    break;

That is in a actionPerformed method

这是在actionPerformed方法中

but when I run the program it just ignores the first cards.show, it executes the test code and then it executes the second cards.show

但是当我运行程序时它只是忽略了第一个cards.show,它执行测试代码,然后执行第二个cards.show

I also provide you the rest of the code in case you need it

如果您需要,我还会为您提供其余的代码

public CreatePanel(JFrame frame) {
        ext = new ArrayList<>();
        files = new ArrayList<>();

        this.mainWindow = frame;
        this.setSize(256, 256);
        cards = new CardLayout();
        this.setLayout(cards);

        panel1 = new JPanel(null);
        createPanel1(panel1);
        this.add(panel1, "panel1");

        panel2 = new JPanel(null);
        createPanel2(panel2);
        this.add(panel2, "panel2");

        panel3 = new JPanel(null);
        createPanel3(panel3);
        this.add(panel3, "pp");

        cards.show(this, "panel1");
    }

public class ProgressPanel {
    private static JPanel panel;
    private static JProgressBar progressBar;
    private static int progress;

    public static JPanel createProgressPanel(){
        ProgressPanel.panel = new JPanel(null);
        ProgressPanel.initPanel();
        return ProgressPanel.panel;
    }

    private static void initPanel(){
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(progress);
        progressBar.setStringPainted(true);
        panel.add(progressBar, c);
    }

    public static void updateProgress(int x){
        ProgressPanel.progress += x;
    }

    public void resetProgress(){
        this.progress = 0;
    }
}

1 个解决方案

#1


Your problem likely likes here: this.test();. I'm betting that this method calls long running code, and since it's being called on the Swing event thread, it's tying up this thread preventing your GUI from updating itself and in fact completely freezing the GUI. Solution: use a SwingWorker or other background thread for long-running tasks. Please have a look at Concurrency in Swing.

你的问题可能就像这里:this.test();.我打赌这个方法调用长时间运行的代码,并且由于它是在Swing事件线程上调用的,它正在占用这个线程,阻止你的GUI自我更新,实际上完全冻结了GUI。解决方案:使用SwingWorker或其他后台线程进行长时间运行的任务。请看一下Swing中的Concurrency。

Note that if you do use a SwingWorker, you'll need to use either a PropertyChangeListener or the done() method to be notified when the background thread has completed and that it's time to call cards.show(this, "panel1");. Myself, I'd add a PropertyChangeListener to my SwingWorker, since this way, I could both listen for the completion of the listener, and also could listen for changes in the worker's progress property, and then use that value to set my JProgressBar's value.

请注意,如果您使用SwingWorker,则需要使用PropertyChangeListener或done()方法在后台线程完成时通知并且是时候调用cards.show(this,“panel1”); 。我自己,我将一个PropertyChangeListener添加到我的SwingWorker,因为这样,我既可以监听监听器的完成,也可以监听worker的progress属性的变化,然后使用该值设置我的JProgressBar的值。


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • Java SE从入门到放弃(三)的逻辑运算符详解
    本文详细介绍了Java SE中的逻辑运算符,包括逻辑运算符的操作和运算结果,以及与运算符的不同之处。通过代码演示,展示了逻辑运算符的使用方法和注意事项。文章以Java SE从入门到放弃(三)为背景,对逻辑运算符进行了深入的解析。 ... [详细]
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
author-avatar
泉怪的皮毛_884
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有