简体   繁体   English

Java:AbsoluteLayout中的额外空间

[英]Java: Extra space in AbsoluteLayout

Is it possible to have some extra space around the edges of a JFrame that uses AbsoluteLayout? 是否可以在使用AbsoluteLayout的JFrame边缘周围留出一些额外的空间? When I have a button as the downwardsmost component on the JFrame, it gets positioned right up against the bottom edge of the JFrame window, and it looks bad. 当我有一个按钮作为JFrame上最向下的组件时,它会直接定位在JFrame窗口的底部边缘,看起来很糟糕。 I would like to know if there's a way to add a little extra space between components and the edge of the JFrame while using AbsoluteLayout. 我想知道是否有一种方法可以在使用AbsoluteLayout时在组件和JFrame边缘之间添加一些额外的空间。

Suggestions: 建议:

  • When you add a component to a JFrame, you're actually adding it to the JFrame's contentPane. 将组件添加到JFrame时,实际上是将其添加到JFrame的contentPane中。 To give the contentPane a "buffer" border, consider giving it an EmptyBorder(...) with the parameters being int constants for the amount of border desired around the component. 要给contentPane一个“缓冲区”边框,请考虑给它一个EmptyBorder(...),其参数是int常量,用于组件周围所需的边框量。
  • Avoid using "absolute" layouts for anything, and especially for placing components at easy to place locations for the layout managers, such as at the bottom of the GUI. 避免对任何事物使用“绝对”布局,尤其是将组件放置在布局管理器的易于放置的位置,例如在GUI的底部。

For example, note in the GUI created in the code below how the center and bottom JPanel's don't go out to the edge of the GUI because of the empty border: 例如,请注意在下面的代码中创建的GUI中,由于空边框,中心和底部JPanel不会出现在GUI的边缘:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.*;

public class ButtonAtBottom {
   private static void createAndShowGui() {
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(new JButton("Bottom Button"));
      bottomPanel.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));

      JPanel centerPanel = new JPanel();
      centerPanel.setBorder(BorderFactory.createTitledBorder("Center Panel"));


      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(centerPanel, BorderLayout.CENTER);
      mainPanel.add(bottomPanel, BorderLayout.PAGE_END);

      // **** here I add the border to the mainPanel which I'll 
      // make into the contentPane
      int eb = 25;
      mainPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));

      // don't set the preferredSize per Kleopatra, but am doing it
      // here simply to make code shorter for this sscce
      mainPanel.setPreferredSize(new Dimension(500, 400));

      JFrame frame = new JFrame("ButtonAtBottom");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setContentPane(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

您可以使用Box.createRigidArea(维度)创建一个可以在按钮下方添加的空白区域。

Set an empty border on your content panel where SIZE is the amount of padding you want. 在内容面板上设置一个空边框,其中SIZE是您想要的填充量。

JFrame frame = new JFrame();
JPanel panel = new JPanel(null);
panel.setBorder(BorderFactory.createEmptyBorder(SIZE,SIZE,SIZE,SIZE);
frame.setContentPane(panel); 
//The rest

The arguments are for top, left, bottom and right padding so if you want different paddings on each edge, you can set it accordingly. 参数用于顶部,左侧,底部和右侧填充,因此如果您希望每条边上有不同的填充,则可以相应地进行设置。

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

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