Java 自定义JScrollPane中的JRadioButtons不显示?
我正在为一个项目编写一个IM客户端GUI。在窗口的左侧,我想要一个滚动窗格,其中包含每个活动用户的单选按钮,这样当按下新的聊天按钮时,将与所选用户创建聊天 我已经用3个给定的用户实现了一个示例GUI。我为每个按钮创建一个JRadioButton,设置一个ActionCommand和一个ActionListener,将其添加到ButtonGroup,然后将其添加到“this”,这是一个扩展JScrollPane的类。但是,当我运行代码时,我在左侧只看到一个空框,没有按钮。有人能解释一下吗?相关代码如下Java 自定义JScrollPane中的JRadioButtons不显示?,java,swing,jscrollpane,jradiobutton,buttongroup,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");
}
}
这不是滚动窗格的工作方式。您不会向其“添加”组件。将组件设置为滚动窗格视图端口
不要扩展
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
相反
查看更多详细信息…这不是滚动窗格的工作方式。您不会向其“添加”组件。将组件设置为滚动窗格视图端口
不要扩展
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
相反
查看更多详细信息…而不是使用适当的布局扩展
JScrollPane
extendJPanel
。将您的JRadioButtons
添加到面板中,并将面板放置在JScrollPane
中,而不是使用适当的布局扩展JScrollPane
扩展JPanel
。将您JRadioButtons
添加到面板中,并将面板放置在JScrollPane
中,我将使用带有图标的JList代替JRadioButton,然后ListSelectionListner可以对JList中的选择事件做出反应什么是对话span
?回答形式为。我将使用带有图标的JList而不是JRadioButton,然后ListSelectionListner可以对JList中的选择事件做出反应什么是对话span
?以A的形式回答。这很有意义!在发布之前,我曾看到过那个链接在谷歌上搜索我的问题,但我无法从中了解太多,但你写出来的方式让我更清楚。我将尝试以这种方式实现它!它起作用了!最后,我让ActiveList扩展JPanel并成为示例中的“视图”,然后创建了一个JScrollPane并在ChatGUI的构造函数中设置了视口。非常感谢你!这是有道理的!在发布之前,我曾看到过那个链接在谷歌上搜索我的问题,但我无法从中了解太多,但你写出来的方式让我更清楚。我将尝试以这种方式实现它!它起作用了!最后,我让ActiveList扩展JPanel并成为示例中的“视图”,然后创建了一个JScrollPane并在ChatGUI的构造函数中设置了视口。非常感谢你!