JDesktopPane的首选大小由内容设置

安德鲁·汤普森

我一直试图JDesktopPane通过可调整大小的GUI和滚动窗格来很好地工作,但是这样做有些麻烦。似乎除非拖动模式为大纲模式否则桌面窗格将无法按预期调整大小(当将内部框架拖动到桌面窗格的边缘之外时),因此不会产生滚动条。

在此处输入图片说明

我在这个来源中做的事很愚蠢吗?我错过了更好的方法吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MDIPreferredSize {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final JDesktopPane dt = new JDesktopPane() {

                    @Override
                    public Dimension getPreferredSize() {
                        Dimension prefSize = super.getPreferredSize();
                        System.out.println("prefSize: " + prefSize);
                        // inititialize the max to the first normalized bounds
                        Rectangle max = getAllFrames()[0].getNormalBounds();
                        for (JInternalFrame jif : this.getAllFrames()) {
                            max.add(jif.getNormalBounds());
                        }
                        System.out.println("maxBounds(): "
                                + max);
                        int x1 = max.width + (max.x * 2) < prefSize.width
                                ? prefSize.width
                                : max.width + (max.x * 2);
                        int y1 = max.height + (max.y * 2) < prefSize.height
                                ? prefSize.height
                                : max.height + (max.y * 2);
                        System.out.println("x,y: "
                                + x1
                                + ","
                                + y1);
                        return new Dimension(x1, y1);
                    }
                };
                dt.setAutoscrolls(true);

                int xx = 5;
                int yy = 5;
                int vStep = 10;
                int yStep = 22;
                for (int ii = 0; ii < 3; ii++) {
                    JInternalFrame jif = new JInternalFrame(
                            "Internal Frame " + (ii + 1),
                            true,
                            true,
                            true);
                    dt.add(jif);
                    jif.setLocation(xx, yy);
                    xx += vStep;
                    yy += yStep;
                    jif.setSize(200, 75);
                    jif.setVisible(true);
                }

                ComponentListener componentListener = new ComponentListener() {

                    @Override
                    public void componentResized(ComponentEvent e) {
                        e.getComponent().validate();
                    }

                    @Override
                    public void componentMoved(ComponentEvent e) {
                        e.getComponent().validate();
                    }

                    @Override
                    public void componentShown(ComponentEvent e) {
                        e.getComponent().validate();
                    }

                    @Override
                    public void componentHidden(ComponentEvent e) {
                        // do nothing 
                    }
                };
                // causes maximized internal frames to be resized..
                dt.addComponentListener(componentListener);

                final JCheckBox outLineDragMode = new JCheckBox("Outline Drag Mode");
                ActionListener dragModeListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (outLineDragMode.isSelected()) {
                            dt.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
                        } else {
                            dt.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
                        }
                    }
                };
                outLineDragMode.addActionListener(dragModeListener);

                JPanel gui = new JPanel(new BorderLayout());
                gui.add(outLineDragMode, BorderLayout.PAGE_START);
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.add(new JScrollPane(dt), BorderLayout.CENTER);

                JFrame f = new JFrame("DTP Preferred");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setMinimumSize(f.getSize());

                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);

                printProperty("os.name");
                printProperty("java.version");
                printProperty("java.vendor");
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }

    public static void printProperty(String name) {
        System.out.println(name + ": \t" + System.getProperty(name));
    }
}

编辑

在打印的信息中,另请参阅以下3个系统属性:

os.name:    Windows 7
java.version:   1.7.0_21
java.vendor:    Oracle Corporation

这些是这里的值。

MouseMotionListener 固定

感谢Jonathan Drapeau对a的建议MouseListener,此固定示例实际上使用aMouseMotionListener允许在拖动时主动调整桌面窗格的大小。使用a可能会引起一些奇怪的MouseListener问题,这会引起问题(尚不知道),如果是这样,请回到“在内部框架放置上调整桌面窗格的大小”这一更简单的技术(MouseListener仅限)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

public class MDIPreferredSize {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final JDesktopPane dt = new JDesktopPane() {

                    @Override
                    public Dimension getPreferredSize() {
                        Dimension prefSize = super.getPreferredSize();
                        System.out.println("prefSize: " + prefSize);
                        // inititialize the max to the first normalized bounds
                        Rectangle max = getAllFrames()[0].getNormalBounds();
                        for (JInternalFrame jif : this.getAllFrames()) {
                            max.add(jif.getNormalBounds());
                        }
                        System.out.println("maxBounds(): "
                                + max);
                        int x1 = max.width + (max.x * 2) < prefSize.width
                                ? prefSize.width
                                : max.width + (max.x * 2);
                        int y1 = max.height + (max.y * 2) < prefSize.height
                                ? prefSize.height
                                : max.height + (max.y * 2);
                        System.out.println("x,y: "
                                + x1
                                + ","
                                + y1);
                        return new Dimension(x1, y1);
                    }
                };

