Dimension screen=Toolkit.getDefaultToolkit.getScreenSize();//获取屏幕尺寸对象

本文介绍了如何在Java Swing中使JFrame窗口居中显示的方法,包括使用setLocationRelativeTo(null)快速实现窗口居中,以及通过计算屏幕分辨率和窗口大小来精确定位窗口位置的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以:如下
假如你有个JFrame 对象 f;

f.setLocationRelativeTo(null);//f启动后就在屏幕正中央了
--------------------------
屏幕分辨率求法:
int w = f.getToolkit().getScreenSize().width;//宽度
int h = f.getToolkit().getScreenSize().height;//高度


也可以

 

Dimension screen=Toolkit.getDefaultToolkit.getScreenSize();//获取屏幕尺寸对象

Dimension myframe=this.getSize();//获取当前窗体的尺寸对象

int w=(screen.width-myframe.width)/2;//水平位置
int h=(screen.height-myframe.width)/2);//垂直位置
楼主能看懂什么意思吧,就是屏幕宽度(高度)减去窗口宽度(高度),然后在除以2,就是居中了

setLocation(w,h);

以上方法绝对行,这是我在做GUI设计的时候用到的代码!


package useless; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class UselessButFunApp extends JFrame { private JButton targetButton; private JLabel scoreLabel; private JLabel timeLabel; private Timer timer; private int score = 0; private int timeLeft = 30; private Random random = new Random(); private Dimension screenSize; public UselessButFunApp() { setTitle("随机颜色按钮挑战"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); setLayout(null); // 获取屏幕尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 创建分数标签 scoreLabel = new JLabel("分数: 0"); scoreLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); scoreLabel.setBounds(20, 20, 200, 30); add(scoreLabel); // 创建时间标签 timeLabel = new JLabel("剩余时间: 30秒"); timeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); timeLabel.setBounds(20, 60, 200, 30); add(timeLabel); // 创建目标按钮 targetButton = new JButton("点我!"); targetButton.setFont(new Font("微软雅黑", Font.BOLD, 20)); targetButton.setBounds(300, 250, 100, 60); targetButton.setOpaque(true); targetButton.setBorderPainted(false); targetButton.addActionListener(e -> { score++; scoreLabel.setText("分数: " + score); moveButton(); }); add(targetButton); // 设置初始按钮位置和颜色 moveButton(); // 创建计时器 timer = new Timer(1000, e -> { timeLeft--; timeLabel.setText("剩余时间: " + timeLeft + "秒"); if (timeLeft <= 0) { timer.stop(); targetButton.setEnabled(false); int option = JOptionPane.showConfirmDialog( this, "时间到!你的最终得分: " + score + "\n再玩一次?", "游戏结束", JOptionPane.YES_NO_OPTION ); if (option == JOptionPane.YES_OPTION) { restartGame(); } else { System.exit(0); } } }); // 添加开始按钮 JButton startButton = new JButton("开始游戏"); startButton.setFont(new Font("微软雅黑", Font.PLAIN, 18)); startButton.setBounds(320, 500, 150, 40); startButton.addActionListener(e -> { startButton.setVisible(false); timer.start(); }); add(startButton); setLocationRelativeTo(null); setVisible(true); } private void moveButton() { // 随机位置 int buttonWidth = targetButton.getWidth(); int buttonHeight = targetButton.getHeight(); int x = random.nextInt(getWidth() - buttonWidth - 20) + 10; int y = random.nextInt(getHeight() - buttonHeight - 80) + 70; // 随机颜色 Color buttonColor = new Color( random.nextInt(256), random.nextInt(256), random.nextInt(256) ); // 设置按钮位置和颜色 targetButton.setLocation(x, y); targetButton.setBackground(buttonColor); // 根据背景色调整文字颜色 int brightness = (buttonColor.getRed() * 299 + buttonColor.getGreen() * 587 + buttonColor.getBlue() * 114) / 1000; targetButton.setForeground(brightness > 128 ? Color.BLACK : Color.WHITE); } private void restartGame() { score = 0; timeLeft = 30; scoreLabel.setText("分数: 0"); timeLabel.setText("剩余时间: 30秒"); targetButton.setEnabled(true); moveButton(); timer.start(); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new UselessButFunApp()); } }纠错
最新发布
06-10
java这段代码private void CreateMainWindow(){ this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); WindowBar = new JMenuBar(); this.setJMenuBar(WindowBar); this.setTitle("停车场管理系统"); int WindowHeight = 300; int WindowWidth = 400; Dimension Screen = Toolkit.getDefaultToolkit().getScreenSize(); int ScreenWidth = Screen.width; int ScreenHeight = Screen.height; this.setResizable(false); this.setLayout(null); this.setBounds((ScreenWidth-WindowWidth)/2,(ScreenHeight-WindowHeight)/2,WindowWidth,WindowHeight); jLabelTitle = new JLabel(); getContentPane().add(jLabelTitle); jLabelTitle.setText("欢迎进入"); jLabelTitle.setBounds(130, 50, 141, 30); //jLabelTitle.setLocation(100, 20); jLabelTitle.setFont(new java.awt.Font("宋体",1,20)); jLabelTitle.setHorizontalAlignment(SwingConstants.CENTER); jLabel = new JLabel(); getContentPane().add(jLabel); getContentPane().add(jLabelTitle); jLabel.setText("停车场信息管理系统"); jLabel.setBounds(50, 100, 300, 30); jLabel.setFont(new java.awt.Font("宋体",1,30)); jLabel.setHorizontalAlignment(SwingConstants.CENTER); notMenu = new JMenu("管理模式"); UserMenu = new JMenu("信息管理"); TypeMenu = new JMenu("车位管理"); WindowBar.add(UserMenu); WindowBar.add(notMenu); WindowBar.add(TypeMenu); JMenuItem AddItem ,DeleteItem,ChangeItem,QueryItem; AddItem = new JMenuItem("添加"); DeleteItem = new JMenuItem("删除"); ChangeItem = new JMenuItem("修改"); QueryItem = new JMenuItem("查询"); notMenu.add(AddItem); notMenu.add(DeleteItem); notMenu.add(ChangeItem); notMenu.add(QueryItem); AddItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //MainWindow.this.setVisible(false); AddFrame add = new AddFrame(); add.setVisible(true); } }); QueryItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //MainWindow.this.setVisible(false); QueryFrame query = new QueryFrame(); query.setVisible(true); } }); }怎么设置点击按钮打开另一个窗口,该如何修改
02-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值