[英]Java: Extra space in AbsoluteLayout
是否可以在使用AbsoluteLayout的JFrame邊緣周圍留出一些額外的空間? 當我有一個按鈕作為JFrame上最向下的組件時,它會直接定位在JFrame窗口的底部邊緣,看起來很糟糕。 我想知道是否有一種方法可以在使用AbsoluteLayout時在組件和JFrame邊緣之間添加一些額外的空間。
建議:
例如,請注意在下面的代碼中創建的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(維度)創建一個可以在按鈕下方添加的空白區域。
在內容面板上設置一個空邊框,其中SIZE
是您想要的填充量。
JFrame frame = new JFrame();
JPanel panel = new JPanel(null);
panel.setBorder(BorderFactory.createEmptyBorder(SIZE,SIZE,SIZE,SIZE);
frame.setContentPane(panel);
//The rest
參數用於頂部,左側,底部和右側填充,因此如果您希望每條邊上有不同的填充,則可以相應地進行設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.