java组件大小_想用窗口调整组件大小?

我希望通过调整窗口大小来调整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;

public class 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值