- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我举了一个我想要的程序的例子。问题是我无法使我的套接字连接正常工作(按照我希望的方式工作)。而且我不知道问题出在哪里。
public class TestChat extends Frame {
public static Panel1 p1;
public static Panel2 p2;
public static TestChat tc;
public TestChat() {
super();
setPreferredSize(new Dimension(800, 600));
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
p1 = new Panel1();
p2 = new Panel2();
add(p1);
}
public static void main(String[] args) {
// TODO code application logic here
tc = new TestChat();
tc.pack();
tc.setVisible(true);
///*
try {
TestChat.p2.run();
} catch (IOException ioe) {
System.out.println("IO here");
}
//*/
}
public void change(int to) {
if (to == 1) {
tc.remove(p2);
tc.add(p1);
}
if (to == 2) {
tc.remove(p1);
tc.add(p2);
}
tc.pack();
}
}
public class Panel1 extends Panel implements ActionListener{
public Button button = new Button("Launch chat");
public Panel1() {
super();
setLayout(new BorderLayout());
Label label = new Label("Launcher panel here");
add(label);
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
TestChat.tc.change(2);
/*
try {
TestChat.p2.run();
} catch (IOException ioe) {
System.out.println("IO here");
}
//*/
}
}
}
public class Panel2 extends Panel implements ActionListener {
private static final int LOGIN_MAX = 300;
public static TextArea ta = new TextArea();
public static TextField tf = new TextField();
public static TextArea logins = new TextArea();
public static PrintWriter out = null;
public static String[] loginList = new String[LOGIN_MAX];
public static int loginCount = 0;
public Panel temp = new Panel();
public Button startButton = new Button("Start!");
///*
public String fromServer;
public BufferedReader in = null;
public BufferedReader stdIn;
public Socket kkSocket = null;
//*/
public Panel2() {
setLayout(new BorderLayout());
temp.setLayout(new BorderLayout());
ta.setEditable(false);
tf.addActionListener(this);
startButton.addActionListener(this);
logins.setEditable(false);
temp.add(ta, BorderLayout.CENTER);
temp.add(tf, BorderLayout.SOUTH);
add(temp);
add(logins, BorderLayout.EAST);
add(startButton, BorderLayout.SOUTH);
}
//private static void makeLogins() {
public static void makeLogins() {
String userArea = loginList[0] + "\n";
for (int i = 1; i < loginCount; i++) {
userArea = userArea + loginList[i] + "\n";
}
logins.setText(userArea);
}
public void run() throws IOException {
kkSocket = null;
BufferedReader in = null;
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
//System.err.println("Can't host to server.");
System.out.println("Can't host to server.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to server.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
while ((fromServer = in.readLine()) != null) {
validate();
if (fromServer.startsWith("cmd_newUser_")) {
loginList[loginCount++] = fromServer.substring(12);
if (loginCount > 1) {
Arrays.sort(loginList, 1, loginCount - 1);
}
makeLogins();
} else if (fromServer.startsWith("cmd_deleteUser_")) {
String tmp = fromServer.substring(15);
for (int i = 0; i < loginCount; i++) {
if (loginList[i].equals(tmp)) {
loginList[i] = "" + ((char) 255);
break;
}
}
Arrays.sort(loginList, 1, loginCount);
loginCount--;
makeLogins();
} else {
ta.append(fromServer + "\n");
}
if (fromServer.equals("Bye.")) {
break;
}
}
out.close();
in.close();
stdIn.close();
}
private void sendStr(PrintWriter out) {
if (tf.getText() != "") {
out.println(tf.getText());
tf.setText("");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == tf) {
sendStr(out);
} else if (e.getSource() == startButton) {
System.out.println("I make some actions in the original proj");
}
}
}
当我以这种方式使用它时,我的程序将连接并且一切正常。但我想在按下按钮时从 Panel1 类开始连接(注释调用)。当我尝试从 Panel1 调用它时,我的整个程序卡住了。问题出在哪里以及如何解决?
附注这是我的服务器代码(以防万一)
public class KKMultiServer extends Frame {
public static int userCount = 0;
public static Label users;
public static KKMultiServerThread[] userList=new KKMultiServerThread[100];
public static int writer=0;
public static int curNum=1;
public KKMultiServer() {
super("Server");
setLayout(new GridLayout(2, 1));
users = new Label("Users online: " + userCount);
add(users);
setLocation(200, 200);
setResizable(false);
setMinimumSize(new Dimension(300, 200));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
KKMultiServer server = new KKMultiServer();
server.pack();
server.setVisible(true);
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening) {
userList[writer]=new KKMultiServerThread(serverSocket.accept());
System.out.println("Client added!");
userCount++;
users.setText("Users online: " + userCount);
userList[writer++].start();
}
serverSocket.close();
}
}
最佳答案
静态
引用是一个非常糟糕的设计。您面临着不确切知道您引用的内容的风险。到目前为止,你基本上已经侥幸成功了。当 main
由它所在的 JVM 运行时,通常称为“主”线程。当您开始使用任何 Swing 组件时,Swing API 将启动“事件调度线程”...
所以发生了什么,您正在启动应用程序的 UI 部分,它的上下文正在移至 EDT,而 run
方法 p2
正在继续运行在主
线程中。
现在您想要从按钮启动通信,您已将执行上下文移至 EDT,这使得您的应用程序看起来像是已挂起......
现在,你有两个问题。第一个是,您需要从 EDT 中获取通信,第二个是,您永远不应该从 EDT 之外的任何线程修改或更新 UI...
有许多解决方案可供您使用,SwingWorker
可能是解决您问题的最简单的解决方案。
看看Concurrency in Swing了解更多详情
关于Java Socket 卡住问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19043931/
我正在我的 xamarin.forms 应用程序中实现扫描仪功能,为此我正在使用 iOS native AVCaptureSession。但我的问题是在扫描或捕获 session 处于事件状态并且设备
所以我目前正在为我的项目制作一个音乐应用程序,它允许用户创建自己的音乐播放列表。但是,当我单击显示媒体选择器按钮时,它只显示白屏,当包含媒体选择器的 View 是 Initial View Contr
当我尝试在模拟器中启动 AVD 时,会出现一个小窗口(见图片),5 秒后它说没有响应并一直保持这种状态直到我关闭它。 我在网上搜索并尝试了所有解决方案,但都没有成功 在 BIOS 中启用了虚拟化 已安
尝试使用以下命令从视频中提取特定帧(删除了文件的特定名称!: ffmpeg -i video.mp4 -vf "select-gte(n\,6956)"-vframes 10262 文件夹/帧%d.j
我怎么知道终端正在继续工作而不中断它? 我已经运行了以下 git 命令: clone git://ligo-vcs.phys.uwm.edu/lalsuite.gituote 一段时间后它似乎被卡住了
我对 WPF 中的数据网格有一个奇怪的问题。我正在为我的应用程序使用 MVVM 模式,并且我的 View 模型实现了 idataerrorinfo 接口(interface)。每当我在添加新行后在我的
我有这个 Excel 文件,当我输入数据时它卡住了。例如,我双击一个单元格,输入数据,然后按“输入”。它会卡住而不是进入下面的细胞。按几次“enter”不会解冻程序,唯一有效的是用鼠标选择另一个单元格
我有线程池的任务队列,每个任务都有卡住锁定其正在使用的所有资源的倾向。并且除非重新启动服务,否则这些无法释放。 ThreadPool 中有没有办法知道它的线程已经被卡住?我有一个使用超时的想法(虽然我
我制作了以下小程序来确定内存是否用于 freeze(X,Goal) 之类的目标回收时 X变得无法访问: %:- use_module(library(freeze)). % Ciao Prolog n
我有一个使用 swing 的简单 java 应用程序。然而,当我执行程序时,框架将会出现,但我无法单击任何地方,并且按钮仅在几秒钟后出现。我对 javas Swing 库非常陌生,所以我可能会丢失一些
我正在尝试创建一个简单的 TCP 客户端服务器应用程序接口(interface)用户可以在按下相应按钮时启动或停止服务器我创建了一个 StartServer 按钮,当用户按下按钮时它应该连接到服务
我正在尝试从 ftp 服务器下载文件,但在检索文件时卡住了。我正在使用 commons-net-3.6.jar 我注意到的事情 当我使用 ftpClient.enterRemotePassiveMod
我正在尝试编写一个函数,该函数将能够找到位于我系统上的可执行文件搜索路径中的任意可执行文件。我遇到了一些输入会导致 SearchPathW 的问题无限期地卡住,我不确定到底发生了什么。 std::op
我的 Nativescript 应用程序的许多页面中都有 RadSideDrawer。主应用程序组件有一个 page-router-outlet并且所有其他页面都通过导航加载到此组件中。带抽屉的页面包
我有一个最小的服务器,它等待客户端连接,然后他启动一个线程,将回复发送回客户端,问题是回复。 这是服务器的代码: int port = 1234; ServerSocket servSock =
我有一个使用 C# 的 WinForms 应用程序。我尝试从文件中读取一些数据并将其插入到数据表中。虽然此操作很忙,但我的表单卡住并且我无法移动它。有谁知道我该如何解决这个问题? 最佳答案 这可能是因
在我们学校最新的项目中,我遇到了一些问题。我想观察新条目的路径,该路径是由文件导向器按钮选择的,但如果我选择任何文件,整个窗口都会卡住...我猜它被卡住,因为调用了“observePath”方法,但我
当我输入一百万作为输入数字时,我的程序卡住了。我该如何解决这个问题? 我尝试将第二个 for 循环分离为第二个函数,但没有成功。 import java.io.*; public class Arra
早上好编译我的应用程序时,我在 Android Studio 上遇到问题。我在构建时没有收到关于 app:transformClassesWithDexBuilderForDebug 的任何输出错误,
我正在使用以下触发器 DELIMITER ; CREATE TRIGGER updateCount AFTER INSERT ON user_info FOR EACH ROW BEGIN UPDA
我是一名优秀的程序员,十分优秀!