简体   繁体   中英

JFrame in Dual Monitor System

I am trying to display two JFrames, one on each monitor, and the code seems to output the wrong stuff. My question is border line similar to these...
Question 1
Question 2
Question 3
Except mine has to do with the JFrames basically being overridden and showing up blank. Sample code below from one of the questions that I was testing to see if it is what I wanted, but the JFrames show up nothing like I want. They are blank, even though they are supposed to display Icons. The code is bare, as I am trying to figure out why the JFrames become re-sizable, and don't exit like they are supposed to.

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

As best as I can tell, you messing up your references. You don't need a JFrame, you actually need a Window.

Check out http://www.exampledepot.com/egs/java.awt/screen_FullWin.html for an example. I used this to modify your code to make a working model, which ended up looking something like:

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) {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM