【发布时间】:2019-07-16 13:19:55
【问题描述】:
我在 Eclipse 中使用 JFrame 编写了一个程序来显示将在单独的 PC 上运行的 GUI。为了适应屏幕,我使用了Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 和setSize((int)screenSize.getWidth(), (int)screenSize.getHeight());,但由于某种原因,当我在另一台PC 上运行JAR 文件时它太大了。使它适合运行 JAR 文件的任何屏幕的最佳方法是什么?
编辑:我添加了我的问题的示例代码,并注释掉了我尝试过但没有成功的 4 个“选项”。
package testDisplay;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel
import javax.swing.JPanel;
public class Main {
public static void main(String []args) {
Display display = new Display();
}
}
@SuppressWarnings("serial")
public class Display extends JFrame {
/**
* Constructor for Display
*/
public Display() {
// Create Window
setTitle("Production Perfomance");
setUndecorated(true);
setVisible(true);
setLocation(new Point(0,0));
setLayout(null);
getContentPane().setBackground(Color.BLACK);
// Option 1:
//GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
//setSize((int)gd.getDisplayMode().getWidth(), (int)gd.getDisplayMode().getHeight());
// Option 2:
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//setSize((int)screenSize.getWidth(), (int)screenSize.getHeight());
// Option 3:
//setSize(MAXIMIZED_HORIZ, MAXIMIZED_VERT);
// Option 4:
//setExtendedState(MAXIMIZED_BOTH);
// Add Panel with Label
JLabel title = new JLabel("<- - - - - - - - - - - - - - - ->");
title.setFont(new Font("Arial Black", Font.BOLD, 140));
title.setForeground(new Color(53, 203, 253));
JPanel bTitle = new JPanel();
bTitle.setBackground(new Color(39, 67, 157));
title.setBounds(0, 90, getWidth(), 150);
bTitle.setBounds(0, 400, getWidth(), 300);
add(bTitle);
bTitle.add(title);
}
}
【问题讨论】:
-
其他电脑有多少显示器? stackoverflow.com/questions/3680221/…
-
最终会有 4 台显示器。我会将输入帧扔到其他 3 个上,背景显示将保持在主监视器上。目前,只有一台显示器。
-
你在某处使用 pack() 吗? stackoverflow.com/questions/6777135/…
-
不,我不在任何地方使用 pack()
-
@Alan 我刚刚尝试了您发送的 GraphicsEnvironment 链接,它产生了相同的结果...
标签: java swing jframe executable-jar screen-size