java:如何制作带有单选按钮和标签的可滚动面板?

发布于 2024-09-02 01:56:30 字数 360 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-09-09 01:56:30

最好将按钮和标签放入包装器 JPanel 中,然后将其放入 JScrollPane 中。

试试这个:

    JPanel panel = new JPanel(new GridLayout(0,1));
    JRadioButton myRadio;
    for(int i = 0; i<100; i++){
        myRadio = new JRadioButton("text" + i);
        panel.add(myRadio);
     }
    JScrollPane scrollPane = new JScrollPane(panel);

一定要查看 ButtonGroup 也是如此。 ButtonGroups 允许您强制执行单选按钮常见的单选约束。

It is better to put your buttons and labels in a wrapper JPanel and then drop that into a JScrollPane.

try this:

    JPanel panel = new JPanel(new GridLayout(0,1));
    JRadioButton myRadio;
    for(int i = 0; i<100; i++){
        myRadio = new JRadioButton("text" + i);
        panel.add(myRadio);
     }
    JScrollPane scrollPane = new JScrollPane(panel);

be sure to look into ButtonGroup as well. ButtonGroups allow you to enforce the single selection constraint common to radio buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文