0

プロジェクトの 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");
    }
}
4

2 に答える 2

2

これは、スクロール ペインの仕組みではありません。それらにコンポーネントを「追加」しません。Aコンポーネントをスクロール ペインのビュー ポートに設定します

ここに画像の説明を入力

拡張する代わりにJScrollPane、それに値を追加していないので、もっと似たようなことをしてみてください...

JScrollPane scrollPane = new JScrollPane();
JPanel view = new JPanel(new GridLayout(0, 1));
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);
    view.add(button);
}

scrollPane.setViewportView(view);
// Add scrollpane to WEST position of main view

その代わり...

詳細については、スクロール ペインの使用方法を参照してください...

于 2013-05-02T05:39:15.813 に答える
1

拡張する代わりに、適切なレイアウトでJScrollPane拡張します。JPanelあなたをパネルに追加JRadioButtonsし、パネルを に配置しJScrollPaneます。

于 2013-05-02T05:40:40.567 に答える