Java多屏显示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haoranhaoshi/article/details/81359620

Java实现多屏显示:不同或相同的窗口运行在不同的屏幕上。
参考:https://stackoverflow.com/questions/4627553/show-jframe-in-a-specific-screen-in-dual-monitor-configuration
代码:
package application;
    
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.Window;

import javax.swing.JFrame;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;


public class Main extends Application {
    Button btn;
    @Override
    public void start(Stage primaryStage) {
        try {
            GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[]gs=ge.getScreenDevices();
            GraphicsDevice gd=gs[0];
            Robot robot=new Robot(gd);            
            int i=gs.length;
            btn=new Button(""+i);
            
            StackPane root = new StackPane();
            root.getChildren().add(btn);
            
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
     public static void showOnScreen2(int screen, JFrame frame)
        {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] gd = ge.getScreenDevices();
            if (screen > -1 && screen < gd.length)
            {
                frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY());
            } else if (gd.length > 0)
            {
                frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY());
            } else
            {
                throw new RuntimeException("No Screens Found");
            }
        }
    public static void main(String[] args) {
        launch(args);
        JFrame jf = new JFrame();
        jf.setSize(400, 400);
        jf.setDefaultCloseOperation(3);
        jf.setVisible(true);
        showOnScreen2(1, jf);
        JFrame jf2 = new JFrame();
        jf2.setSize(400, 400);
        jf2.setDefaultCloseOperation(3);
        jf2.setVisible(true);
        showOnScreen2(0, jf2);
    }
}
 

猜你喜欢

转载自blog.csdn.net/haoranhaoshi/article/details/81359620