Java 线程内的方法不会被调用
以下代码中未调用serverinitiator类中的方法serverinititor().initialize(5555) 此方法将在端口5555上为客户端启动一个新帧,用于发送消息或聊天等功能Java 线程内的方法不会被调用,java,swing,actionlistener,serversocket,event-dispatch-thread,Java,Swing,Actionlistener,Serversocket,Event Dispatch Thread,以下代码中未调用serverinitiator类中的方法serverinititor().initialize(5555) 此方法将在端口5555上为客户端启动一个新帧,用于发送消息或聊天等功能 public static void main(String[] args) { new Thread(new Runnable() { public void run() { frame = new JFrame("client list");
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
frame = new JFrame("client list");
panel = new JPanel();
JButton button = new JButton("Refresh Client list");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(success){
JButton jb = new JButton("A new Client on port 5555");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ServerInitiator().initialize(5555);
}
});
panel.add(jb);
frame.revalidate();
}
}).start();
}
ServerInitiator.java
synchronized public void initialize(int port){
try {
final ServerSocket sc = new ServerSocket(port);
System.out.println(SwingUtilities.isEventDispatchThread());
drawGUI();
new Thread(new Runnable(){
public void run()
{
try {
Socket client = sc.accept();
System.out.println("New client Connected to the server");
new ClientHandler(client,desktop);
} catch (IOException ex) {
}
}
}).start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
请回复
谢谢。我尝试应用最小的更改,使其可编译。结果就是这个班。出于某种神秘的原因,我把这个类叫做
ItDoesGetCalled
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ItDoesGetCalled
{
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
final JFrame frame = new JFrame("client list");
final JPanel panel = new JPanel();
JButton button = new JButton("Refresh Client list");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
boolean success = true;
if(success){
JButton jb = new JButton("A new Client on port 5555");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ServerInitiator().initialize(5555);
}
});
panel.add(jb);
frame.pack();
}
}
});
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}).start();
}
}
class ServerInitiator
{
synchronized public void initialize(int port)
{
System.out.println("Here we go...");
}
}
但是,我强烈建议你清理一下。所有线程的用途是什么?目前,这样的事情应该足够了:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ClientServerUI
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame("Client list");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel clientListPanel = new ClientListPanel();
f.getContentPane().add(clientListPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class ClientListPanel extends JPanel
{
ClientListPanel()
{
setLayout(new GridLayout(0,1));
JButton refreshButton = new JButton("Refresh client list");
refreshButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
boolean success = true;
if(success)
{
createNewClientButton();
}
}
});
add(refreshButton);
}
private void createNewClientButton()
{
JButton newClientButton = new JButton("A new Client on port 5555");
newClientButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
new ServerInitiator().initialize(5555);
}
});
add(newClientButton);
SwingUtilities.getWindowAncestor(this).pack();
}
}
class ServerInitiator
{
synchronized public void initialize(int port)
{
System.out.println("Here we go...");
}
}
您不应在
事件调度线程
(EDT)之外分配UI。另一方面,保持网络远离EDT。请注意,Swing具有单线程绘制模型。UI组件分配及其交互必须在EDT上完成。请查看以了解更多详细信息
为了获得最佳性能,EDT上的所有任务都应简短。应在工作线程上处理网络。有几个选项:
SwingWorker
、ExecutorService
或您自己的辅助线程SwingWorker
有一个内置机制,可以在EDT上推送更新。如果是ExecutorService
,您可以使用SwingUtilities.invokeLater
实现此目的,与您自己的工作线程相同 serverinititor()在哪里。initialize(5555)
。你有没有把它编译好?