【问题标题】:Executable JAR file not fitting screen可执行 JAR 文件不适合屏幕
【发布时间】: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 个“选项”。

Image run on Eclipse :

Image run on JAR file:

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


【解决方案1】:

没有其他方法可以获取显示器的当前大小。如果 Toolkit 由于某种原因无法正常工作(可能是您的代码中的问题),那么使框架适合您的屏幕的唯一另一种方法是硬编码屏幕的大小。

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多