如何在 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); } }
输出
广告