将JScrollPane添加到JDialog [英] Adding a JScrollPane to JDialog

查看:70
本文介绍了将JScrollPane添加到JDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在工作的JDialog,现在我想向其中添加滚动条.该文档对我来说有些混乱.是否将对话框添加到JScrollPane,反之亦然?

I have a working JDialog and now I want to add scroll bars to it. The documentation is a bit confusing to me. Do I add the dialog to the JScrollPane or vice-versa?

似乎所有示例在对话框中都有一个JPanel,并且面板是可滚动的.我有一个动态增长的对话框,所以我希望对话框本身是可滚动的.有人可以指出我正确的方向吗?

It seems like all the examples have a JPanel in the dialog and the panel is scrollable. I have a dialog that grows dynamically so I want the dialog itself to be scrollable. Can someone point me in the right direction?

回复安德鲁·汤普森

感谢您的答复.

这时,让大小由布局管理器确定.我还不确定要放多大,所以我没有设置任何尺寸.它随着我添加行而增长.这将是此开发阶段的一部分.宽度不会改变,只会改变高度.我使用"invokelater"显示对话框.这是相关代码:

I let the size be determined by the layout manager at this point. I'm not exactly sure yet how big to let it get yet so I have not set any sizes. It just grows as I add rows. That will be part of this development phase. The width will not change, just the height. I display the dialog using 'invokelater'. This is the relevant code:

timeLineDialog = new JDialog();
timeLineDialog.setLayout(layout);
timeLineDialog.setModalityType(ModalityType.MODELESS);
timeLineDialog.setTitle("Time Line Settings");
timeLineDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
.
.
.
timeLineDialog.pack();
timeLineDialog.setLocationRelativeTo(GUI.getInstance().getFrame());
timeLineDialog.setVisible(true);

我想要对话框右侧的滚动条.

I want the scroll bars on the right side of the dialog pane.

推荐答案

我认为您必须创建一个JDialog,然后向其中添加JScrollPane及其内容.以下代码对我有用:

I think you have to create a JDialog, then add a JScrollPane to it and your content inside that. The following code worked for me:

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JLabel;

public class Dialog extends JDialog {
    private final JScrollPane scrollPane = new JScrollPane();
    //I added the breaks to the label below to be able to scroll down.
    private final JLabel lblContent = new JLabel("<html><body><p>Content<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>Content<br><br><br><br><br><br>Content</p></body></html>");

    public static void main(String[] args) {
                    Dialog dialog = new Dialog();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
    }

    public Dialog() { 
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout(0, 0));

        getContentPane().add(scrollPane, BorderLayout.CENTER);

        scrollPane.setViewportView(lblContent);

    }
}

希望这会有所帮助.

这篇关于将JScrollPane添加到JDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