要使用到的函数有:
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