java swing 空白_在Swing GUI中提供空白区域

本文介绍如何使用Java Swing中的不同布局管理器(BorderLayout, FlowLayout, GridLayout, GridBagLayout 和 CardLayout)来创建GUI中的空白区域。通过示例代码展示每个布局的用法,包括设置组件间隔和边界。" 50814426,5305667,OpenStack私有云实践:企业级部署与资源管理,"['云计算', 'OpenStack', '虚拟化', '资源管理', '企业级部署']
摘要由CSDN通过智能技术生成

使用LayoutManagers各种组件可以提供各种组件之间的间隔。

1.)BorderLayout:

2.)FlowLayout:

3.)GridLayout:

4.)GridBagLayout:

5.)CardLayout(示例):

显示所有构造函数的示例:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LayoutExample {

private final int hGap = 5;

private final int vGap = 5;

private String[] borderConstraints = {

BorderLayout.PAGE_START,

BorderLayout.LINE_START,

BorderLayout.CENTER,

BorderLayout.LINE_END,

BorderLayout.PAGE_END

};

private JButton[] buttons;

private GridBagConstraints gbc;

private JPanel borderPanel;

private JPanel flowPanel;

private JPanel gridPanel;

private JPanel gridBagPanel;

private JPanel cardPanel;

public LayoutExample() {

buttons = new JButton[16];

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.insets = new Insets(hGap, vGap, hGap, vGap);

}

private void displayGUI() {

JFrame frame = new JFrame("Layout Example");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel contentPane = new JPanel(

new GridLayout(0, 1, hGap, vGap));

contentPane.setBorder(

BorderFactory.createEmptyBorder(hGap, vGap, hGap, vGap));

borderPanel = new JPanel(new BorderLayout(hGap, vGap));

borderPanel.setBorder(

BorderFactory.createTitledBorder("BorderLayout"));

borderPanel.setOpaque(true);

borderPanel.setBackground(Color.WHITE);

for (int i = 0; i < 5; i++) {

buttons[i] = new JButton(borderConstraints[i]);

borderPanel.add(buttons[i], borderConstraints[i]);

}

contentPane.add(borderPanel);

flowPanel = new JPanel(new FlowLayout(

FlowLayout.CENTER, hGap, vGap));

flowPanel.setBorder(

BorderFactory.createTitledBorder("FlowLayout"));

flowPanel.setOpaque(true);

flowPanel.setBackground(Color.WHITE);

for (int i = 5; i < 8; i++) {

buttons[i] = new JButton(Integer.toString(i));

flowPanel.add(buttons[i]);

}

contentPane.add(flowPanel);

gridPanel = new JPanel(new GridLayout(2, 2, hGap, vGap));

gridPanel.setBorder(

BorderFactory.createTitledBorder("GridLayout"));

gridPanel.setOpaque(true);

gridPanel.setBackground(Color.WHITE);

for (int i = 8; i < 12; i++) {

buttons[i] = new JButton(Integer.toString(i));

gridPanel.add(buttons[i]);

}

contentPane.add(gridPanel);

gridBagPanel = new JPanel(new GridBagLayout());

gridBagPanel.setBorder(

BorderFactory.createTitledBorder("GridBagLayout"));

gridBagPanel.setOpaque(true);

gridBagPanel.setBackground(Color.WHITE);

buttons[12] = new JButton(Integer.toString(12));

addComp(gridBagPanel, buttons[12], 0, 0, 1, 1

, GridBagConstraints.BOTH, 0.33, 0.5);

buttons[13] = new JButton(Integer.toString(13));

addComp(gridBagPanel, buttons[13], 1, 0, 1, 1

, GridBagConstraints.BOTH, 0.33, 0.5);

buttons[14] = new JButton(Integer.toString(14));

addComp(gridBagPanel, buttons[14], 0, 1, 2, 1

, GridBagConstraints.BOTH, 0.66, 0.5);

buttons[15] = new JButton(Integer.toString(15));

addComp(gridBagPanel, buttons[15], 2, 0, 1, 2

, GridBagConstraints.BOTH, 0.33, 1.0);

contentPane.add(gridBagPanel);

cardPanel = new JPanel(new CardLayout(hGap, vGap));

cardPanel.setBorder(

BorderFactory.createTitledBorder("CardLayout"));

cardPanel.setOpaque(true);

cardPanel.setBackground(Color.WHITE);

cardPanel.add(getPanel(Color.BLUE));

cardPanel.add(getPanel(Color.GREEN));

contentPane.add(cardPanel);

frame.setContentPane(contentPane);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

private JPanel getPanel(Color bColor) {

JPanel panel = new JPanel(new FlowLayout(

FlowLayout.CENTER, hGap, vGap));

panel.setOpaque(true);

panel.setBackground(bColor.darker().darker());

JButton swapperButton = new JButton("Next");

swapperButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

CardLayout cardLayout = (CardLayout) cardPanel.getLayout();

cardLayout.next(cardPanel);

}

});

panel.add(swapperButton);

return panel;

}

private void addComp(JPanel panel, JComponent comp

, int x, int y, int gWidth

, int gHeight, int fill

, double weightx, double weighty) {

gbc.gridx = x;

gbc.gridy = y;

gbc.gridwidth = gWidth;

gbc.gridheight = gHeight;

gbc.fill = fill;

gbc.weightx = weightx;

gbc.weighty = weighty;

panel.add(comp, gbc);

}

public static void main(String[] args) {

Runnable runnable = new Runnable(){

@Override

public void run() {

new LayoutExample().displayGUI();

}

};

EventQueue.invokeLater(runnable);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值