gpt4 book ai didi

Java 第二个变量忽略第一个变量

转载 作者:行者123 更新时间:2023-12-02 09:01:06 25 4
gpt4 key购买 nike

我要做的是制作一个小 GUI,当 button 时单击它开始进度条 threads ;但是,我们必须使用一个实现 Runnable interface 的内部类来完成此操作。 ,并且在该类的构造函数中,它需要 String和一个 JProgressBar目的。然后在主构造函数中,我们应该实例化 2 JProgressBar对象,然后使用这些 JProgressBars 创建 2 个内部类对象.

只是我的第二个JProgressBar正在更新 thread 。我知道我的第二个JProgressBar基本上覆盖了我的第一个,但我不知道如何解决这个问题,因为如果我尝试将内部类的构造函数更改为设置 2 JProgressBar属性等于构造函数的参数,那么第一个JProgessBar完全从 GUI 中消失。这是我的代码,感谢任何可以提供帮助的人。

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Lab4Part2 {
//Attributes
private JFrame frame;
private String progress;
private JProgressBar jpb;

//Constructor
public Lab4Part2() {
//Create frame with specified grid layout
frame = new JFrame();
GridLayout grid = new GridLayout(0,1);
frame.setLayout(grid);
JButton jbClick = new JButton("Let's start this show");
frame.add(jbClick);

//Add in JProgressBars and create Inner Class objects with them
JProgressBar jpb1 = new JProgressBar();
JProgressBar jpb2 = new JProgressBar();
InnerProgress bar1 = new InnerProgress("Progress 1: ", jpb1);
InnerProgress bar2 = new InnerProgress("Progress 2: ", jpb2);

//Anonymous class for the button's action listener
jbClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread t1 = new Thread(bar1);
Thread t2 = new Thread(bar2);
t1.start();
t2.start();
}
});

//Adds inner object to frame
frame.add(bar1);
frame.add(bar2);

//Packing and stuff
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

//Inner class
class InnerProgress extends JPanel implements Runnable {

//Constructor
public InnerProgress(String pbar, JProgressBar jpBar) {
jpBar.setMinimum(0);
jpBar.setMaximum(80);
jpBar.setStringPainted(true);
progress = pbar;
jpb = jpBar;
JLabel label = new JLabel(pbar);
add(label);
add(jpBar);
}

//Thread action
public void run() {
System.out.println("We are running " + Thread.currentThread().getName());
for(int i = 1; i<= 80; i++) {
try {
Thread.currentThread().sleep((long)Math.random()*100);
jpb.setValue(i);
}
catch(InterruptedException ie) {
System.out.println(ie);
}
}
System.out.println("Thread name: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
}
}

//Main
public static void main(String[] args) {
new Lab4Part2();
}

}

最佳答案

您的内部类应该维护它自己对所传递的 JProgressBar 的引用。

只需...

private JProgressBar jpb;

并将其移动到你的内部类中

class InnerProgress extends JPanel implements Runnable {

private JProgressBar jpb;
//...

还有另外两个问题

一,(long)Math.random()*100 导致 Math.random() 的结果为 int code>,这将导致它变成 0

您需要转换操作的结果,例如...

Thread.currentThread().sleep((int)(Math.random() * 500));

(注意:我为测试设置了 500)

第二,Swing 不是线程安全的。这意味着您不应该从事件调度线程的上下文之外更新 UI。

所以,而不是...

jpb.setValue(i);

我编写了一个新方法,它将相应地更新 UI

protected void updateProgress(int value) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
InnerProgress.this.jpb.setValue(value);
}
});
}

(长话短说,这解决了从匿名上下文中引用变量的问题)

然后简单地使用 updateProgress(i); 调用它

可运行示例...

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class Lab4Part2 {
//Attributes

private JFrame frame;
private String progress;

//Constructor
public Lab4Part2() {
//Create frame with specified grid layout
frame = new JFrame();
GridLayout grid = new GridLayout(0, 1);
frame.setLayout(grid);
JButton jbClick = new JButton("Let's start this show");
frame.add(jbClick);

//Add in JProgressBars and create Inner Class objects with them
JProgressBar jpb1 = new JProgressBar();
JProgressBar jpb2 = new JProgressBar();
InnerProgress bar1 = new InnerProgress("Progress 1: ", jpb1);
InnerProgress bar2 = new InnerProgress("Progress 2: ", jpb2);

//Anonymous class for the button's action listener
jbClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread t1 = new Thread(bar1);
Thread t2 = new Thread(bar2);
t1.start();
t2.start();
}
});

//Adds inner object to frame
frame.add(bar1);
frame.add(bar2);

//Packing and stuff
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

//Inner class
class InnerProgress extends JPanel implements Runnable {

private JProgressBar jpb;

//Constructor
public InnerProgress(String pbar, JProgressBar jpBar) {
jpBar.setMinimum(0);
jpBar.setMaximum(80);
jpBar.setStringPainted(true);
progress = pbar;
jpb = jpBar;
JLabel label = new JLabel(pbar);
add(label);
add(jpBar);
}

//Thread action
public void run() {
System.out.println("We are running " + Thread.currentThread().getName());
for (int i = 1; i <= 80; i++) {
try {
Thread.currentThread().sleep((int)(Math.random() * 500));
updateProgress(i);
} catch (InterruptedException ie) {
System.out.println(ie);
}
}
System.out.println("Thread name: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
}

protected void updateProgress(int value) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
InnerProgress.this.jpb.setValue(value);
}
});
}
}

//Main
public static void main(String[] args) {
new Lab4Part2();
}

}

关于Java 第二个变量忽略第一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60143504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com