简体   繁体   English

Java Swing GUI BorderLayout:组件的位置

[英]Java Swing GUI BorderLayout: Location of components

I'm new to Java and I'm playing around with a simple GUI example:我是 Java 的新手,我正在玩一个简单的 GUI 示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class DrawTest {

    class DrawingPanel extends JPanel {

        private Rectangle2D shape;

        public DrawingPanel(Rectangle2D shape) {
            this.shape = shape;
        }

        public void paintComponent(Graphics g) {

            Graphics2D g2D = (Graphics2D) g;            
            super.paintComponent(g2D);  
            g2D.setColor(new Color(31, 21, 1));
            g2D.fill(shape);

        }

    }


    public void draw() {
        JFrame frame = new JFrame();
        Rectangle2D shape = new Rectangle2D.Float();
        final DrawingPanel drawing = new DrawingPanel(shape);

        shape.setRect(0, 0, 400, 400);
        frame.getContentPane().add(BorderLayout.NORTH, new JButton("TestN"));
        frame.getContentPane().add(BorderLayout.SOUTH, new JButton("TestS"));
        frame.getContentPane().add(BorderLayout.EAST, new JButton("TestE"));
        frame.getContentPane().add(BorderLayout.WEST, new JButton("TestW"));
        frame.getContentPane().add(BorderLayout.CENTER, drawing);
        frame.pack();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);  
    }
}

public class DrawMain {
    public static void main(String[] args) {
        DrawTest test = new DrawTest();
        test.draw();

    }
}

As expected, this code produces a frame with the rectangle at the centre and buttons around it.正如预期的那样,这段代码生成了一个框架,矩形在中心,按钮在它周围。 However, if I change the code like this:但是,如果我像这样更改代码:

        frame.getContentPane().add(BorderLayout.NORTH, drawing);
        frame.getContentPane().add(BorderLayout.SOUTH, new JButton("TestS"));
        frame.getContentPane().add(BorderLayout.EAST, new JButton("TestE"));
        frame.getContentPane().add(BorderLayout.WEST, new JButton("TestW"));
        frame.getContentPane().add(BorderLayout.CENTER, new JButton("TestC"));

the "TestC" button gets a huge area in the middle while the rectangle doesn't get enough space. “TestC”按钮在中间有一个很大的区域,而矩形没有足够的空间。 This is even true if I remove the other buttons (TestS, TestE, TestW): I get a huge TestC button and a tiny part of the rectangle (even not the scaled rectangle) at the top.如果我移除其他按钮(TestS、TestE、TestW),情况也是如此:我在顶部得到一个巨大的 TestC 按钮和矩形的一小部分(甚至不是缩放的矩形)。

Why doesn't the rectangle get enough space when it's drawn at the top (NORTH) but does get it when it's drawn at the CENTER?为什么矩形在顶部(北)绘制时没有足够的空间,但在中心绘制时却得到了?

The DrawingPanel should @Override getPreferredSize() to return an appropriate size. DrawingPanel应该@Override getPreferredSize()以返回适当的大小。

The layout manager will then take that preferred size as a hint .然后,布局管理器将采用该首选尺寸作为提示 Some layout managers will expand a component's height or width according to the logic of the layout and constraint.一些布局管理器会根据布局和约束的逻辑扩展组件的高度或宽度。 EG a BorderLayout will stretch components in the PAGE_START / PAGE_END to the width of the content pane, and LINE_START / LINE_END to the height of the tallest of either of those, or the CENTER .例如, BorderLayout会将PAGE_START / PAGE_END中的组件拉伸到内容窗格的宽度,并将LINE_START / LINE_END到其中最高者或CENTER的高度。 A GridBagLayout OTOH will completely hide / remove a component for which there is not enough space to display it at the preferred size, and that's where 'pack' comes in. GridBagLayout OTOH 将完全隐藏/删除没有足够空间以首选大小显示它的组件,这就是“pack”的用武之地。

So change frame.setSize(500,500);所以改变frame.setSize(500,500); (which is no better than a guess) to frame.pack(); (这并不比猜测更好)到frame.pack(); , which will make frame the minimum size it needs to be, in order to display the components it contains. ,这将使框架成为它需要的最小尺寸,以便显示它包含的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM