【问题标题】:JRadioButtons in a custom JScrollPane not appearing?自定义 JScrollPane 中的 JRadioButtons 没有出现?
【发布时间】:2013-05-02 05:36:03
【问题描述】:

我正在为一个项目编写一个 IM 客户端 GUI。在窗口的左侧,我想要一个滚动窗格,其中包含每个活动用户的单选按钮,以便在按下新聊天按钮时,将与所选用户创建聊天。

我已经实现了一个包含 3 个给定用户的示例 GUI。我为每个创建一个 JRadioButton,设置一个 ActionCommand 和一个 ActionListener,将其添加到 ButtonGroup,然后将其添加到“this”,这是一个扩展 JScrollPane 的类。但是,当我运行代码时,我在左侧只看到一个空框架,没有按钮。谁能解释一下?相关代码如下。

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");
    }
}

【问题讨论】:

  • 我将使用带有 Icon 的 JList 而不是 JRadioButton,然后 ListSelectionListner 可以对 JList 中的选择事件做出反应
  • 什么是ConversationsPane?以SSCCE 的形式回答。

标签: java swing jscrollpane jradiobutton buttongroup


【解决方案1】:

这不是滚动窗格的工作方式。您不会向它们“添加”组件。您将 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

相反...

查看How to use scroll panes了解更多详情...

【讨论】:

  • 这是有道理的!在发布之前,我已经看到该链接在谷歌上搜索了我的问题,但无法从中理解太多,但是您将其写出来的方式使其更加清晰。我会尝试以这种方式实现它!
  • 有效!我最终使 ActiveList 扩展了 JPanel 并成为您示例中的“视图”,然后创建了一个 JScrollPane 并在 ChatGUI 的构造函数中设置了视口。非常感谢!
【解决方案2】:

而不是扩展 JScrollPane 扩展 JPanel 与适当的布局。将您的JRadioButtons 添加到面板并将面板放在JScrollPane 中。

【讨论】:

    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    相关资源
    最近更新 更多