3

SwingアプリケーションgetLocationOnScreen()から呼び出します。JFrameが-32000に最小化されている場合JFrame、-32000が返されます。

それは期待されています:

X = -32000、Y=-32000を示すコンピューター上の位置座標

ただし、ウィンドウを最小化する前にウィンドウの位置を知る必要があります。実際に最大化せずに再度最大化した場合の位置になります。最小化してもJDialog相対的に配置する必要があるからです。JFrame

考えられる解決策:イベント に追加WindowListenerしてJFramewindowIconified()座標を保存します。そして、の代わりにそれを使用します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
4

1 に答える 1

4

単に使用して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();
            }

        });
    }
}
于 2013-01-21T13:03:39.827 に答える