我有一個Applet代碼,如下所示。我在這個小程序中有一個名爲String randomNumberStr
的字符串,用於顯示從套接字服務器檢索到的隨機數。 顯示Applet窗口後,我有一個名爲「Connect」的按鈕。點擊這個按鈕將連接到Socket程序並獲得一個隨機數字符串。從非主線程更新字符串到主動態在Applet中
我的問題是,我在這個Applet中運行套接字連接代碼在一個單獨的線程SocketConnectionThread
,但是,String randomNumberStr
和repaint
函數在主線程中。
如何訪問並將此線程的隨機數值SocketConnectionThread
傳遞給main並重新繪製Applet窗口?
public class CobrowseApplet extends Applet implements ActionListener
{
private static final long serialVersionUID = 1L;
String titleStr ;
String randomNumberStr;
Image sharedImage;
BufferedImage image;
private Button connectBtn;
Socket localSocket;
PrintWriter out;
BufferedReader in;
static Timer timer;
int delay = 1000;
int period = 1000;
DataInputStream inStream;
PrintStream outStream;
InputStream input;
public void init(){
titleStr = "Welcome";
randomNumberStr = "";
connectBtn = new Button("Connect");
connectBtn.addActionListener(this);
add(connectBtn);
connectBtn.setBounds(200, 50, 90, 20);
connectBtn.setEnabled(true);
setLayout(null);
setSize(550, 650);
}
public void paint (final Graphics g)
{
//super.paint(g);
int x = getSize().width;
int c1 = x/2;
g.drawString(titleStr, c1-100, 20);
g.drawString(randomNumberStr, c1-100, 80);
System.out.println("sharedImage" + sharedImage);
//g.drawImage(sharedImage, 100, 100, this);
System.out.println("drawImage");
}
@Override
public void actionPerformed(ActionEvent e)
{
Thread thr = new Thread(new SocketConnectionThread(randomNumberStr));
thr.start();
if (connectBtn.getLabel()=="Connect")
{
connectBtn.setLabel("");
connectBtn.setLabel("Disconnect");
}
else
{
connectBtn.setLabel("");
connectBtn.setLabel("Connect");
}
System.out.println("randomNumberStr: " + randomNumberStr);
repaint();
}
}
class SocketConnectionThread implements Runnable {
String randomStr;
public SocketConnectionThread(String randomNumberStr) {
this.randomStr = randomNumberStr;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("Before Applet socket connection");
Socket localSocket = new Socket(getLocalIP(), 8080);
BufferedReader socketReader = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
String msgStr = socketReader.readLine();
System.out.println("Server Message on Client: " + msgStr);
// IT GETS THE NEW STRING HERE FROM SOCKET. HOW CAN I PUSH TO MAIN?
randomStr = msgStr;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String getLocalIP() {
InetAddress inetAddress = null;
String ipAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
ipAddress = inetAddress.getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ipAddress : " + ipAddress);
return ipAddress;
}
}
您可以用[觀察者模式]嘗試(http://en.wikipedia.org/wiki/Observer_pattern)。 –
1)爲什麼要編寫一個applet?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)爲什麼選擇AWT而不是Swing?看到我對[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 –