自定义JScrollPane中的JRadioButtons没有出现?

时间:2013-05-02 05:36:04

标签: java swing jscrollpane jradiobutton buttongroup

我正在为项目编写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");
    }
}

2 个答案:

答案 0 :(得分:2)

这不是滚动窗格的工作方式。您没有“添加”组件。您将 A 组件设置为滚动窗格视图端口

enter image description here

不要延长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了解更多详情......

答案 1 :(得分:1)

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