                int xx = 5;
                int yy = 5;
                int vStep = 10;
                int yStep = 22;
                for (int ii = 0; ii < 3; ii++) {
                    JInternalFrame jif = new JInternalFrame(
                            "Internal Frame " + (ii + 1),
                            true,
                            true,
                            true);
                    dt.add(jif);
                    jif.setLocation(xx, yy);
                    xx += vStep;
                    yy += yStep;
                    jif.setSize(200, 75);
                    jif.setVisible(true);
                }

                /*final MouseListener mouseListener = new MouseAdapter() {

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        dt.revalidate();
                    }
                };
                */
                final MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        dt.revalidate();
                    }
                };
                for (JInternalFrame jif : dt.getAllFrames()) {
                    for (Component comp : jif.getComponents()) {
                        if (comp instanceof BasicInternalFrameTitlePane) {
                            //comp.addMouseListener(mouseListener);
                            comp.addMouseMotionListener(mouseMotionListener);
                        }
                    }
                }

                dt.setAutoscrolls(true);


                final JCheckBox outLineDragMode =
                        new JCheckBox("Outline Drag Mode");
                ActionListener dragModeListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (outLineDragMode.isSelected()) {
                            dt.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
                        } else {
                            dt.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
                        }
                    }
                };
                outLineDragMode.addActionListener(dragModeListener);

                JPanel gui = new JPanel(new BorderLayout());
                gui.add(outLineDragMode, BorderLayout.PAGE_START);
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.add(new JScrollPane(dt), BorderLayout.CENTER);

                JFrame f = new JFrame("DTP Preferred");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setMinimumSize(f.getSize());

                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);

                printProperty("os.name");
                printProperty("java.version");
                printProperty("java.vendor");
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }

    public static void printProperty(String name) {
        System.out.println(name + ": \t" + System.getProperty(name));
    }
}

怪癖

除了使用a以外,它可能还会MouseListener引起一些奇怪的问题,这会导致问题(尚不知道)。

那是那时

  1. 在完全渲染模式下,只要用户拖动内部框架(甚至离开GUI),桌面窗格就会动态增长。(很好。)在大纲模式下,容器将仅在拖放时调整大小,而不会拖动。(不好,但至少滚动条可靠地显示/消失。)
乔纳森·德拉波(Jonathan Drapeau)

添加MouseListenerJInternalFrame标题窗格而JDesktopPane.LIVE_DRAG_MODErevalidateJDesktopPane释放后一种方式来获得在每个模式完全相同的行为。

            final MouseListener testList = new MouseListener() {

              @Override
              public void mouseReleased(MouseEvent e) {
                dt.revalidate();
              }

              @Override
              public void mousePressed(MouseEvent e) {
              }

              @Override
              public void mouseExited(MouseEvent e) {
              }

              @Override
              public void mouseEntered(MouseEvent e) {
              }

              @Override
              public void mouseClicked(MouseEvent e) {
              }
            };
            // causes maximized internal frames to be resized..
            dt.addComponentListener(componentListener);

            for (JInternalFrame jif : dt.getAllFrames()) {
              for (Component comp : jif.getComponents()) {
                if (comp instanceof BasicInternalFrameTitlePane) {
                  comp.addMouseListener(testList);
                }
              }
            }        

            final JCheckBox outLineDragMode = new JCheckBox("Outline Drag Mode");
            ActionListener dragModeListener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                  if (outLineDragMode.isSelected()) {
                    dt.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
                    for (JInternalFrame jif : dt.getAllFrames()) {
                      for (Component comp : jif.getComponents()) {
                        if (comp instanceof BasicInternalFrameTitlePane) {
                          comp.removeMouseListener(testList);
                        }
                      }
                    }
                  } else {
                    dt.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
                    for (JInternalFrame jif : dt.getAllFrames()) {
                      for (Component comp : jif.getComponents()) {
                        if (comp instanceof BasicInternalFrameTitlePane) {
                          comp.addMouseListener(testList);
                        }
                      }
                    }
                  }
                }
            };

我将其删除,JDesktopPane.OUTLINE_DRAG_MODE因为它已经可以正确反应。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JDialog无法正确设置首选大小?

来自分类Dev

