Java - 如何防止 BorderLayout EAST 拥抱屏幕的一侧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2212647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:04:05  来源:igfitidea点击:

Java - How do I prevent BorderLayout EAST from hugging the side of the screen?

javaswing

提问by ShrimpCrackers

If I add components like JButtons on the East or West side, how do I prevent it from hugging the side of the screen? I want some space between the JButtons and the edge of the screen.

如果我JButton在东侧或西侧添加s 之类的组件,如何防止它抱住屏幕的一侧?我想要在JButtons 和屏幕边缘之间有一些空间。

采纳答案by SyntaxT3rr0r

call setBorderon your JButton like this:

setBorder像这样调用你的 JButton:

setBorder( new EmptyBorder( 3, 3, 3, 3 ) )

From the JavaDoc, an EmptyBorder is a "transparent border which takes up space but does no drawing". In my example it shall take 3 pixels respectively top, left, bottom and right.

在 JavaDoc 中,EmptyBorder 是“占用空间但不绘制的透明边框”。在我的示例中,它将分别占用 3 个像素的顶部、左侧、底部和右侧。

Complete code whose purpose is only to show how to use EmptyBorder:

完整代码,其目的仅在于展示如何使用 EmptyBorder:

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;

public class ALineBorder {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Line Borders");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button1 = new JButton("Button1");
        button1.setBorder( new EmptyBorder( 8, 8, 8, 8 ) );
        JButton button2 = new JButton("Button2");
        JButton button3 = new JButton("Button3");
        button3.setBorder( new EmptyBorder( 16, 16, 16, 16 ) );
        Container contentPane = frame.getContentPane();
        contentPane.add(button1, BorderLayout.WEST);
        contentPane.add(button2, BorderLayout.CENTER);
        contentPane.add(button3, BorderLayout.EAST);
        frame.pack();
        frame.setSize(300, frame.getHeight());
        frame.setVisible(true);
    }

}

回答by finnw

Most likely you have (or soon will have) more than one button in the container, and want to align them horizontally. So consider putting the buttons within in a nested JPanelwith a GridBagLayout:

很可能您在容器中拥有(或很快将拥有)多个按钮,并希望将它们水平对齐。所以考虑将按钮内嵌套JPanelGridBagLayout

class ButtonPanel extends JPanel {
    ButtonPanel() {
        setLayout(new GridBagLayout());
    }

    @Override
    public Component add(Component button) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = nextGridY++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(3, 3, 3, 3);
        super.add(button, gbc);
        return button;
    }

    int nextGridY;
}

Then add this panel to the parent frame or panel (with a BorderLayout.)

然后将此面板添加到父框架或面板(带有BorderLayout.)

回答by Thorbj?rn Ravn Andersen

BorderLayout goes as the name says to the border. You can however nest things inside, so you could insert a JPanel with a border and then put your button in that.

BorderLayout 顾名思义就是边框。但是,您可以将内容嵌套在里面,因此您可以插入一个带边框的 JPanel,然后将您的按钮放入其中。

You may also want to experiment with the GUI designer in Netbeans. It is really quite nice, and give a lot of help to things you usually want to do (like have a margin to the border, etc).

您可能还想在 Netbeans 中试验 GUI 设计器。它真的非常好,并且为您通常想做的事情提供了很多帮助(例如边界有余量等)。