- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个项目,需要一个进度条。我已经有了带有计时器的类(class),当我包含一个 main 时,它运行良好;但是当我尝试在 mainGUI 方法中调用它时,它全黑,直到它达到 100% 然后出现。
package microproject.resources;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Timer extends JFrame {
JProgressBar current;
JTextArea out;
JButton find;
Thread runner;
int num = 0;
int length = 0;
public Timer() {
setTitle("Progress");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
length = Integer.parseInt(JOptionPane.showInputDialog(null, "How many seconds:"));
JPanel p = new JPanel(new GridLayout(1,1));
p.setPreferredSize(new Dimension(300,65));
current = new JProgressBar(0, length);
current.setPreferredSize(new Dimension(250,50));
current.setValue(0);
current.setStringPainted(true);
p.add(current);
setVisible(true);
setContentPane(p);
pack();
setVisible(true);
iterate();
}
public void iterate() {
while(num < length +1) {
current.setValue(num);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
num += 1;
}
}
public static void main(String[] args) {
Timer f = new Timer();
}
}
这是计时器类的代码 ^
package microproject.resources;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUIMain extends JFrame {
public static void main(String []args){
GuiFrame();
}
public static void GuiFrame(){
JFrame frame = new JFrame("Casino Royal3");
frame.setSize(811,577);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2,1));
frame.setResizable(false);
JPanel PNorth = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel PSouth = new JPanel(new BorderLayout());
//Creating Image for Casino Button
ImageIcon img1 = new ImageIcon("src\\Casino.jpg");
final JButton btn1 = new JButton(img1);
btn1.setPreferredSize(new Dimension(550,274));
btn1.setMargin(new Insets(0,0,0,0));
PNorth.add(btn1, BorderLayout.EAST);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn1.setIcon(new ImageIcon("src\\Casino2.jpg"));
}
});
//Creating Image for Sheridan Label
ImageIcon img2 = new ImageIcon("src\\SHERIDAN_LOGO.jpg");
JButton btn2 = new JButton(img2);
btn2.setMargin(new Insets(0,0,0,0));
PNorth.add(btn2);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon instruc = new ImageIcon("src\\Instructions.jpg");
JLabel instructions = new JLabel(instruc);
JOptionPane.showConfirmDialog(null, instructions, "instructions", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE);
}
});
JPanel timmus = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
timmus.setPreferredSize(new Dimension(166, 273));
timmus.setBackground(Color.BLUE);
ImageIcon time = new ImageIcon("src\\Timer.jpg");
JButton timer = new JButton(time);
timer.setMargin(new Insets(0,0,0,0));
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Timer f = new Timer();
}
});
timmus.add(timer);
ImageIcon mus = new ImageIcon("src\\music.jpg");
JButton music = new JButton(mus);
music.setMargin(new Insets(0,0,0,0));
timmus.add(music);
JPanel games = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
games.setPreferredSize(new Dimension(500,279));
games.setBackground(Color.BLUE);
ImageIcon calculator = new ImageIcon("src\\Calculator.jpg");
JButton calc = new JButton(calculator);
calc.setMargin(new Insets(0,0,0,0));
calc.setPreferredSize(new Dimension(166,273));
games.add(calc);
calc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calculator c1 = new Calculator();
}
});
ImageIcon g1 = new ImageIcon("src\\250Hangman.jpg");
JButton game1 = new JButton(g1);
//game1.setBackground(Color.WHITE);
game1.setMargin(new Insets(0,0,0,0));
game1.setPreferredSize(new Dimension(166,273));
games.add(game1);
game1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Hangman h1 = new Hangman();
}
});
ImageIcon g2 = new ImageIcon("src\\Minesweeper.jpg");
JButton game2 = new JButton(g2);
// game2.setBackground(Color.WHITE);
game2.setMargin(new Insets(0,0,0,0));
game2.setPreferredSize(new Dimension(166,273));
games.add(game2);
PSouth.add(timmus, BorderLayout.CENTER);
PSouth.add(games, BorderLayout.EAST);
frame.add(PNorth, BorderLayout.NORTH);
frame.add(PSouth, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
}
这就是整个程序,Timer ActionListener 称为“计时器”
提前致谢
最佳答案
欢迎来到阻塞事件调度线程(以及违反初始线程)的奇妙世界
基本上,Swing 是一个单线程环境,对 UI 的所有更新和修改都应该在事件调度线程(AKA EDT)的上下文中执行。
EDT 负责处理重绘请求等。如果由于某种原因,您阻塞了该线程(例如,使用长时间运行的循环或阻塞 IO),它将阻止 EDT 处理新的绘制请求,使其看起来好像您的程序已挂起......因为本质上它有。
您可能会发现直接运行 Timer
与在 GUI 中使用它之间存在差异的原因是,当应用程序启动时,它将在通常称为“主程序”的内部运行。 “线程。
当您第一次创建顶级 Swing 容器时,EDT 会启动(这是一个单独的线程),这意味着 UI 将出现在它自己的线程中,但应用程序将继续在“主”线程中运行,从而允许您的iterate
方法独立于 EDT 运行。
但是,当您尝试从 GUI 中运行它时,它全部在 EDT 上下文中运行,导致它被阻止。
首先看一下
要解决该问题,根据您的示例代码,我建议使用 SwingWorker
。这将允许您在后台线程中运行“长时间运行的任务”,但提供了许多方法,允许您将更新重新同步回 EDT。这非常重要,因为您永远不应该尝试从 EDT 之外的任何线程更新 UI 或更改其状态。
看看Worker Threads and SwingWorker了解更多详情
如果需要,可以提供一些示例...
关于java - JProgressBar - 在我调用它的类中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825069/
我正在尝试将 2 个 JProgressBars 组合在一起,以便我可以表示有关单个事物的统计信息。 我的问题是如何将两个进度条组合或叠加才能起作用? 我想到的一个解决方案是将 2 个进度条放在一个
继续 Setting the colors of a JProgressBar text我希望能够根据进程状态在我的程序中设置 JProgressBar 的文本颜色,而不偏离系统外观。 来自 Sett
注意到这个链接后: http://www.newscientist.com/blogs/nstv/2010/12/best-videos-of-2010-progress-bar-illusion.h
我正在尝试在这个问题的投票答案中找到的代码:Download file using java apache commons? 是一个下载应用,稍微看一下,(我对JFrames和ActionEvents
我只是想在 JFrame 中使用 Windows L&F .现在在使用进度条时,默认颜色是绿色,类似于其他 windows 功能中使用的颜色,如复制文件等。在复制文件的情况下覆盖)。如何在我的进度条的
你好堆栈交换器, 我在 java Swing 中的进度条有问题。我认为我的困惑源于我对线程和 Swing 事件队列的理解不足(我不太了解 java 线程,以及 AWTEventQueue 上到底发生了
如何从另一个线程更新 JProgressBar.setValue(int)? 我的次要目标是尽可能少地上课。 这是我现在拥有的代码: // Part of the main class.... pp.
我正在尝试使用 JProgressBar 作为简单游戏的健康栏。我试图让它完整且可见以启动程序。这是我的 JProgressBar 代码: progressBar = new JProgres
我无法更新我的进度条...这是我的代码 Thread t=new Thread(new Runnable(){ public void run(){ int i
您好,我想知道是否可以扩展 JProgressBar 以使用 double 值来表示最小值最大值,而不是 int。谢谢。 最佳答案 如果您知道值的范围和精度,那么使用包装器就很容易做到。我已将这种方法
我正在尝试将 JProgressBar 添加到我的程序中,但它不会更新!该值仅在推理 100% 后才会更改。这是我的方法。 public void downloadImages(List images
我正在使用 JProgressBar 在我的框架上显示进度条。进度条设置为不确定模式,因为我不知道任务何时结束。显示的不是正常的进度条,而是奇怪的橙色波浪。 任务运行时波浪会持续移动。结束后,该值设置
我最近的项目遇到了一个严重的问题:我正在一个名为 writer(...) 的方法中进行长时间比较,大约需要 20 秒。该方法在 View 类中进行标记,我的 GUI 元素也在其中设置。当方法进行比较时
以下是将图像转换为字节数组的简单代码(本论坛已显示): File imgPath= new File(textFiled_Path.getText()); BufferedImage original
我已经尝试从库接收字节来设置我的 JProgressBar 更新很长一段时间,但不幸的是我没有得到我想要的结果。 问题是我不知道如何从lib接收字节以及如何在接收字节时更新JProgressBar所有
我想在我的代码中添加一个 JProgressBar,它计数到 5(以秒为单位)。之后,它将产生新的值,但我当前的问题是,我什至无法用我的 ActionListener 制作 ProgressBar,所
我在函数中使用 POI 来填充 Excel 文档的内容(需要 10 秒),当我调用函数时,我想使用 JProgressBar 来查看进度,但按钮和程序被阻止,我需要在其他线程中制作吗?我该怎么做?我的
我编写了一个非常简单的代码来在此处显示它,我有一个按钮应该显示 JDialog 来检查进度状态,我正在使用延迟调用来完成 EDT 并且我的循环不在 run 方法中,那么为什么我的栏没有更新呢?这是代码
我一直在尝试更改 jProgressBar 颜色,但一直是橙色 这是属性 运行 我想将颜色更改为绿色或其他颜色,但不知道如何实现 最佳答案 尝试这个教学示例。它将修改 JProgressBar 的 4
我在创 build 置为不确定的 JProgressBar 时遇到问题。以下代码是我对 JProgressBar 的实现,是从另一个类调用/构造的: public class Progress imp
我是一名优秀的程序员,十分优秀!