线程访问的问题

BtInside 2007-01-08 07:58:36
我想做一个东西,显示网络接收到的数据,每次发送的数据都是4个int类型的数,然后有四个progressBar显示。收到一个Socket连接后我想通过Swing的界面显示出来,而连接的Socket的东西我单独做到一个类里面,而且做了线程。因为前面相当一个服务器,对处理Socket连接做了一个线程,而这个线程里面的run方法中只要一调用Swing中的组件(比如JProgressBar)就抛出线程异常。曾经看到有帖子说用户线程访问Swing线程中的东西就会抛异常……那如何才能做到用户线程访问Swing主线程中的组件呢?
谁有类似的可运行代码?:) 保证一个星期内揭帖,大家多多帮忙.
...全文
378 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
BtInside 2007-01-12
  • 打赏
  • 举报
回复
嗯...看来是时候揭帖了
ls的我有空发给你吧
emin_lee 2007-01-10
  • 打赏
  • 举报
回复
已经发至你邮箱了:)
希望对你有帮助!
emin_lee 2007-01-10
  • 打赏
  • 举报
回复
一时也说不清楚,
我发个例子给你吧
你的邮箱?
BtInside 2007-01-10
  • 打赏
  • 举报
回复
楼上的可以具体一点吗?我有点不懂谢谢
jingfeng198 2007-01-10
  • 打赏
  • 举报
回复
emin_lee()
也给我发一个吧。
zhenhua.zhou@chinacreator.com
BtInside 2007-01-10
  • 打赏
  • 举报
回复
:)谢谢 lee 你的代码比较大

我会慢慢看的,我相信对我的作用肯定会很大(顺便佩服一下你...代码的结构很清晰,高手)

呵呵 我周五再揭帖,lee你的分数我也是给定的了

希望周五之前会有更多的相关的代码 :)
BtInside 2007-01-09
  • 打赏
  • 举报
回复
首先谢谢lixiaoxue85(蛮野蛮) 提供的代码,呆会我试一下
现在程序不在我的机子上 无法贴出异常,大概就是说thread has not owner(??好象是这样的)
我要的效果就是在一个面板上有4条不停变化的进度条,而决定这4条进度条显示的刻度值由客户端发送的值决定(客户端不停的发一个int[1][4]数组) :)
大头贼 2007-01-09
  • 打赏
  • 举报
回复
显示进度条的应该单独做一个线程,不要在接收线程中直接调用processbar
BtInside 2007-01-09
  • 打赏
  • 举报
回复
-_-!...什么作业帖 我只是把我在工作遇到的一些问题简化一下请教大家
lixiaoxue85(蛮野蛮) 谢谢你的代码 我看了
不过感觉和我的需求还有一定的距离..不过分数是给定你的了 哈哈
希望大家多发一些接近我需求的程序上来 谢谢
windproof 2007-01-09
  • 打赏
  • 举报
回复
作业帖!鉴定完毕!
xiaoyetao08 2007-01-09
  • 打赏
  • 举报
回复
up



............
  • 打赏
  • 举报
回复
标记一下
lixiaoxue85 2007-01-08
  • 打赏
  • 举报
回复
JProgressBar并不是同步的~抛的社么异常~LZ贴出来~
随便贴个例子,LZ看看有没有用~~不太明白LZ想要社么效果
/**
@version 1.03 2004-08-22
@author Cay Horstmann
*/

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

/**
This program demonstrates the use of a progress bar
to monitor the progress of a thread.
*/
public class ProgressBarTest
{
public static void main(String[] args)
{
JFrame frame = new ProgressBarFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame that contains a button to launch a simulated activity,
a progress bar, and a text area for the activity output.
*/
class ProgressBarFrame extends JFrame
{
public ProgressBarFrame()
{
setTitle("ProgressBarTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// this text area holds the activity output
textArea = new JTextArea();

// set up panel with button and progress bar

JPanel panel = new JPanel();
startButton = new JButton("Start");
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
panel.add(startButton);
panel.add(progressBar);

checkBox = new JCheckBox("indeterminate");
checkBox.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
progressBar.setIndeterminate(checkBox.isSelected());
}
});
panel.add(checkBox);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);

// set up the button action

startButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
progressBar.setMaximum(1000);
activity = new SimulatedActivity(1000);
new Thread(activity).start();
activityMonitor.start();
startButton.setEnabled(false);
}
});


// set up the timer action

activityMonitor = new Timer(500, new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int current = activity.getCurrent();

// show progress
textArea.append(current + "\n");
progressBar.setStringPainted(!progressBar.isIndeterminate());
progressBar.setValue(current);

// check if task is completed
if (current == activity.getTarget())
{
activityMonitor.stop();
startButton.setEnabled(true);
}
}
});
}

private Timer activityMonitor;
private JButton startButton;
private JProgressBar progressBar;
private JCheckBox checkBox;
private JTextArea textArea;
private SimulatedActivity activity;

public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 200;
}

/**
A similated activity runnable.
*/
class SimulatedActivity implements Runnable
{
/**
Constructs the simulated activity thread object. The
thread increments a counter from 0 to a given target.
@param t the target value of the counter.
*/
public SimulatedActivity(int t)
{
current = 0;
target = t;
}

public int getTarget()
{
return target;
}

public int getCurrent()
{
return current;
}

public void run()
{
try
{
while (current < target)
{
Thread.sleep(100);
current++;
}
}
catch(InterruptedException e)
{
}
}

private volatile int current;
private int target;
}







BtInside 2007-01-08
  • 打赏
  • 举报
回复
没有那本书...楼上的有这方面的例子程序吗?
syhan 2007-01-08
  • 打赏
  • 举报
回复
Swing里面只有个别组件是线程安全的,具体在Corejava里面有讲到的
BtInside 2007-01-08
  • 打赏
  • 举报
回复
:) 自己顶一下.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