设置 JDialog 的最大尺寸?

发布于 2024-11-02 22:19:45 字数 507 浏览 0 评论 0原文

简而言之:我需要做一些棘手的事情才能让 JDialog 的 setMaximumSize() 工作吗?

完整版本:我有一个 JDialog(布局管理器:BorderLayout),其中包含一个滚动窗格和底部带有提交按钮的 JPanel。

滚动窗格包含一个 JPanel,它是在系统的其他位置动态构建的。

我想要的是对话框在 JPanel 周围动态调整自身大小到一定大小,然后开始增长滚动条。这或多或少是默认情况下发生的情况,除了最大尺寸似乎是我的显示器的尺寸。

我认为这就是从 java.awt.Component 继承的 .setMaximumSize() 方法所做的,但设置它似乎没有任何效果。

设置首选大小确实会产生影响 - 但无论如何,对话框始终是该大小,这确实不是我想要的。

(如果我在滚动窗格上设置最大/首选尺寸属性,效果是相同的。)

我是否错过了一些非常明显的东西?是否有一些我不知道的古怪 JDialog / BorderLayout / MaximumSize 交互?

The short version: do I need to do something tricky to get JDialog's setMaximumSize() to work?

The full version: I've got a JDialog (layout manager: BorderLayout) which contains a scroll pane and a JPanel on the bottom with the commit buttons.

The scroll pane contains a JPanel which is built dynamically elsewhere in the system.

What I want is for the dialog to dynamically size itself around the JPanel up to a certain size, and then start growing scrollbars. This is, more or less, what happens by default, except the maximum size seems to be the size of my monitor.

I thought this is what the .setMaximumSize() method inherited from java.awt.Component did, but setting it doesn't seem to have any effect.

Setting the preferred size does has an effect - but then the dialog is always that size no matter what, which really isn't what I want.

(And the effects are the same if I set the maximum/preferred size properties on the scroll pane.)

Have I missed something tremendously obvious? Is there some wacky JDialog / BorderLayout / MaximumSize interaction I don't know about?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

深陷 2024-11-09 22:19:45

正如调整滚动窗格的大小<中所讨论的/a>,某些组件可以提供有关设置视口首选大小的有用信息。 JListsetVisibleRowCount() 方法特别方便,但甚至 getViewport().setPreferredSize(…) 也足够了。当然,sscce 会有所帮助。

附录:作为一个具体示例,下面的对话框最初大小为有 N-2 行。随着更多的添加,对话框会不断增长,直到数量达到N。此时滚动条开始“增长”。该示例使用 JList,但任何 Scrollable 组件应该具有适应性。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** @see http://stackoverflow.com/questions/5759131 */
public class ListDialog {

    private static final int N = 12;
    private JDialog dlg = new JDialog();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JScrollPane sp = new JScrollPane(list);
    private int count;

    public ListDialog() {
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                append();
                if (count <= N) {
                    list.setVisibleRowCount(count);
                    dlg.pack();
                }
            }
        }));
        for (int i = 0; i < N - 2; i++) {
            this.append();
        }
        list.setVisibleRowCount(N - 2);
        dlg.add(sp, BorderLayout.CENTER);
        dlg.add(panel, BorderLayout.SOUTH);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setVisible(true);
    }

    private void append() {
        model.addElement("String " + String.valueOf(++count));
        list.ensureIndexIsVisible(count - 1);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ListDialog pd = new ListDialog();
            }
        });
    }
}

As discussed in Sizing a Scroll Pane, some components can provide useful information about setting the viewport's preferred size. The setVisibleRowCount() method of JList is particularly convenient, but even getViewport().setPreferredSize(…) may suffice. Naturally, an sscce would help.

Addendum: As a concrete example, the dialog below is initially sized to have N-2 rows. As more are added, the dialog grows until the number reaches N. At that point the scrollbars start "growing". The example uses a JList, but any Scrollable component should be adaptable.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** @see http://stackoverflow.com/questions/5759131 */
public class ListDialog {

    private static final int N = 12;
    private JDialog dlg = new JDialog();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JScrollPane sp = new JScrollPane(list);
    private int count;

    public ListDialog() {
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                append();
                if (count <= N) {
                    list.setVisibleRowCount(count);
                    dlg.pack();
                }
            }
        }));
        for (int i = 0; i < N - 2; i++) {
            this.append();
        }
        list.setVisibleRowCount(N - 2);
        dlg.add(sp, BorderLayout.CENTER);
        dlg.add(panel, BorderLayout.SOUTH);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setVisible(true);
    }

    private void append() {
        model.addElement("String " + String.valueOf(++count));
        list.ensureIndexIsVisible(count - 1);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ListDialog pd = new ListDialog();
            }
        });
    }
}
蒲公英的约定 2024-11-09 22:19:45

