如何在 Java Swing 中垂直排列 JButtons?
使用 Box 類將按鈕垂直排列。使用 createVerticalBox() 方法,從上到下顯示元件 -
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
JButton button3 = new JButton("Three");
Box box = Box.createVerticalBox();
box.add(button1);
box.add(button2);
box.add(button3);以下是一個按順序垂直放置按鈕的示例 -
示例
package my;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SwingDemo {
public static void main(String[] args) {
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
JButton button3 = new JButton("Three");
JButton button4 = new JButton("Four");
JButton button5 = new JButton("Five");
JButton button6 = new JButton("Six");
JButton button7 = new JButton("Seven");
Box box = Box.createVerticalBox();
box.add(button1);
box.add(button2);
box.add(button3);
box.add(button4);
box.add(button5);
box.add(button6);
box.add(button7);
JFrame frame = new JFrame();
frame.add(box);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(500, 300);
frame.setVisible(true);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP