我希望通过调整窗口大小来调整JFrame上的组件大小。
如何做到这一点。
目前,当我使JFrame变小时,组件会保留在同一个位置,然后随着帧变小而消失。
理想情况下,我想设置一个最小的JFrame大小,这个大小不允许表单变得更小,但是当我把它变大时,组件应该随JFrame一起移动。
添加:如果我想使用绝对布局管理器怎么办?这是我老板喜欢的布局管理器。
代码:
import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JCheckBox; import java.awt.Font;
公共类TestFrame扩展了JFrame { private static final long serialVersionUID = 1L; 私人JPanel contentPane; 私人JButton btnNewButton; 私人JButton btnNewButton_1; 私人JButton btnNewButton_2; 私人JCheckBox chckbxNewCheckBox; 私人JCheckBox chckbxNewCheckBox_1; 私人JCheckBox chckbxNewCheckBox_2;
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 458);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
setContentPane(contentPane);
contentPane.setLayout(null);
btnNewButton = new JButton("Save");
btnNewButton.setFont(new Font("Sylfaen", Font.BOLD, 14));
btnNewButton.setBounds(514, 386, 89, 23);
contentPane.add(btnNewButton);
btnNewButton_1 = new JButton("Open");
btnNewButton_1.setBounds(514, 352, 89, 23);
contentPane.add(btnNewButton_1);
btnNewButton_2 = new JButton("Close");
btnNewButton_2.setBounds(514, 318, 89, 23);
contentPane.add(btnNewButton_2);
btnNewButton_2.setBackground(Color.YELLOW);
btnNewButton_2.setEnabled(false);
chckbxNewCheckBox = new JCheckBox("Employeed");
chckbxNewCheckBox.setBounds(506, 7, 97, 23);
contentPane.add(chckbxNewCheckBox);
chckbxNewCheckBox_1 = new JCheckBox("Not Employeed");
chckbxNewCheckBox_1.setBounds(506, 33, 97, 23);
contentPane.add(chckbxNewCheckBox_1);
chckbxNewCheckBox_2 = new JCheckBox("Self Employeed");
chckbxNewCheckBox_2.setBounds(506, 59, 97, 23);
contentPane.add(chckbxNewCheckBox_2);
}
}
GridBagLayout的示例:
这是GridBagLayout的一个示例。组件正确调整大小,但我没有看到增长属性。它必须自动完成。
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}