這讓我想起了我的一個非常喜歡的職位,採用Window.setLocationByPlatform(true),在計算器上。
How to best position Swing GUIs
編輯1:
您可以添加FocusListener
您JDialog
和focusGained(...)
方法,你可以使用setLocationRelativeTo(null)
同時爲JFrame
和JDialog
,讓他們都來了不管他們在哪裏,屏幕的中心。
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 1/14/13
* Time: 7:34 PM
* To change this template use File | Settings | File Templates.
*/
public class FrameFocus
{
private JFrame mainwindow;
private CustomDialog customDialog;
private void displayGUI()
{
mainwindow = new JFrame("Frame Focus Window Example");
customDialog = new CustomDialog(mainwindow, "Modal Dialog", true);
mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
JButton mainButton = new JButton(
"Click me to open a MODAL Dialog");
mainButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!customDialog.isShowing())
customDialog.setVisible(true);
}
});
contentPane.add(mainButton);
mainwindow.setContentPane(contentPane);
mainwindow.pack();
mainwindow.setLocationByPlatform(true);
mainwindow.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new FrameFocus().displayGUI();
}
});
}
}
class CustomDialog extends JDialog
{
private JFrame mainWindow;
public CustomDialog(JFrame owner, String title, boolean modal)
{
super(owner, title, modal);
mainWindow = owner;
JPanel contentPane = new JPanel();
JLabel dialogLabel = new JLabel(
"I am a Label on JDialog.", JLabel.CENTER);
contentPane.add(dialogLabel);
setContentPane(contentPane);
pack();
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
mainWindow.setLocationRelativeTo(null);
setLocationRelativeTo(null);
}
@Override
public void focusLost(FocusEvent e) {
/*
* Nothing written for this part yet
*/
}
});
}
}
編輯2:
我搜索了一下這裏和那裏,而且事實證明,在我看來,這實際上是在哪個Monitor Screen
您的應用程序來的第一個實例,將決定它的GraphicsConfiguration 。但正如我通過API漫遊,僅存在用於所述GraphicsConfiguration
啄並且對於相同沒有setter方法(靜止可以通過任何頂層窗口即JFrame(...)/JDialog(...)的構造函數指定一個)的吸氣劑的方法。
現在,您可以使用此代碼佔位您的頭部,該代碼可以用來確定合適的位置,您想要設置的位置,您可能必須使用focusGain()
方法來滿足您的問題的條件2 。看看連接的代碼,雖然沒有必要建立一個new JFrame/JDialog
,只是看如何獲取座標對於屏幕(您可以在focusGain()
方法添加到確定整個應用程序的位置。)
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
JFrame f = new
JFrame(gs[j].getDefaultConfiguration());
Canvas c = new Canvas(gc[i]);
Rectangle gcBounds = gc[i].getBounds();
int xoffs = gcBounds.x;
int yoffs = gcBounds.y;
f.getContentPane().add(c);
f.setLocation((i*50)+xoffs, (i*60)+yoffs);
f.show();
}
}
編輯3:
試圖改變這一點:
int x = loc.getX() + (mainWindow.getWidth() - getWidth())/2;
int y = loc.getY() + (mainWindow.getHeight() - getHeight())/2;
setLocation(x, y);
只是:
setLocationRelativeTo(mainWindow);
要測試上述事情,我使用了我的FrameFocus
類,儘管我已將您的更改添加到我的CustomDialog
方法中,如此修改的CustomDialog
類所示。
class CustomDialog extends JDialog
{
private JFrame mainWindow;
public CustomDialog(JFrame owner, String title, boolean modal)
{
super(owner, title, modal);
mainWindow = owner;
JPanel contentPane = new JPanel();
JLabel dialogLabel = new JLabel(
"I am a Label on JDialog.", JLabel.CENTER);
contentPane.add(dialogLabel);
setContentPane(contentPane);
pack();
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
//mainWindow.setLocationRelativeTo(null);
//setLocationRelativeTo(null);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc = gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
Rectangle gcBounds = gc[i].getBounds();
Point loc = mainWindow.getLocationOnScreen();
if (gcBounds.contains(loc)) {
System.out.println("at " + j + " screen");
int x = gcBounds.x + (gcBounds.width - mainWindow.getWidth())/2;
int y = gcBounds.y + (gcBounds.height - mainWindow.getHeight())/2;
mainWindow.setLocation(x, y);
//x = (int) (loc.getX() + (mainWindow.getWidth() - CustomDialog.this.getWidth())/2);
//y = (int) (loc.getY() + (mainWindow.getHeight() - CustomDialog.this.getHeight())/2);
//CustomDialog.this.setLocation(x, y);
CustomDialog.this.setLocationRelativeTo(mainWindow);
break;
}
}
}
}
@Override
public void focusLost(FocusEvent e) {
/*
* Nothing written for this part yet
*/
}
});
}
}
回報一些JComponent的 – mKorbel
@mKorbel點,對不起,不明白這一點 –
[見這裏](http://stackoverflow.com/a/10283669/714968):-),取消'// dialog.setLocation(x,y);'並且註釋'dialog.setLocationRelativeTo(frame);','dialog.setVisible(true);'必須在'invokeLater'裏面 – mKorbel