java:如何制作带有单选按钮和标签的可滚动面板?
我有一个 JScrollPane,我想在其中放置单选按钮和标签的列表。我的问题是面板不滚动,我想这是因为我没有设置视口,但是当我有很多组件时如何设置它? 我的代码看起来像这样:
JScrollPane panel = new JScrollPane();
JRadioButton myRadio;
JLabel myLabel;
for(int i = 0; i<100; i++){
myRadio = new JRadioButton();
myLabel = new JLabel("text");
panel.add(myRadio);
panel.add(myLabel);
}
谢谢。
I got a JScrollPane in which I want to place a list of radio buttons and labels. My problem is the panel doesn't scroll, I suppose it's because i didn't set a viewport, but how can I set it when I have to many components?
My code looks something like this:
JScrollPane panel = new JScrollPane();
JRadioButton myRadio;
JLabel myLabel;
for(int i = 0; i<100; i++){
myRadio = new JRadioButton();
myLabel = new JLabel("text");
panel.add(myRadio);
panel.add(myLabel);
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好将按钮和标签放入包装器
JPanel
中,然后将其放入JScrollPane
中。试试这个:
一定要查看 ButtonGroup 也是如此。 ButtonGroups 允许您强制执行单选按钮常见的单选约束。
It is better to put your buttons and labels in a wrapper
JPanel
and then drop that into aJScrollPane
.try this:
be sure to look into ButtonGroup as well. ButtonGroups allow you to enforce the single selection constraint common to radio buttons.