Swing007——绝对布局

一、API简介

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二、实例

2.1、分开设置按钮位置和大小

package com.absulote;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public interface TestNullLayout {
	public static void main(String[] args) {
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame("绝对布局");
		// 设置大小
		jFrame.setSize(500, 400);
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建一个中间容器
		JPanel jPanel = new JPanel(null);

		// 创建按钮
		JButton jButton1 = new JButton("按钮1");
		// 设置位置
		jButton1.setLocation(100, 100);
		// 设置按钮大小
		jButton1.setSize(80, 35);
		// 添加到指定位置
		jPanel.add(jButton1);

		// 将中间容器添加到顶层容器中
		jFrame.setContentPane(jPanel);
		// 显示窗口
		jFrame.setVisible(true);

	}

}

在这里插入图片描述

2.2、同时设置按钮位置和大小

package com.absulote;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public interface TestNullLayout {
	public static void main(String[] args) {
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame("绝对布局");
		// 设置大小
		jFrame.setSize(500, 400);
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建一个中间容器
		JPanel jPanel = new JPanel(null);

		// 创建按钮
		JButton jButton1 = new JButton("按钮1");
		// 设置位置
		jButton1.setLocation(100, 100);
		// 设置按钮大小
		jButton1.setSize(80, 35);
		// 添加到指定位置
		jPanel.add(jButton1);

		// 创建按钮
		JButton jButton2 = new JButton("按钮2");
		// 设置位置和按钮大小
		jButton2.setBounds(200, 200, 120, 45);
		// 添加到指定位置
		jPanel.add(jButton2);

		// 将中间容器添加到顶层容器中
		jFrame.setContentPane(jPanel);
		// 显示窗口
		jFrame.setVisible(true);

	}

}

在这里插入图片描述

2.3、设置按钮位置不随窗口大小变换而变换位置

package com.absulote;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public interface TestNullLayout {
	public static void main(String[] args) {
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame("绝对布局");
		// 设置大小
		jFrame.setSize(500, 400);
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建一个中间容器
		JPanel jPanel = new JPanel(null);

		// 创建按钮1
		JButton jButton1 = new JButton("按钮1");
		// 设置位置
		jButton1.setLocation(100, 100);
		// 设置按钮大小
		jButton1.setSize(80, 35);
		// 添加中间容器
		jPanel.add(jButton1);

		// 创建按钮2
		JButton jButton2 = new JButton("按钮2");
		// 设置位置和按钮大小
		jButton2.setBounds(200, 200, 120, 45);
		// 添加中间容器
		jPanel.add(jButton2);

		// 将中间容器添加到顶层容器中
		jFrame.setContentPane(jPanel);
		// 显示窗口
		jFrame.setVisible(true);

		// jFrame.setVisible(true);要放在前面,因为只有将面板添加到窗口才知道宽高
		// 创建按钮3
		JButton jButton3 = new JButton("按钮3");
		// 设置位置和按钮大小
		jButton3.setBounds(jPanel.getWidth() - 100, jPanel.getHeight() - 50, 120, 45);
		// 添加中间容器
		jPanel.add(jButton3);

	}

}

在这里插入图片描述
修改窗口大小后
在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值