Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最小化窗口的JFrame.getLocationOnScreen()_Java_Swing_Jframe_Jdialog - Fatal编程技术网

Java 最小化窗口的JFrame.getLocationOnScreen()

Java 最小化窗口的JFrame.getLocationOnScreen(),java,swing,jframe,jdialog,Java,Swing,Jframe,Jdialog,我在Swing应用程序中从JFrame调用。如果JFrame最小化-32000,则返回-32000 预计: 但是我需要在最小化窗口之前知道它的位置,或者如果在没有实际最大化窗口的情况下再次最大化窗口,它将是一个位置。因为我需要相对于JFrame定位JDialog,即使它被最小化了 可能的解决方案: 将WindowListener添加到JFrame并在windowIconified()上保存坐标。然后用它代替getLocationOnScreen() 仅使用JFrame方法是否有更好的解决方案

我在Swing应用程序中从
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()时出现重大问题。