1

2 モニター システムで動作するように設計されたプログラムを作成しています。オブジェクトを分離JFrameする必要があり、デフォルトにすると、最初のフレーム インスタンスが開きます。次に、ユーザーはそのフレームを特定のモニターにドラッグするか、そのままにしておく必要があります。そのフレームのボタンをクリックすると、プログラムが反対側のモニターで 2 番目のフレームを開くようにします。

では、フレーム オブジェクトがオンになっているモニターを特定し、別のフレーム オブジェクトを反対側のモニターで開くように指示するにはどうすればよいでしょうか。

4

1 に答える 1

3

GraphicsEnvironment を調べると、各画面の境界と位置を簡単に見つけることができます。その後は、フレームの位置をいじるだけです。

ここで小さなデモのサンプル コードを参照してください。

import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestMultipleScreens {

    private int count = 1;

    protected void initUI() {
        Point p = null;
        for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            p = gd.getDefaultConfiguration().getBounds().getLocation();
            break;
        }
        createFrameAtLocation(p);
    }

    private void createFrameAtLocation(Point p) {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame-" + count++);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        final JButton button = new JButton("Click me to open new frame on another screen (if you have two screens!)");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                GraphicsDevice device = button.getGraphicsConfiguration().getDevice();
                Point p = null;
                for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
                    if (!device.equals(gd)) {
                        p = gd.getDefaultConfiguration().getBounds().getLocation();
                        break;
                    }
                }
                createFrameAtLocation(p);
            }
        });
        frame.add(button);
        frame.setLocation(p);
        frame.pack(); // Sets the size of the unmaximized window
        frame.setExtendedState(Frame.MAXIMIZED_BOTH); // switch to maximized window
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestMultipleScreens().initUI();
            }
        });
    }

}

ただし、複数の JFrame の使用、良い/悪い習慣を注意深く読むことを検討してください。それらは非常に興味深い考慮事項をもたらすからです。

于 2013-01-15T13:21:20.620 に答える