为什么在更改框架宽度时仅调整左侧按钮的大小? 如何将所有字段调整大小? 我尝试使用Swing做灵活的计算器。 我应该使用fill = GridBagConstraints.BOTH;或者说不同的东西?
package zda;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Insets;
public class CalcAlfa {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcAlfa window = new CalcAlfa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CalcAlfa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 462);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{50, 50, 50, 50, 50};
gridBagLayout.rowHeights = new int[]{50, 50, 50, 50, 50};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 5);
gbc_textField.fill = GridBagConstraints.BOTH;
gbc_textField.gridwidth = 4;
gbc_textField.gridx = 0;
gbc_textField.gridy = 0;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
JButton b1 = new JButton("C");
GridBagConstraints gbc_b1 = new GridBagConstraints();
gbc_b1.insets = new Insets(0, 0, 5, 5);
gbc_b1.fill = GridBagConstraints.BOTH;
gbc_b1.gridx = 4;
gbc_b1.gridy = 0;
frame.getContentPane().add(b1, gbc_b1);
JButton b2 = new JButton("7");
GridBagConstraints gbc_b2 = new GridBagConstraints();
gbc_b2.insets = new Insets(0, 0, 5, 5);
gbc_b2.fill = GridBagConstraints.BOTH;
gbc_b2.gridx = 0;
gbc_b2.gridy = 1;
frame.getContentPane().add(b2, gbc_b2);
JButton b3 = new JButton("8");
GridBagConstraints gbc_b3 = new GridBagConstraints();
gbc_b3.insets = new Insets(0, 0, 5, 5);
gbc_b3.fill = GridBagConstraints.BOTH;
gbc_b3.gridx = 1;
gbc_b3.gridy = 1;
frame.getContentPane().add(b3, gbc_b3);
JButton b4 = new JButton("9");
GridBagConstraints gbc_b4 = new GridBagConstraints();
gbc_b4.insets = new Insets(0, 0, 5, 5);
gbc_b4.fill = GridBagConstraints.BOTH;
gbc_b4.gridx = 2;
gbc_b4.gridy = 1;
frame.getContentPane().add(b4, gbc_b4);
JButton b5 = new JButton("/");
GridBagConstraints gbc_b5 = new GridBagConstraints();
gbc_b5.insets = new Insets(0, 0, 5, 5);
gbc_b5.fill = GridBagConstraints.BOTH;
gbc_b5.gridx = 3;
gbc_b5.gridy = 1;
frame.getContentPane().add(b5, gbc_b5);
JButton b6 = new JButton("sqrt");
GridBagConstraints gbc_b6 = new GridBagConstraints();
gbc_b6.insets = new Insets(0, 0, 5, 5);
gbc_b6.fill = GridBagConstraints.BOTH;
gbc_b6.gridx = 4;
gbc_b6.gridy = 1;
frame.getContentPane().add(b6, gbc_b6);
}
}