java swing 设置组件大小,Java Swing - 如何将组件的首选大小保留在BorderLayout的中心...

此博客展示了如何在Java Swing中使用BorderLayout和BoxLayout布局管理器创建用户界面。示例包括在North区域添加标签,在South区域添加组合框,以及在Center区域使用Box和GridBagLayout来组织组件。
摘要由CSDN通过智能技术生成

import java.awt.BorderLayout;

import java.awt.GridBagLayout;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class Main extends JFrame {

public static void main(String args[]) {

Main bnb = new Main();

bnb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

bnb.createUI();

bnb.setVisible(true);

}

public void createUI() {

JPanel borderPanel = new JPanel(new BorderLayout());

JLabel northLabel = new JLabel("Nawth");

borderPanel.add(northLabel, BorderLayout.NORTH);

JComboBox southCombo = new JComboBox();

borderPanel.add(southCombo, BorderLayout.SOUTH);

JPanel centerPanel = new JPanel();

centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));

JComboBox firstCombo = new JComboBox();

centerPanel.add(firstCombo);

centerPanel.add(Box.createVerticalGlue());

JPanel centerPanelConstrain = new JPanel(new GridBagLayout());

centerPanelConstrain.add(centerPanel);

borderPanel.add(centerPanelConstrain, BorderLayout.CENTER);

getContentPane().add(borderPanel);

pack();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值