-1

如您所见,我一直在研究并尝试在main.java类中设置一个线程。这是主要方法:

public static void main(String args[]) {     
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new main().setVisible(true);
            check ch = new check();
            ch.start();          
        }
    });
}

Main 方法从check.java类调用一个名为ch的线程。

这是线程类:

public class check extends Thread {

    public JTextArea estado = new JTextArea();   
    public JTextField updatedVersion = new JTextField();
    public JLabel updatedLabel = new JLabel();
    public String catchUpdatedVersion;
    int UPDATENUMBER;
    int CURRENTNUMBER;

    public void run() {
        String infURL = "https://thread.googlecode.com/svn/trunk/thread.inf";
        String name = "thread.inf";
        File file = new File(name);
        try {
            URLConnection conn = new URL(infURL).openConnection();
            conn.connect();
            estado.append("Conectando al servidor...");
            estado.append(System.getProperty("line.separator"));
            estado.append(" -- Buscando actualizaciones... --");
            estado.append(System.getProperty("line.separator"));
            InputStream in = conn.getInputStream();
            OutputStream out = new FileOutputStream(file);
            int b = 0;
            while (b != -1) {
                b = in.read();
                if (b != -1) {
                    out.write(b);
                }
            }
            out.close();
            in.close();
        } catch (MalformedURLException ex) {
        } catch (IOException ioe) { }

        String fileToReadUpdatedVersion = "thread.inf";
        try {
            BufferedReader br = new BufferedReader(
                    new FileReader(fileToReadUpdatedVersion));
            String brr = br.readLine();
            catchUpdatedVersion = brr.substring(34,42);
            String catchUpdatedShortVersion = brr.substring(15,16);
            UPDATENUMBER = Integer.parseInt(catchUpdatedShortVersion);

            String fileToReadCurrentVer = "thread.inf";
            BufferedReader brrw = new BufferedReader(
                                new FileReader(fileToReadCurrentVer));
            String brrwREAD = brrw.readLine();
            String catchCurrentShortVersion = brrwREAD.substring(15,16);
            CURRENTNUMBER = Integer.parseInt(catchCurrentShortVersion);

            if (CURRENTNUMBER >= UPDATENUMBER) {
                estado.setText("No se han encontrado actualizaciones.");
            } else {
                updatedVersion.setForeground(new Color(0,102,0));
                updatedLabel.setForeground(new Color(0,153,51));
                updatedVersion.setText(catchUpdatedVersion);
                estado.append("-------------------" +
                        "NUEVA ACTUALIZACIÓN DISPONIBLE: " +
                            catchUpdatedVersion + " -------------------");;
                estado.append(System.getProperty("line.separator"));
                estado.append("Descargando actualizaciones... " +
                            "Espere por favor, no cierre este " +
                                "programa hasta que esté completado...");
                try {
                    String updateURL = "https://thread.googlecode.com/" +
                                                    "svn/trunk/thread.inf";
                    String updatedname = (catchUpdatedVersion + ".zip");
                    File updatedfile = new File(updatedname);
                    URLConnection conn = new URL(updateURL).openConnection();
                    conn.connect();
                    estado.append(System.getProperty("line.separator"));
                    estado.append("   Archivo actual: " + updatedname);
                    estado.append(System.getProperty("line.separator"));
                    estado.append("   Tamaño: " + 
                        conn.getContentLength() / 1000 / 1000 + " MB");
                    InputStream in = conn.getInputStream();
                    OutputStream out = new FileOutputStream(updatedfile);
                    int c = 0;
                    while (c != -1) {
                        c = in.read();
                        if (c != -1) {
                            out.write(c);
                        }
                    }
                    out.close();
                    in.close();    
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException ioe) {
            System.out.println(ioe);
            ioe.printStackTrace();
        }
    }
}

当我运行程序时,线程无法正常工作。它应该下载一个文件,然后在main.java类的 JTextArea 中显示其进度。它确实下载了文件,但在 JTextArea 中没有出现任何内容。

我的错误在哪里?

编辑:显示所有代码。

4

1 回答 1

1

问题 #1

您尝试更新的组件无论如何都不会连接到屏幕...

public JTextArea estado = new JTextArea();   
public JTextField updatedVersion = new JTextField();
public JLabel updatedLabel = new JLabel();

这意味着,无论何时您与这些组件进行交互,它都不会对屏幕上的内容做任何事情......

问题 #2

您正在尝试从事件调度线程的上下文之外对 UI 进行修改。这严重违反了 Swing 线程规则。

public class Check extends SwingWorker<String, String> {

    private JTextArea estado;   
    Private JTextField updatedVersion;
    private JLabel updatedLabel;
    private String catchUpdatedVersion;
    int UPDATENUMBER;
    int CURRENTNUMBER;

    public Check(JTextArea estado, JTextField updatedVersion, JLabel updatedLabel) {
        this.estado = estado;
        this.updatedVersion = updatedVersion;
        this.updatedLabel = updatedLabel;
    }

    protected void process(List<String> values) {
        for (String value : values) {
            estado.append(value);
        }
    }

    protected String doInBackground() throws Exception {
        String infURL = "https://thread.googlecode.com/svn/trunk/thread.inf";
        String name = "thread.inf";
        File file = new File(name);

        URLConnection conn = new URL(infURL).openConnection();
        conn.connect();
        publish("Conectando al servidor...");
        publish(System.getProperty("line.separator"));
        publish(" -- Buscando actualizaciones... --");
        publish(System.getProperty("line.separator"));
        /*...*/          
    }
}

如果您需要进行任何后处理,那么您还需要覆盖done将在doInBackground已存在后调用的,但在 EDT 的上下文中调用

有关更多详细信息,请阅读Swing 中的并发

于 2013-09-28T20:54:00.723 回答