如何在 Java Swing 中使用 EmptyBorder 留出空白区域
我们首先创建一个 JPanel 并设置标题边框
JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));
现在创建 Empty Border
JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
以下是如何在 Java Swing 中使用 EmptyBorder 留出空白区域的示例
示例
import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel")); JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100)); panel2.setPreferredSize(new Dimension(700, 350)); JFrame frame = new JFrame("Set Space"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel2); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } }
输出
广告