双监视器系统中的JFrame

时间:2012-03-01 04:41:59

标签: java swing jframe multiple-monitors

我正在尝试显示两个JFrame,每个监视器一个,代码似乎输出错误的东西。我的问题是与这些类似的边界线...
Question 1
Question 2
Question 3
除了我的JFrames基本上被覆盖并显示空白。下面的示例代码来自我正在测试的一个问题,看它是否是我想要的,但JFrames显示的并不像我想要的那样。它们是空白的,即使它们应该显示图标。代码是裸露的,因为我试图弄清楚为什么JFrame变得可以重新调整大小,并且不会像它们应该的那样退出。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Dual extends JFrame implements ActionListener, ChangeListener {
    public Dual(String one) {
        JFrame frame = new JFrame(one);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public static void showOnScreen(int screen, JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        if( screen > -1 && screen < gs.length ) {
            gs[screen].setFullScreenWindow(frame);
        } else if( gs.length > 0 ) {
            gs[0].setFullScreenWindow(frame);
        } else {
            throw new RuntimeException( "No Screens Found" );
        }
    }

    public static void main(String[] args) {
        showOnScreen(1, new Dual("Test"));
        showOnScreen(2, new Dual("1...2"));
        GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        for ( int i = 0; i < devices.length; i++ ) {
            System.out.println( "Device " + i + " width: " + devices[ i ].getDisplayMode().getWidth() );
            System.out.println( "Device " + i + " height: " + devices[ i ].getDisplayMode().getHeight() );
        }       
    }
}

1 个答案:

答案 0 :(得分:0)

尽我所知,你搞砸了你的参考资料。你不需要一个JFrame,你实际上需要一个Window。

查看http://www.exampledepot.com/egs/java.awt/screen_FullWin.html以获取示例。我用这个来修改你的代码来制作一个工作模型,结果看起来像是:

public class Dual extends Window implements ActionListener, ChangeListener {

    private int screen;

    public Dual(String one) {

        super(new Frame());

        setLayout(new FlowLayout());

        JButton button = new JButton(one);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice[] gs = ge.getScreenDevices();
                if (screen > -1 && screen < gs.length) {
                    gs[screen].setFullScreenWindow(null);

                }

                dispose();
                System.exit(0);

            }
        });

        add(button);

    }

    public void setScreen(int screen) {

        this.screen = screen;

    }

    public static void showOnScreen(int screen, Dual frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        if (screen > -1 && screen < gs.length) {
            frame.setScreen(screen);
            gs[screen].setFullScreenWindow(frame);
            frame.validate();
        } else if (gs.length > 0) {
            gs[0].setFullScreenWindow(frame);
        } else {
            throw new RuntimeException("No Screens Found");
        }
    }

    public static void main(String[] args) {
        showOnScreen(1, new Dual("Test"));
    showOnScreen(2, new Dual("1...2"));
//        GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
//        for ( int i = 0; i < devices.length; i++ ) {
//            System.out.println( "Device " + i + " width: " + devices[ i ].getDisplayMode().getWidth() );
//            System.out.println( "Device " + i + " height: " + devices[ i ].getDisplayMode().getHeight() );
//        }       
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    @Override
    public void stateChanged(ChangeEvent e) {
    }
}