我试图显示两个JFrame,每个监视器一个,并且代码似乎输出了错误的东西。我的问题是类似于这些的边界线...
Question 1
Question 2
Question 3
除了我的工作与JFrames基本上被覆盖并显示空白有关。下面的示例代码来自我正在测试的问题之一,以查看它是否正是我想要的东西,但是JFrames却没有显示出我想要的东西。即使应显示图标,它们还是空白。代码是空的,因为我试图弄清楚为什么JFrames可以重新调整大小,并且不按预期退出。

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() );
        }
    }
}

最佳答案

据我所知,您搞砸了您的参考文献。您不需要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) {
    }
}

10-07 16:50