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