プロジェクトの IM クライアント GUI を作成しています。ウィンドウの左側に、各アクティブ ユーザー用のラジオ ボタンを備えたスクロール ペインが必要です。これにより、[新しいチャット] ボタンを押すと、選択したユーザーとのチャットが作成されます。
3 人の特定のユーザーを使用してサンプル GUI を実装しました。それぞれに JRadioButton を作成し、ActionCommand と ActionListener を設定し、それを ButtonGroup に追加してから、JScrollPane を拡張するクラスである「this」に追加します。ただし、コードを実行すると、左側に空のフレームしか表示されず、ボタンは表示されません。誰でも説明できますか?関連するコードは以下です。
package gui;
import javax.swing.*;
public class ActiveList extends JScrollPane implements ActionListener {
private ButtonGroup group;
private String selected;
public ActiveList() {
//TODO: will eventually need access to Server's list of active usernames
String[] usernames = {"User1", "User2", "User3"};
ButtonGroup group = new ButtonGroup();
this.group = group;
for (String name: usernames) {
JRadioButton button = new JRadioButton(name);
button.setActionCommand(name);
button.addActionListener(this);
this.group.add(button);
this.add(button);
}
}
public String getSelected() {
return this.selected;
}
@Override
public void actionPerformed(ActionEvent e) {
this.selected = e.getActionCommand();
System.out.println(e.getActionCommand());
}
}
私が実行しているメイン メソッドは、ChatGUI.java という別のクラスからのものです。ConversationsPane コンテナーは、私の GUI の別のクラスであり、正常に動作しています。
package gui;
import javax.swing.*;
public class ChatGUI extends JFrame {
private ConversationsPane convos;
private ActiveList users;
public ChatGUI() {
ConversationsPane convos = new ConversationsPane();
this.convos = convos;
ActiveList users = new ActiveList();
this.users = users;
GroupLayout layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(users, 100, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(convos)
);
layout.setVerticalGroup(
layout.createParallelGroup()
.addComponent(users)
.addComponent(convos)
);
}
public static void main(String[] args) {
ChatGUI ui = new ChatGUI();
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ui.setVisible(true);
ui.setSize(800,400);
ui.convos.newChat("Chat A");
}
}