在内容大小之前设置UICollectionViewCell大小

来自分类Dev

OS X“缩放”时,如何设置首选的窗口大小?

来自分类Dev

将div设置为其内容的大小

来自分类Dev

如何使用stroyboard(!)设置UIScrollView的内容大小?

来自分类Dev

根据内容大小设置网格高度

来自分类Dev

在 UITableView 上设置最小内容大小

来自分类Dev

在由Selenium驱动的PhantomJS C#中设置屏幕大小

来自分类Dev

如何在序言中创建由用户设置大小的列表

来自分类Dev

Azure ExecuteBatch-如何在C#中设置首选内容目录标头?

来自分类Dev

Azure ExecuteBatch-如何在C#中设置首选内容目录标头?

来自分类Dev

如何更改实现MigLayout的JDesktopPane中显示的JInternalFrame的大小?

来自分类Dev

控件的首选大小是多少?

来自分类Dev

当页面大小由窗口屏幕大小动态设置时,剑道网格数据不显示

来自分类Dev

将特定大小和内容设置为数组

来自分类Dev

当RelativeLayout的内容更改时,难以设置动画大小

来自分类Dev

设置内容高度后无法更新UITextView大小

来自分类Dev

Scrollview内容大小设置为最大屏幕宽度

来自分类Dev

android在viewholder中设置了widget的大小,但是内容不显示?

来自分类Dev

根据其中的文本设置大小内容可编辑div

来自分类Dev

WPF页面内容未设置为窗口的大小

来自分类Dev

如果大小大于 800 像素,则设置最大内容宽度

来自分类Dev

设置首选任务顺序

来自分类Dev

在WPF应用程序中将“按钮大小”设置为“内容大小”

来自分类Dev

Xcode UIScrollView布局-设置水平内容大小并水平调整子视图的大小

来自分类Dev

Flask-SocketIO的首选生产设置是什么?对Gunicorn感到困惑,因为它只能由一个工人生成

来自分类Dev

在具有自动版式的Interface Builder中的Scrollview中,内容大小是否动态设置为屏幕大小?

来自分类Dev

Unity3D C#:如何根据设置为动态的内容调整输入字段的大小

来自分类Dev

将UITextView框架设置为内容大小在Xcode 5中不再起作用

Related 相关文章

  1. 1

    JDialog无法正确设置首选大小?

  2. 2

    在内容大小之前设置UICollectionViewCell大小

  3. 3

    OS X“缩放”时,如何设置首选的窗口大小?

  4. 4

    将div设置为其内容的大小

  5. 5

    如何使用stroyboard(!)设置UIScrollView的内容大小?

  6. 6

    根据内容大小设置网格高度

  7. 7

    在 UITableView 上设置最小内容大小

  8. 8

    在由Selenium驱动的PhantomJS C#中设置屏幕大小

  9. 9

    如何在序言中创建由用户设置大小的列表

  10. 10

    Azure ExecuteBatch-如何在C#中设置首选内容目录标头?

  11. 11

    Azure ExecuteBatch-如何在C#中设置首选内容目录标头?

  12. 12

    如何更改实现MigLayout的JDesktopPane中显示的JInternalFrame的大小?

  13. 13

    控件的首选大小是多少?

  14. 14

    当页面大小由窗口屏幕大小动态设置时,剑道网格数据不显示

  15. 15

    将特定大小和内容设置为数组

  16. 16

    当RelativeLayout的内容更改时,难以设置动画大小

  17. 17

    设置内容高度后无法更新UITextView大小

  18. 18

    Scrollview内容大小设置为最大屏幕宽度

  19. 19

    android在viewholder中设置了widget的大小,但是内容不显示?

  20. 20

    根据其中的文本设置大小内容可编辑div

  21. 21

    WPF页面内容未设置为窗口的大小

  22. 22

    如果大小大于 800 像素,则设置最大内容宽度

  23. 23

    设置首选任务顺序

  24. 24

    在WPF应用程序中将“按钮大小”设置为“内容大小”

  25. 25

    Xcode UIScrollView布局-设置水平内容大小并水平调整子视图的大小

  26. 26

    Flask-SocketIO的首选生产设置是什么?对Gunicorn感到困惑,因为它只能由一个工人生成

  27. 27

    在具有自动版式的Interface Builder中的Scrollview中,内容大小是否动态设置为屏幕大小?

  28. 28

    Unity3D C#:如何根据设置为动态的内容调整输入字段的大小

  29. 29

    将UITextView框架设置为内容大小在Xcode 5中不再起作用

热门标签

归档