如何用 Java 新增具有相對 Y 位置的元件?\n
要新增具有相對 Y 位置的元件,請使用 GridBagConstraints.RELATIVE 常量。將其設定為 gridy 欄位 −
GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE;
以下是一個如何在 Java 中新增具有相對 Y 位置的元件的示例 −
示例
package my;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
panel.add(new JButton("1st row"), constraints);
constraints.gridx = 1;
constraints.gridy = GridBagConstraints.RELATIVE;
panel.add(new JButton("1st row 2nd column"), constraints);
panel.add(new JButton("2nd row 2nd column"), constraints);
frame.add(panel);
frame.setSize(550, 300);
frame.setVisible(true);
}
}輸出

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