저는이 문제를 며칠 동안 처리해 왔으며 어디에서 잘못되었는지 이해할 수 없습니다. 서버 - 클라이언트 채팅 프로그램을 만들었고 서버 GUI에는 사용자 목록을 보여주는 탭이 있습니다. 이 목록은 내가 원하는 모든 방식으로 작동합니다. UserList
을 클라이언트 GUI에 추가하고 JList
이 있지만, DefaultListModel
을 업데이트하면 JList
은 ServerGUI
에서만 업데이트됩니다. 디버그를 시도하여 ChatGUI의 JList
이 표시 가능하지 않으며 이유 또는 수정 방법을 알 수 없습니다. (클라이언트 조인 할 때 등록 방법은 프로그램에서 나중에라고합니다)JList가 표시되지 않습니다.
public class ServerGUI {
public volatile static ArrayList<Client> users;
public static DefaultListModel<String> model = new DefaultListModel<String>();
static Client clientReg;
public static void register(Client client) {
clientReg = client;
users.add(clientReg);
model.addElement(clientReg.username);
ServerView.userList.setModel(model);
ChatView.userList.setModel(model);
}
}
-
클라이언트 클래스
public class Client {
String username;
Socket socket;
PrintWriter out;
Scanner in;
public Client (String username, Socket socket, PrintWriter out, Scanner in) {
this.username = username;
this.socket = socket;
this.out = out;
this.in = in;
}
}
ServerGUI 클래스 : 여기
내 (관련) 코드 채팅보기 클래스public class ChatView extends JFrame {
public JPanel contentPane;
public static JList<String> userList = new JList<String>();
public static JTextArea chatOutput;
private JTextField inputField;
public ChatView() {
setResizable(false);
setTitle("Chat GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 550, 475);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
tabs.setBounds(0, 0, 535, 435);
contentPane.add(tabs);
JPanel chatViewer = new JPanel();
tabs.addTab("Chat", null, chatViewer, null);
chatViewer.setLayout(null);
// Code that makes up the chatViewer JPanel
JPanel userListPane = new JPanel();
tabs.addTab("User List", null, userListPane, null);
userListPane.setLayout(null);
JLabel label = new JLabel("User List:");
label.setBounds(10, 10, 510, 20);
userListPane.add(label);
userList.setModel(new AbstractListModel<String>() {
public String getElementAt(int index) {
return ServerGUI.model.get(index);
}
public int getSize() {
return ServerGUI.model.size();
}
});
userList.setValueIsAdjusting(true);
userList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
userList.setBounds(10, 40, 510, 395);
userListPane.add(userList);
}
}
대부분의 프로그래밍은 스스로 가르쳤으므로 잘못된 형식이 있으면 알려 주시면 수정 해 드릴 수 있습니다.
null 레이아웃을 사용하지 마십시오. 이 사이트에서 Swing Q/A를 읽은 적이 있다면, 이것은 매우 좋은 이유 때문에 권장되지 않는다는 것을 이미 알고있을 것입니다. –
userListPane이 다른 탭에 추가되었습니다. 내가 실수로 선택한 코드에이를 추가하는 것을 잊었습니다 – bh34e5
그래서 JList가 작동합니까? 탭에서 볼 수 있습니까? 하지만 다시 말하지만 null 레이아웃을 사용하지 마십시오. 대신 JList의 가시 행 수를 설정하고 프로토 타입 요소를 제공하여 JLists가 항상 JScrollPane 내에 표시되어야하므로 JScrollPane 내에서 자체 표시된 크기를 설정합니다. 어디에서나 setBounds를 제거하고 레이아웃 관리자를 사용하는 방법을 배웁니다. –