简体   繁体   English

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

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

If I add components like JButton s on the East or West side, how do I prevent it from hugging the side of the screen? 如果我在东方或西方添加像JButton的组件,我该如何阻止它在屏幕的一侧? I want some space between the JButton s and the edge of the screen. 我想在JButton和屏幕边缘之间JButton一些空间。

call setBorder on your JButton like this: 在JButton上调用setBorder ,如下所示:

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

From the JavaDoc, an EmptyBorder is a "transparent border which takes up space but does no drawing". 从JavaDoc开始,EmptyBorder是一个“透明边框,占用空间但没有绘图”。 In my example it shall take 3 pixels respectively top, left, bottom and right. 在我的例子中,它应分别为顶部,左侧,底部和右侧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);
    }

}

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 JPanel with a GridBagLayout : 因此,请考虑将按钮放在带有GridBagLayout的嵌套JPanel

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 。)

BorderLayout goes as the name says to the border. BorderLayout正如名字所说的边界。 You can however nest things inside, so you could insert a JPanel with a border and then put your button in that. 然而,您可以将内容嵌入其中,因此您可以插入带边框的JPanel,然后将按钮放入其中。

You may also want to experiment with the GUI designer in Netbeans. 您可能还想在Netbeans中试验GUI设计器。 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). 这真的很不错,并为你通常想做的事情提供了很多帮助(比如边界边缘等等)。

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

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