如何在 Java 中透過 GridBagLayout 禁止元件縮放
若要禁止使用 GridBagLayout 調整元件大小,請使用 GridBagConstraints NONE 常量 −
GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.NONE;
以下是一個禁止使用 GridBagLayout 調整元件大小的示例 −
示例
package my;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
JLabel label = new JLabel("Rank: ");
JTextArea text = new JTextArea();
text.setText("Add rank here...");
layout.setConstraints(label, gbc);
panel.add(label);
layout.setConstraints(text, gbc);
panel.add(text);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(550, 400);
frame.setVisible(true);
}
}輸出

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