Java 最小化窗口的JFrame.getLocationOnScreen()
我在Swing应用程序中从Java 最小化窗口的JFrame.getLocationOnScreen(),java,swing,jframe,jdialog,Java,Swing,Jframe,Jdialog,我在Swing应用程序中从JFrame调用。如果JFrame最小化-32000,则返回-32000 预计: 但是我需要在最小化窗口之前知道它的位置,或者如果在没有实际最大化窗口的情况下再次最大化窗口,它将是一个位置。因为我需要相对于JFrame定位JDialog,即使它被最小化了 可能的解决方案: 将WindowListener添加到JFrame并在windowIconified()上保存坐标。然后用它代替getLocationOnScreen() 仅使用JFrame方法是否有更好的解决方案
JFrame
调用。如果JFrame
最小化-32000,则返回-32000
预计:
但是我需要在最小化窗口之前知道它的位置,或者如果在没有实际最大化窗口的情况下再次最大化窗口,它将是一个位置。因为我需要相对于JFrame
定位JDialog
,即使它被最小化了
可能的解决方案:
将WindowListener
添加到JFrame
并在windowIconified()上保存坐标。然后用它代替getLocationOnScreen()
仅使用JFrame
方法是否有更好的解决方案
应使用多屏幕配置,并使用以下代码
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 = topContainer.getLocationOnScreen(); //might return -32000 when minimized
if (gcBounds.contains(loc)) { //fails if -32000 is returned
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[]gs=ge.getScreenDevices();
对于(int j=0;j
只需使用getLocation()
。无论是否最小化,它都会返回适当的值:
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestJFrame {
public void initUI() {
final JFrame frame = new JFrame(TestJFrame.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.err.println(frame.getLocation());
}
}, 0, 1000, TimeUnit.MILLISECONDS);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJFrame().initUI();
}
});
}
}
对于两个或多个屏幕?或从单一显示模式:-)@mKorbel,是的,多屏幕。我已使用代码更新。我使用gcBounds.contains(loc)
如果将JFrame
最小化,它就会失败。是的,它适用于JFrame
。我认为getLocation
以前曾被使用过。但由于topContainer
是JApplet,所以我将它改为getLocationOnScreen
@NikolayKuznetsov不是applet的专家,但就我所能测试的范围而言,它确实失败了ot导致使用SwingUtilities.windowForComponent(TestApplet.this).getLocation()时出现重大问题。