[英]add space between JFrame and JPanel
how I can add space between JFrame and a JPanel inserts into this JFrame? 如何在JFrame和JPanel插入之间添加空间到这个JFrame中? I would insert space in the way that the elements inside the JPanel didn't appear too near at the JFrame 我会以JPanel中的元素在JFrame上看起来不太近的方式插入空格
Set the JPanel's border with an EmptyBorder with appropriate parameters. 使用具有适当参数的EmptyBorder设置JPanel的边框。
ie, 即
// caveat: code not tested
myPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
If the JPanel already has a border, then you can either use a compound border or wrap the JPanel in another JPanel, say using BorderLayout and in the BorderLayout.CENTER position, and give the wrapper JPanel an empty border. 如果JPanel已经有边框,那么你可以使用复合边框或将JPanel包装在另一个JPanel中,例如使用BorderLayout并在BorderLayout.CENTER位置,并为包装器JPanel提供一个空边框。
Why don't you use the GridBagLayout and setBounds for components? 为什么不使用GridBagLayout和setBounds作为组件? the Components will stay at the position you want. 组件将保持在您想要的位置。
Here an example: ` 这是一个例子:`
'import javax.swing.*;
import java.awt.*;
public class set_Components_where_i_want {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setLayout(null);
//make new Components
JButton b1 = new JButton("One");
JButton b2 = new JButton("Two");
JButton b3 = new JButton("Three");
//add Components first
frame.add(b1);
frame.add(b2);
frame.add(b3);
//get frame inserts
Insets insets = frame.getInsets();
Dimension size = b1.getPreferredSize();
//set position here
b1.setBounds(50 + insets.left, 10 + insets.top,
size.width, size.height);
size = b2.getPreferredSize();
b2.setBounds(110 + insets.left, 80 + insets.top,
size.width, size.height);
size = b3.getPreferredSize();
b3.setBounds(300 + insets.left, 60 + insets.top,
size.width + 100, size.height + 40);
//set size for the frame so it can contain all Components
frame.setSize(600 + insets.left + insets.right,
250 + insets.top + insets.bottom);
// make the frame be visible
frame.setVisible(true);
}
}` }`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.