【问题标题】:setting the size of a JPanel设置 JPanel 的大小
【发布时间】:2013-11-17 19:34:03
【问题描述】:

我有一个扩展名为 Row 的 JPanel 的类。我有一堆Row添加到JLabel,代码如下:

JFrame f=new JFrame();

JPanel rowPanel = new JPanel();
//southReviewPanel.setPreferredSize(new Dimension(400,130));
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(test1);
rowPanel.add(test1);
rowPanel.add(test2);
rowPanel.add(test3);
rowPanel.add(test4);
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize()); 
rowPanel.setMinimumSize(rowPanel.getPreferredSize());

f.setSize(new Dimension(300,600));

JScrollPane sp = new JScrollPane(rowPanel);
sp.setSize(new Dimension(300,600));
f.add(sp);

f.setVisible(true);

其中 test1...etc 是一个行。但是,当我调整窗口大小时,Row 的布局会变得混乱(它也会调整大小)......我怎样才能防止这种情况发生?

【问题讨论】:

    标签: java swing jpanel


    【解决方案1】:

    阅读 Using Layout Managers 上的 Swing 教程。每个布局管理器都有自己的规则来调整容器大小时会发生什么。试验和玩耍。

    在 BoxLayout 的情况下,它应该尊重添加到面板的组件的最大尺寸,以便您可以这样做:

    childPanel.setMaximumSize( childPanel.getPreferredSize() );
    

    如果您需要更多帮助,请发布您的 SSCCE 来展示问题。

    【讨论】:

    • 我确实按照您上面的建议使用 getPreferredSize 和 setMaximumSize 进行了更改(请参阅我编辑的代码)......仍然,因为代码甚至不存在。如果我有边框布局怎么办?如何设置尺寸?您在上面给出的链接中没有提到 BorderLayout
    • 您编辑的代码不是 SSCCE。是的,该链接确实提到了 BorderLayout。你不可能在 6 分钟内读完整个部分!!!布局管理器处理添加到面板的组件,而不是面板本身。
    • 没有说任何会使其无法调整大小的内容
    【解决方案2】:

    我采用了http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java 中的代码,并根据您要执行的操作对其进行了调整,仅使用按钮而不是自定义 JPanel:

    public class BoxLayoutDemo {
        public static void addComponentsToPane(Container pane) {
            JPanel rowPanel = new JPanel();
            pane.add(rowPanel);
    
            rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
            rowPanel.add(addAButton("Button 1"));
            rowPanel.add(addAButton("Button 2"));
            rowPanel.add(addAButton("Button 3"));
            rowPanel.add(addAButton("Button 4"));
            rowPanel.add(addAButton("5"));
            rowPanel.setPreferredSize(new Dimension(600, 400));
            rowPanel.setMaximumSize(rowPanel.getPreferredSize()); 
            rowPanel.setMinimumSize(rowPanel.getPreferredSize());
        }
    
        private static JButton addAButton(String text) {
            JButton button = new JButton(text);
            button.setAlignmentX(Component.CENTER_ALIGNMENT);
            return button;
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("BoxLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addComponentsToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    

    最终结果是这样的:

    如您所见,按钮行完全对齐。如果您调整 JFrame 的大小,它们将保持对齐。这就是你要找的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多