如何精确的计算JFrame中菜单栏、边框及客户区(内容面板)的大小

要使用到的函数有:

JFrame类中函数中setVisible()或show()函数。

JFrame类中getInsets()函数,获得内容面板边框到JFrame边框的距离。

JPanel类中getWidth()和getHeight()函数,获得面板的宽度和高度。

注意:getInsets()、getWidth()和getHeight()函数必须放在setVisible()或show()函数之后,才能获得正确的像素值,否则获得的结果为0。

情况一:getInsets()、getWidth()和getHeight()函数放在setVisible()或show()函数前面

package a;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
	private static final long serialVersionUID = 1L;

	public Test()
	{
		this.setSize(600,600);
		this.setLocation(300,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel contentPane=new JPanel();
		this.setContentPane(contentPane);
		contentPane.setLayout(null);
		int width=contentPane.getWidth();
		int height=contentPane.getHeight();
		Insets a=this.getInsets();	
		this.setVisible(true);		
		System.out.println("菜单栏的高度为:"+a.top);
		System.out.println("JFrame左边框的宽度:"+a.left);
		System.out.println("JFrame右边框的宽度:"+a.right);
		System.out.println("JFrame下边框的宽度:"+a.bottom);
		System.out.println("面板的宽度:"+width);
		System.out.println("面板的高度:"+height);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Test();
	}
}
运行结果:

菜单栏的高度为:0
JFrame左边框的宽度:0
JFrame右边框的宽度:0
JFrame下边框的宽度:0
面板的宽度:0
面板的高度:0

情况二:getInsets()、getWidth()和getHeight()函数放在setVisible()或show()函数后面

package a;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
	private static final long serialVersionUID = 1L;

	public Test()
	{
		this.setSize(600,600);
		this.setLocation(300,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel contentPane=new JPanel();
		this.setContentPane(contentPane);
		contentPane.setLayout(null);		
		this.setVisible(true);		
		int width=contentPane.getWidth();
		int height=contentPane.getHeight();
		Insets a=this.getInsets();	
		System.out.println("菜单栏的高度为:"+a.top);
		System.out.println("JFrame左边框的宽度:"+a.left);
		System.out.println("JFrame右边框的宽度:"+a.right);
		System.out.println("JFrame下边框的宽度:"+a.bottom);
		System.out.println("面板的宽度:"+width);
		System.out.println("面板的高度:"+height);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Test();
	}
}
运行结果:

菜单栏的高度为:30
JFrame左边框的宽度:8
JFrame右边框的宽度:8
JFrame下边框的宽度:8
面板的宽度:584
面板的高度:562
注意:要获取内容面板的宽度,必须在jf.setcontentPane(contentPan)放在jf.setVisible(true)的前面。

请看如下代码:

package mysnake;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Snake extends JPanel
{
	private static final long serialVersionUID = 1L;
	private JFrame jf=new JFrame("贪吃蛇");
	private JPanel contentPan=new JPanel();
	private JPanel pan2=new JPanel();
	public Snake()
	{
		jf.setSize(600,600);
		jf.setLocation(350,300);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		jf.setVisible(true);
		jf.setContentPane(contentPan);
		contentPan.setLayout(null);	
		System.out.println(contentPan.getWidth());
		contentPan.add(this);
//		this.setBounds(5, 5, 500, 500);
		this.setBackground(Color.red);
		contentPan.add(pan2);		
	}
	public static void main(String[] args) 
	{
		new Snake();
	}
}
输出:0

package mysnake;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Snake extends JPanel
{
	private static final long serialVersionUID = 1L;
	private JFrame jf=new JFrame("贪吃蛇");
	private JPanel contentPan=new JPanel();
	private JPanel pan2=new JPanel();
	public Snake()
	{
		jf.setSize(600,600);
		jf.setLocation(350,300);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		jf.setContentPane(contentPan);
		jf.setVisible(true);		
		contentPan.setLayout(null);	
		System.out.println(contentPan.getWidth());
		contentPan.add(this);
//		this.setBounds(5, 5, 500, 500);
		this.setBackground(Color.red);
		contentPan.add(pan2);		
	}
	public static void main(String[] args) 
	{
		new Snake();
	}
}
输出:584






  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值