简体   繁体   English

为什么 Flow Layout 上的按钮会离开屏幕?

[英]Why do buttons go off screen on Flow Layout?

I'm learning flow layout from Head First Java and I'm trying to have buttons wrap around as it says a Flow Layout should (left to right, top to bottom).我正在从 Head First Java 学习流布局,我试图让按钮环绕,因为它说Flow Layout应该(从左到右,从上到下)。

import javax.swing.*;
import java.awt.*;
public class Flow {
    public static void main(String[] args) {
        Flow gui = new Flow();
        gui.go();
    }
    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        JButton buttonOne = new JButton("hello");
        JButton buttonTwo = new JButton("this is");
        JButton buttowThree = new JButton("woody");

        panel.add(buttonOne);
        panel.add(buttonTwo);
        panel.add(buttonThree);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        frame.setSize(200,200);
        frame.setVisible(true);
    }
}

However, when setting the panel on the EAST region of the frame, the buttons go off the screen and don't wrap around.但是,在框架的EAST区域上设置面板时,按钮会离开屏幕并且不会环绕。 If I set the panel on the NORTH or SOUTH region, I only see two buttons.如果我将面板设置在NORTHSOUTH区域,我只会看到两个按钮。 If I set the panel on the CENTER region, they DO wrap and can see all of them clearly.如果我将面板设置在CENTER区域,它们会环绕并且可以清楚地看到所有这些。 Why is this?为什么是这样?

If you're adding a component to a BorderLayout frame, the East and West regions will let the component get its preferred width.如果您将组件添加到BorderLayout框架,则EastWest区域将让组件获得其首选宽度。 Since the panel contains three buttons and places them side by side, the preferred width of the panel is that of the three buttons side by side.由于面板包含三个按钮并将它们并排放置,面板的首选宽度是三个按钮并排的宽度。 Therefore, it is going "off-the-screen" since that is the preferred width.因此,它会“离开屏幕”,因为这是首选宽度。 If you were to a button on to the frame on the EAST region instead and fill it up with enough text, the same would happen.如果您改为在EAST区域的框架上单击按钮并用足够的文本填充它,也会发生同样的情况。

The CENTER region gets whatever is left that the other regions haven't taken up already. CENTER区域获取其他区域尚未占用的剩余部分。 Since that is the policy of the CENTER region, the panel does NOT get its preferred width or height, therefore forcing the components of the panel to wrap.因为这是政策CENTER区域,所述面板没有得到它的优选的宽度或高度,因此迫使面板的部件来包装。

As far as the NORTH and SOUTH regions go, the panel will get its preferred height which is the height of the tallest component (in the example all buttons are the same height).NORTHSOUTH区域而言,面板将获得其首选高度,即最高组件的高度(在示例中,所有按钮的高度相同)。 Therefore it sees no reason to wrap.因此,它认为没有理由换行。 The panel however, DOESN'T get its preferred width.然而,面板没有得到它的首选宽度。 Therefore the buttons are "cut off" since the max width the panel can have is that of the frame.因此按钮被“切断”,因为面板可以具有的最大宽度是框架的宽度。 You will notice as the frame resizes, the buttons appear on the screen as space is made available on the panel.您会注意到随着框架调整大小,按钮出现在屏幕上,因为面板上有可用空间。

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

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