Java 程式,用於使用 GridBagLayout 建立佈局
GridBagLayout 建立了一個網格包佈局管理器。它以橫向和縱向排列元件。
在此處,我們有一個框架和麵板。該面板有兩個使用 GridBagLayout 排列的元件 −
JFrame frame = new JFrame("Demo Frame");
JPanel panel = new JPanel();
JLabel label = new JLabel("Email-Id: ");
JTextArea text = new JTextArea();
text.setText("Add id here...");
panel.setLayout(new GridBagLayout());現在,將元件設定到該面板 −
panel.add(label); panel.add(text);
以下是使用 GridBagLayout 建立佈局的示例 −
示例
package my;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
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("Demo Frame");
JPanel panel = new JPanel();
JLabel label = new JLabel("Email-Id: ");
JTextArea text = new JTextArea();
text.setText("Add id here...");
panel.setLayout(new GridBagLayout());
panel.add(label);
panel.add(text);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
}
}輸出

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