And*_*wan 104 java user-interface swing awt
什么是最简单的中心方式java.awt.Window,例如a JFrame或a JDialog?
And*_*wan 234
从这个链接
如果您使用的是Java 1.4或更高版本,则可以在对话框,框架或窗口上使用简单方法setLocationRelativeTo(null)来使其居中.
Dón*_*nal 64
这应该适用于所有版本的Java
public static void centreWindow(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
Run Code Online (Sandbox Code Playgroud)
Kev*_*Day 25
请注意,setLocationRelativeTo(null)和Tookit.getDefaultToolkit().getScreenSize()技术仅适用于主监视器.如果您处于多监视器环境中,则可能需要在执行此类计算之前获取有关窗口所在的特定监视器的信息.
有时重要,有时不...
有关如何获取此信息的详细信息,请参阅GraphicsEnvironment javadocs.
Pet*_*abo 16
在Linux上的代码
setLocationRelativeTo(null)
Run Code Online (Sandbox Code Playgroud)
每次我启动它时,在多显示环境中将窗口置于随机位置.和代码
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
Run Code Online (Sandbox Code Playgroud)
将窗口"切"成两半,将其放置在我的两个显示器之间的确切中心.我使用以下方法来集中它:
private void setWindowPosition(JFrame window, int screen)
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] allDevices = env.getScreenDevices();
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
if (screen < allDevices.length && screen > -1)
{
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
}
else
{
topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;
screenX = allDevices[0].getDefaultConfiguration().getBounds().width;
screenY = allDevices[0].getDefaultConfiguration().getBounds().height;
}
windowPosX = ((screenX - window.getWidth()) / 2) + topLeftX;
windowPosY = ((screenY - window.getHeight()) / 2) + topLeftY;
window.setLocation(windowPosX, windowPosY);
}
Run Code Online (Sandbox Code Playgroud)
使窗口显示在第一个显示屏的中央.这可能不是最简单的解决方案.
适用于Linux,Windows和Mac.
我终于使用 Swing GUI Forms 使这堆代码在 NetBeans 中工作,以便将主 jFrame 居中:
package my.SampleUIdemo;
import java.awt.*;
public class classSampleUIdemo extends javax.swing.JFrame {
///
public classSampleUIdemo() {
initComponents();
CenteredFrame(this); // <--- Here ya go.
}
// ...
// void main() and other public method declarations here...
/// modular approach
public void CenteredFrame(javax.swing.JFrame objFrame){
Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
int iCoordX = (objDimension.width - objFrame.getWidth()) / 2;
int iCoordY = (objDimension.height - objFrame.getHeight()) / 2;
objFrame.setLocation(iCoordX, iCoordY);
}
}
Run Code Online (Sandbox Code Playgroud)
或者
package my.SampleUIdemo;
import java.awt.*;
public class classSampleUIdemo extends javax.swing.JFrame {
///
public classSampleUIdemo() {
initComponents();
//------>> Insert your code here to center main jFrame.
Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
int iCoordX = (objDimension.width - this.getWidth()) / 2;
int iCoordY = (objDimension.height - this.getHeight()) / 2;
this.setLocation(iCoordX, iCoordY);
//------>>
}
// ...
// void main() and other public method declarations here...
}
Run Code Online (Sandbox Code Playgroud)
或者
package my.SampleUIdemo;
import java.awt.*;
public class classSampleUIdemo extends javax.swing.JFrame {
///
public classSampleUIdemo() {
initComponents();
this.setLocationRelativeTo(null); // <<--- plain and simple
}
// ...
// void main() and other public method declarations here...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178758 次 |
| 最近记录: |