gpt4 book ai didi

java - 在 Applet 中动态地将字符串从非主线程更新为主线程

转载 作者:行者123 更新时间:2023-11-29 08:52:51 25 4
gpt4 key购买 nike

我有一个如下所示的 Applet 代码。我在此 Applet 中有一个名为 String randomNumberStr 的字符串,用于显示从套接字服务器检索到的随机数。显示小程序窗口后,我有一个名为“连接”的按钮。单击此按钮将连接到 Socket 程序并获得一个随机数字符串。

我的问题是,我在这个 Applet 的单独线程 SocketConnectionThread 中运行套接字连接代码,但是,String randomNumberStrrepaint 函数在主线程中。

我如何从这个线程 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;
}

}

最佳答案

要在 java 中比较对象,请使用 .equals() 方法而不是“==”运算符

替换下面的代码

 if ( connectBtn.getLabel()=="Connect" )

  if ( "Connect".equals.(connectBtn.getLabel()))

关于java - 在 Applet 中动态地将字符串从非主线程更新为主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21829639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com