使面板实现可滚动是的最佳选择。另请参阅(除了 Trashgod 已提供的教程链接)Rob 的博客条目

http: //tips4java.wordpress.com/2009/12/20/scrollable-panel/

然后:

1)以对典型内容合理的方式实现 getPreferredScrollableViewportSize() (对于 JList fi 来说,这是要显示的首选行数,又名:visibleRowCount)
2) 为那些“合理条款”实现设置器/获取器

该附加层(协调“合理条款”)允许所有协作组件尽最大努力提出可靠的大小提示,而不会像 setXXSize 那样产生不友好的干扰(这是一个否-no-never-ever,简单地忘记那些方法的存在;)

Make the panel imlement Scrollable is the way to go. See also (in addition to the tutorial link Trashgod already provided) Rob's blog entry

http://tips4java.wordpress.com/2009/12/20/scrollable-panel/

then:

1) implement getPreferredScrollableViewportSize() in terms reasonable for the typical content (for a JList f.i. that's the preferred number of rows to show, aka: visibleRowCount)
2) implement setters/getters for those "reasonable terms"

That additional layer (coordiates "reasonable terms") allows all collaborating components to do their best to come up with robust size hints without unfriendly interference as setXXSize (which is a no-no-never-ever, simply forget those methods exist ;)

恬淡成诗 2024-11-09 22:19:45

导致 JDialog 调整大小的 pack() 方法依赖于调用 getPreferredSize() 来确定要使用的大小。

如果您对 JDialog 进行子类化,则可以通过子类化此方法来强制执行最大大小。

例子:

@Override 
public Dimension getPreferredSize() {    
  int maxWidth=500;    
  int maxHeight=300;    
  Dimension dim=super.getPreferredSize();    
  if (dim.width>maxWidth) dim.width=maxWidth;    
  if (dim.height>maxHeight) dim.height=maxHeight;    
  return dim; 
}

The pack() method which causes the JDialog to be sized relies on a call to getPreferredSize() to determine which size to use.

If you subclass JDialog you can enforce a maximum size by subclassing this method.

Example:

@Override 
public Dimension getPreferredSize() {    
  int maxWidth=500;    
  int maxHeight=300;    
  Dimension dim=super.getPreferredSize();    
  if (dim.width>maxWidth) dim.width=maxWidth;    
  if (dim.height>maxHeight) dim.height=maxHeight;    
  return dim; 
}
铃予 2024-11-09 22:19:45

您必须从 JScrollPane 内的 JComponent 计算 getSize() 只是简单的示例

编辑:并调用 JDialog#pack()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class PackDialog {
    private JDialog dlg;
    private JScrollPane scrollPane;
    private JPanel panel;
    private JButton btn;

    public PackDialog() {
        btn = new JButton();
        btn.setPreferredSize(new Dimension(300, 30));
        btn.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActionPerformed(evt);
            }

            private void btnActionPerformed(ActionEvent evt) {
                int h = panel.getHeight();
                h += btn.getHeight();
                int w = panel.getWidth();
                dlg.setPreferredSize(new Dimension(w, h));
                dlg.pack();
            }
        });
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 600));
        panel.setLayout(new BorderLayout(10, 10));
        panel.add(btn, BorderLayout.SOUTH);
        scrollPane = new JScrollPane(panel);
        dlg = new JDialog();
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setPreferredSize(new Dimension(400, 300));
        dlg.add(scrollPane);
        dlg.pack();
        dlg.setVisible(true);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                PackDialog pd = new PackDialog();
            }
        });
    }}

you have to calculate getSize() from JComponents inside JScrollPane just simple example

Edit: and call JDialog#pack()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class PackDialog {
    private JDialog dlg;
    private JScrollPane scrollPane;
    private JPanel panel;
    private JButton btn;

    public PackDialog() {
        btn = new JButton();
        btn.setPreferredSize(new Dimension(300, 30));
        btn.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActionPerformed(evt);
            }

            private void btnActionPerformed(ActionEvent evt) {
                int h = panel.getHeight();
                h += btn.getHeight();
                int w = panel.getWidth();
                dlg.setPreferredSize(new Dimension(w, h));
                dlg.pack();
            }
        });
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 600));
        panel.setLayout(new BorderLayout(10, 10));
        panel.add(btn, BorderLayout.SOUTH);
        scrollPane = new JScrollPane(panel);
        dlg = new JDialog();
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setPreferredSize(new Dimension(400, 300));
        dlg.add(scrollPane);
        dlg.pack();
        dlg.setVisible(true);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

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