如何在 Java 中创建两个组件之间的不可见固定宽度组件?
使用 createHorizontalStrut() 方法在两个组件之间创建一个不可见的宽度组件。假设我们有一些按钮,我们正在它们之间创建一个固定宽度 −
box.add(button4); box.add(Box.createHorizontalStrut(50)); box.add(button5); box.add(Box.createHorizontalStrut(30)); 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("Groups");
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");
JButton button7 = new JButton("KXIP");
JButton button8 = new JButton("RCB");
Box box = new Box(BoxLayout.X_AXIS);
box.add(button1);
box.add(button2);
box.add(button3);
box.add(button4); box.add(Box.createHorizontalStrut(50));
box.add(button5); box.add(Box.createHorizontalStrut(30));
box.add(button6);
box.add(button7);
box.add(button8);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(box);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(550, 350);
frame.setVisible(true);
}
}output

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