gpt4 book ai didi

java - 在客户端和服务器之间的连接终止之前,Jlabel 组件不再可编辑

转载 作者:行者123 更新时间:2023-12-02 05:55:53 24 4
gpt4 key购买 nike

我有两个简单的应用程序可以远程关闭、重新启动和注销客户端计算机,我面临的是,当客户端连接到服务器时,Jlabel 组件(statuslabel)不再可编辑,直到连接终止(通过关闭服务器应用程序)我尝试了很多方法,例如使用 socket.isConnected 方法..但没有结果。这是客户端代码:

package homework_5.client;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import javax.swing.JOptionPane;

/**
*
* @author حسين
*/
public class ClientSideGUI extends javax.swing.JFrame {
Socket socket=null;
String status;
String recievedCommand;
BufferedReader in=null;
Process child;
/**
* Creates new form ClientSide
*/
public ClientSideGUI() {
initComponents();

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

statuslabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client.");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});

statuslabel.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
statuslabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(statuslabel, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(55, Short.MAX_VALUE)
.addComponent(statuslabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53))
);

pack();
}// </editor-fold>

private void formWindowOpened(java.awt.event.WindowEvent evt) {

try{
socket=new Socket("127.0.0.1", 1234);
status="Online";
}catch(Exception e){status="Offline";}


//Display status @حسين

if(status.equals("Online"))
{
statuslabel.setForeground(Color.GREEN);
statuslabel.setText(status);
}
if(status.equals("Offline"))
{
statuslabel.setForeground(Color.RED);
statuslabel.setText(status);
}


//Getting the work done :) @حسين
{
while(status.equals("Online"))
{
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
recievedCommand=in.readLine();
}catch(IOException e2){JOptionPane.showMessageDialog(rootPane,"Connected to server,\n but facing problems with recieving command\n Exception:"+e2.getClass().getName());}




if(recievedCommand.equals("shutdown -s"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e3){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e3.getClass().getName());}
}

if(recievedCommand.equals("shutdown -r"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e4){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e4.getClass().getName());}
}

if(recievedCommand.equals("shutdown -l"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e5){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e5.getClass().getName());}
}



}

}



}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientSideGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JLabel statuslabel;
// End of variables declaration
}

最佳答案

您的套接字连接正在阻塞事件调度线程(EDT),因此 GUI 无法重新绘制自身。您需要创建一个单独的线程来进行Socket连接。

阅读 Swing 教程中关于 Concurrency 的部分了解更多信息以及使用 Swing Worker 的解决方案。

关于java - 在客户端和服务器之间的连接终止之前,Jlabel 组件不再可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23089850/

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