【发布时间】:2023-03-29 11:10:01
【问题描述】:
我正在制作一个客户端服务器应用程序,其中服务器代码由 JFrame 执行 这是我的服务器代码,当我从 MyServer 类的 main 方法调用 Show 方法时它正在工作,但是当我从 Jframe 的 Key Event 调用它时,它没有显示另一个 Jframe。请帮忙。
public class MyServer
public void Show() throws IOException {
ServerSocket ss = new ServerSocket(6666);
new IPScanning().dispose();
int count = 0;
while (true) {
Socket s = null;
try {
s = ss.accept();
SocketThread socketThread = new SocketThread(s, count);
socketThread.start();
} catch (Exception e) {
ss.close();
s.close();
System.out.println(e);
} finally {
count++;
}
}
}
class SocketThread extends Thread {
public void run() {
try {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ShowTable.INSTANCE.showdata(count, host, ip, username, os_name, os_arch, pro_detail, Mac_add, disk_size, max_memory);
}
});
} catch (Exception e) {
System.out.println("Server Problem");
System.out.println(e);
}
}
}
}`
这是我的关键事件
private void jStartActionPerformed(java.awt.event.ActionEvent evt) {
MyServer ms=new MyServer();
try {
ms.Show();
} catch (IOException ex) {
System.out.println(ex);
}
}
另一个通过 SocketThread 类调用的 JFrame 的代码。
public enum ShowTable{
INSTANCE;
private JFrame f = new JFrame();
private JTable jt = new JTable(new DefaultTableModel());
private DefaultTableModel model = (DefaultTableModel) jt.getModel();
private ShowTable() {
jt.setBounds(30, 40, 200, 300);
jt.setFocusable(false);
jt.setRowSelectionAllowed(false);
JScrollPane sp = new JScrollPane(jt);
f.add(sp);
f.setSize(1300, 600);
}
public void showdata(int count,String host,String ip,String username,String os_name,String os_arch,String pro_detail,String Mac_add,float disk_size,float max_memory){
f.setVisible(true);
}
}
【问题讨论】:
-
1) 请参阅The Use of Multiple JFrames, Good/Bad Practice? 2) 如需更快的帮助,请尽快发布minimal reproducible example 或Short, Self Contained, Correct Example。 3) 请学习常见的 Java 命名法(命名约定 - 例如
EachWordUpperCaseClass、firstWordLowerCaseMethod()、firstWordLowerCaseAttribute,除非它是UPPER_CASE_CONSTANT)并始终如一地使用它。
标签: java swing sockets jframe jtable