java窗口在屏幕中间
时间: 2023-05-15 20:04:41 浏览: 92
可以使用以下代码将Java窗口置于屏幕中央:
```java
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int windowWidth = 500; // 窗口宽度
int windowHeight = 300; // 窗口高度
int x = (screenWidth - windowWidth) / 2;
int y = (screenHeight - windowHeight) / 2;
JFrame frame = new JFrame("窗口标题");
frame.setSize(windowWidth, windowHeight);
frame.setLocation(x, y);
frame.setVisible(true);
```
这段代码会创建一个宽度为500,高度为300的窗口,并将其置于屏幕中央。
相关问题
java的gui可以做到弹出在屏幕最中间吗?
是的,Java的GUI可以通过设置窗口的位置来实现弹出在屏幕最中间的效果。可以使用以下代码实现:
```
// 获取屏幕的宽度和高度
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
// 获取窗口的宽度和高度
int frameWidth = frame.getSize().width;
int frameHeight = frame.getSize().height;
// 设置窗口的位置
int x = (screenWidth - frameWidth) / 2;
int y = (screenHeight - frameHeight) / 2;
frame.setLocation(x, y);
```
其中,`frame`是你要弹出的窗口对象。这段代码会将窗口设置在屏幕的中央位置。
java swing 界面显示 电脑中间 代码
以下是将Java Swing界面显示在电脑屏幕中间的代码:
```java
import javax.swing.*;
public class CenteredFrame extends JFrame {
public CenteredFrame() {
setTitle("Centered Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null); //将窗口显示在屏幕中间
setVisible(true);
}
public static void main(String[] args) {
new CenteredFrame();
}
}
```
在这个例子中,我们创建了一个继承自JFrame的类CenteredFrame,并在构造函数中设置了窗口的标题、关闭操作、大小和位置。通过调用`setLocationRelativeTo(null)`方法,我们将窗口显示在屏幕中间。最后,我们通过调用`setVisible(true)`方法显示窗口。在main方法中,我们创建CenteredFrame对象并显示它。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)