如何在 Java 中创建胶合以填充相邻组件之间的空白?
假设我们有 6 个组件,并且我们需要填充其中一些组件之间的空白 -
JButton button1 = new JButton("CSK");
JButton button2 = new JButton("DC");
JButton button3 = new JButton("MI");
JButton button4 = new JButton("SRH");
JButton button5 = new JButton("RR");
JButton button6 = new JButton("KKR");如需填充空白并分隔组件,请使用 createGlue() 方法创建胶合 -
Box box = new Box(BoxLayout.X_AXIS); box.add(button1); box.add(button2); box.add(Box.createGlue()); box.add(button3); box.add(button4); box.add(Box.createGlue()); box.add(button5); box.add(button6);
以下是一个填充相邻组件之间空白的示例 -
示例
package my;
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class SwingDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Matches");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("CSK");
JButton button2 = new JButton("DC");
JButton button3 = new JButton("MI");
JButton button4 = new JButton("SRH");
JButton button5 = new JButton("RR");
JButton button6 = new JButton("KKR");
Box box = new Box(BoxLayout.X_AXIS);
box.add(button1);
box.add(button2);
box.add(Box.createGlue());
box.add(button3);
box.add(button4);
box.add(Box.createGlue());
box.add(button5);
box.add(button6);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(box);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(550, 250);
frame.setVisible(true);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP