java中的线程和swing组件
如您所见,我一直在研究并尝试在main.java类中设置一个线程。这是主要的方法:java中的线程和swing组件,java,multithreading,swing,Java,Multithreading,Swing,如您所见,我一直在研究并尝试在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();
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中没有显示任何内容
我的错在哪里
编辑:显示所有代码。问题#1
您试图更新的组件没有以任何方式连接到屏幕
public JTextArea estado = new JTextArea();
public JTextField updatedVersion = new JTextField();
public JLabel updatedLabel = new JLabel();
这意味着,无论何时你与这些组件交互,它都不会对屏幕上的内容产生任何影响
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"));
/*...*/
}
}
公共类检查扩展SwingWorker{
私人住宅区;
私有JTextField更新版本;
私有JLabel更新标签;
私有字符串catchUpdatedVersion;
整数更新枚举器;
int-CURRENTNUMBER;
公共检查(JTextArea estado、JTextField updatedVersion、JLabel updatedLabel){
this.estado=estado;
this.updatedVersion=updatedVersion;
this.updatedLabel=updatedLabel;
}
受保护的无效进程(列表值){
for(字符串值:值){
estado.append(值);
}
}
受保护的字符串doInBackground()引发异常{
字符串infull=”https://thread.googlecode.com/svn/trunk/thread.inf";
String name=“thread.inf”;
文件文件=新文件(名称);
URLConnection conn=新URL(infURL).openConnection();
连接();
出版(“Conectando al-servidor…”);
发布(System.getProperty(“line.separator”);
发布(“--Buscando Realizaciones…”);
发布(System.getProperty(“line.separator”);
/*...*/
}
}
如果您需要进行任何后处理,那么您还可以覆盖done
,它将在doInBackground
存在后调用,但在EDT上下文中调用
有关详细信息,请通读问题没有显示,您也没有显示应该显示的代码…请注意gui在事件调度线程中工作。@SotiriosDelimanolis ok。我列出了使用SwingWorker()所需的所有代码,然后使用process方法更新UI()。不必详细说明,这段代码看起来像是一个很大的漏洞。您将构建UI的代码与从web加载数据的行为/业务逻辑代码混为一谈。你在一个非UI线程中做与UI相关的事情,等等。我不知道,从哪里开始…是的,我通过实现swingworker方法解决了问题1和2,现在一切正常。我的程序应该下载一个文件,并在progressbar中表示其百分比。它实际上下载了文件,但progressbar直到下载完成才移动,然后突然标记为100%。我看不到介于0和100之间的“动画”
SwingWorker
本身有能力提供进度反馈,例如,