如何在屏幕中心定位一个JDialog?如何在屏幕上居中放置一个JDialog?
68
A
回答
137
在Java 1.4+,你可以这样做:
final JDialog d = new JDialog();
d.setSize(200,200);
d.setLocationRelativeTo(null);
d.setVisible(true);
或许(预1.4):
final JDialog d = new JDialog();
d.setSize(200, 200);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - d.getWidth())/2;
final int y = (screenSize.height - d.getHeight())/2;
d.setLocation(x, y);
d.setVisible(true);
3
AFAIK你可以传递一个GraphicEnvironment每个的JDialog/JFrame中/的JWindow构造。该对象描述要使用的显示器。
5
这里是我的解决方案来检索多个显示器的屏幕尺寸。
import java.awt.*;
import javax.swing.JFrame;
/**
* Méthodes statiques pour récupérer les informations d'un écran.
*
* @author Jean-Claude Stritt
* @version 1.0/24.2.2009
*/
public class ScreenInfo {
/**
* Permet de récupérer le numéro de l'écran par rapport à la fenêtre affichée.
* @return le numéro 1, 2, ... (ID) de l'écran
*/
public static int getScreenID(JFrame jf) {
int scrID = 1;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
for (int i = 0; i < gd.length; i++) {
GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
Rectangle r = gc.getBounds();
if (r.contains(jf.getLocation())) {
scrID = i+1;
}
}
return scrID;
}
/**
* Permet de récupérer la dimension (largeur, hauteur) en px d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la dimension (largeur, hauteur) en pixels de l'écran spécifié
*/
public static Dimension getScreenDimension(int scrID) {
Dimension d = new Dimension(0, 0);
if (scrID > 0) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
DisplayMode mode = ge.getScreenDevices()[scrID - 1].getDisplayMode();
d.setSize(mode.getWidth(), mode.getHeight());
}
return d;
}
/**
* Permet de récupérer la largeur en pixels d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la largeur en px de l'écran spécifié
*/
public static int getScreenWidth(int scrID) {
Dimension d = getScreenDimension(scrID);
return d.width;
}
/**
* Permet de récupérer la hauteur en pixels d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la hauteur en px de l'écran spécifié
*/
public static int getScreenHeight(int scrID) {
Dimension d = getScreenDimension(scrID);
return d.height;
}
}
6
两个助手用于在屏幕内或母体内进行居中。
// Center on screen (absolute true/false (exact center or 25% upper left))
public void centerOnScreen(final Component c, final boolean absolute) {
final int width = c.getWidth();
final int height = c.getHeight();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width/2) - (width/2);
int y = (screenSize.height/2) - (height/2);
if (!absolute) {
x /= 2;
y /= 2;
}
c.setLocation(x, y);
}
// Center on parent (absolute true/false (exact center or 25% upper left))
public void centerOnParent(final Window child, final boolean absolute) {
child.pack();
boolean useChildsOwner = child.getOwner() != null ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog)) : false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize ;
final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0,0) ;
final Dimension childSize = child.getSize();
childSize.width = Math.min(childSize.width, screenSize.width);
childSize.height = Math.min(childSize.height, screenSize.height);
child.setSize(childSize);
int x;
int y;
if ((child.getOwner() != null) && child.getOwner().isShowing()) {
x = (parentSize.width - childSize.width)/2;
y = (parentSize.height - childSize.height)/2;
x += parentLocationOnScreen.x;
y += parentLocationOnScreen.y;
} else {
x = (screenSize.width - childSize.width)/2;
y = (screenSize.height - childSize.height)/2;
}
if (!absolute) {
x /= 2;
y /= 2;
}
child.setLocation(x, y);
}
5
使用此行pack()
方法之后:
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - getHeight()/2);
相关问题
- 1. 使用SKCameraNode在屏幕上居中放置一个节点[Swift]
- 2. 在图形屏幕上居中放置一个DSL形状
- 3. 在屏幕上完美地居中放置一个按钮
- 4. 在屏幕上放置一个元素
- 5. 在屏幕上居中放置不同的图像
- 6. 将xul对话框居中放置在屏幕上
- 7. 如何在所有屏幕尺寸的网页上居中放置图像
- 8. 如何使用水平字段在屏幕上居中放置标签
- 9. 如何在Qt Symbian屏幕中心放置一个图标?
- 10. 如何在一个屏幕上播放两个视频?
- 11. 在屏幕上居中显示面板
- 12. 在屏幕上居中照相馆
- 13. 在屏幕上居中显示一个弹出窗口?
- 14. 如何在谷歌地图上的屏幕中心放置一个标记
- 15. 如何在另一个中放置一个居中的圆?
- 16. 如何在屏幕上的任何位置放置视图?
- 17. 如何在React Native的屏幕底部放置一个TextInput?
- 18. CSS布局放置一个div屏幕
- 19. 如何在中央屏幕上放置表格?
- 20. visual studio 2012在屏幕左上角放置一个小数字
- 21. 在CSS中居中放置一个框
- 22. 在ListBox.ItemTemplate中居中放置一个TextBlock
- 23. 在屏幕上放置窗口的类
- 24. 如何只在一个全景页面(屏幕)上放置按钮?
- 25. 如何将loginStatus登录屏幕居中
- 26. 如何将标签居中在屏幕上?
- 27. 如何在屏幕上居中WPF应用程序?
- 28. 如何在屏幕中间放置2个按钮
- 29. 如何在屏幕上放置一个Admob广告底部中心?(使用java代码放置)
- 30. 如何在一条线上垂直居中放置图像?
什么,当你setlocation相对于空究竟是怎么回事? – 2009-08-19 13:30:14
想补充一点,你确实需要使用setSize(),否则setLocationRelativeTo()将不起作用。我不得不同时使用setSize()和setPreferredSize()以使一切看起来都正确。 – Richard 2011-06-16 13:35:39