如何在 Java 中设置 GridLayout 中元素之间的垂直间隙?
使用 setVgap() 方法来设置 GridLayout 中元素之间的垂直间隙。比方说我们有一个 GridLayout −
GridLayout layout = new GridLayout(3,3);
设置水平间隙 −
layout.setVgap(30);
以下是一个示例 −
示例
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Sections"); JPanel panel = new JPanel(); panel.setBackground(Color.blue); GridLayout layout = new GridLayout(3,3); layout.setVgap(30); panel.setLayout(layout); panel.add(new JButton("Overview")); panel.add(new JButton("Samples")); panel.add(new JButton("Tutorials")); panel.add(new JButton("Support")); panel.add(new JButton("InterviewQA")); panel.add(new JButton("Tools")); panel.add(new JButton("Editors")); frame.add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
输出
广告