在JAVA的图形界面中,怎样讲界面居中显示?
1个回答
展开全部
取出Screen的大小,和本身界面的大小,
算出界面的左上角坐标即可
示例如下:
import javax.swing.*;
import java.awt.*;
public class FrameCentreTest extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("centre");
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(400,400);
Dimension framesize = frame.getSize();
int x = (int)screensize.getWidth()/2 - (int)framesize.getWidth()/2;
int y = (int)screensize.getHeight()/2 - (int)framesize.getHeight()/2;
frame.setLocation(x,y);
frame.setVisible(true);
}
}
算出界面的左上角坐标即可
示例如下:
import javax.swing.*;
import java.awt.*;
public class FrameCentreTest extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("centre");
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(400,400);
Dimension framesize = frame.getSize();
int x = (int)screensize.getWidth()/2 - (int)framesize.getWidth()/2;
int y = (int)screensize.getHeight()/2 - (int)framesize.getHeight()/2;
frame.setLocation(x,y);
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询