Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Simple cold扩展SwingWorker以帮助在后台计算值_Java - Fatal编程技术网

Java Simple cold扩展SwingWorker以帮助在后台计算值

Java Simple cold扩展SwingWorker以帮助在后台计算值,java,Java,这是一个简单的代码,它扩展了SwingWorker以帮助在后台计算值。 但是当我运行代码时,它只是跳到finished。例如,如果我输入一个数字5,它应该显示54231finished。 我想我的声明可能有问题 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io

这是一个简单的代码,它扩展了SwingWorker以帮助在后台计算值。 但是当我运行代码时,它只是跳到finished。例如,如果我输入一个数字5,它应该显示
54231finished。
我想我的声明可能有问题

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class LabResponsiveGUI extends JFrame implements ActionListener {

    private final String TEXT_COUNT_DOWN = "Count down!";
    private final String TEXT_CANCEL = "Cancel!";

    private JTextField txt = new JTextField("3");
    private JButton button = new JButton(TEXT_COUNT_DOWN);
    //private JButton button1 = new JButton(TEXT_CANCEL);

    private JTextArea txtOutput = new JTextArea(10, 20);

    public LabResponsiveGUI() {
        super("Simple GUI example");
        setSize(400, 400);
        button.addActionListener(this);
        //button1.addActionListener(this);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        add(txt, BorderLayout.NORTH);
        button.setPreferredSize(new Dimension(200, 50));
        add(button, BorderLayout.SOUTH);
        JScrollPane scroll = new JScrollPane(txtOutput);
        add(scroll, BorderLayout.CENTER);
    }


    public void actionPerformed(ActionEvent e) {
        int value ;

        BackgroundTask bgt = new BackgroundTask();

        bgt.execute();

        try {
            value = Integer.parseInt(txt.getText());
            if (value <= 0) {
                JOptionPane.showMessageDialog(this, "Please enter a number greater than zero");
                return;
            }

        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Please enter a valid integer");
            return;
        }
    }


    public class BackgroundTask extends SwingWorker<Void,String> {
        private int value;

        public  void BackgroudTask(int value){
            this.value = value;
        }

        @Override
        protected Void doInBackground() throws Exception {

            try {
                int count =  value;
                String line = null;
                while (count > 0) {
                    line = String.valueOf(count);
                    txtOutput.append(line + System.getProperty("line.separator"));
                    count--;
                    Thread.sleep(1000);
                }
                txtOutput.append("Finished! ");
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    }

    public static void main(String[] agrs){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                LabResponsiveGUI frame = new LabResponsiveGUI();
                frame.setVisible(true);
            }
        });
    }
}
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.concurrent.ExecutionException;
导入javax.swing.Icon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入javax.swing.SwingWorker;
在关闭时导入静态javax.swing.WindowConstants.DISPOSE\u;
公共类LabResponsiveGUI扩展JFrame实现ActionListener{
私有最终字符串文本\u COUNT\u DOWN=“COUNT DOWN!”;
私有最终字符串文本_CANCEL=“CANCEL!”;
私有JTextField txt=新JTextField(“3”);
私有JButton按钮=新JButton(文本计数向下);
//私有JButton button1=新JButton(文本_取消);
私有JTextArea txtOutput=新的JTextArea(10,20);
公共LabResponsiveGUI(){
超级(“简单GUI示例”);
设置大小(400400);
addActionListener(这个);
//按钮1.addActionListener(此按钮);
setDefaultCloseOperation(在关闭时处理);
添加(txt,BorderLayout.NORTH);
按钮。设置首选尺寸(新尺寸(200,50));
添加(按钮,BorderLayout.SOUTH);
JScrollPane scroll=新的JScrollPane(txtOutput);
添加(滚动、边框布局、居中);
}
已执行的公共无效操作(操作事件e){
int值;
BackgroundTask bgt=新的BackgroundTask();
bgt.execute();
试一试{
value=Integer.parseInt(txt.getText());
如果(值为0){
行=字符串。值(计数);
append(line+System.getProperty(“line.separator”);
计数--;
睡眠(1000);
}
追加(“完成!”);
}捕获(例外情况除外){
例如printStackTrace();
}
返回null;
}
}
公共静态void main(字符串[]agrs){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
LabResponsiveGUI框架=新的LabResponsiveGUI();
frame.setVisible(true);
}
});
}
}
更改方法

public void BackgrodTask(int value) {


并在确定
值后调用新构造函数

您的代码是否正在编译,因为我没有看到用于BackGroundTask的空构造函数?改进的代码格式编辑它会让您自己大吃一惊。
public BackgroundTask(int value) {