0

I wrote a program in eclipse using JFrame to display a GUI that will be run on a separate PC. In order to fit the screen I used Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); and setSize((int)screenSize.getWidth(), (int)screenSize.getHeight());, but for some reason it is too big when I run the JAR file on the other PC. What would be the best way to make it fit whatever screen the JAR file is being run on?

Edit: I have added a sample code of my issue, and have commented out the 4 "options" that I have tried without success.

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

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • How many monitors on other pc? https://stackoverflow.com/questions/3680221/how-can-i-get-screen-resolution-in-java – Alan Hay Jul 16 '19 at 13:25
  • There will eventually be 4 monitors. I will throw the input Frames onto the other 3, and the background display will maintain on the main monitor. At the moment, there is only one monitor. – Dawson Naccarato Jul 16 '19 at 13:27
  • Do you use somewhere pack()? https://stackoverflow.com/questions/6777135/java-jframe-size-according-to-screen-resolution – poisn Jul 16 '19 at 13:28
  • No, I don't use pack() anywhere – Dawson Naccarato Jul 16 '19 at 13:29
  • @Alan I just tried the GraphicsEnvironment link you sent, and it yielded the same results... – Dawson Naccarato Jul 16 '19 at 13:37
  • show the code.. – gpasch Jul 16 '19 at 14:23
  • Instead of examining the screen size, maximize your window with `setExtendedState(Frame.MAXIMIZED_BOTH)`. – VGR Jul 16 '19 at 14:51
  • @VGR this produced the same results as well. – Dawson Naccarato Jul 16 '19 at 15:43
  • @gpasch I could post the code, but it involves hundreds of lines over multiple classes. – Dawson Naccarato Jul 16 '19 at 15:46
  • We don’t need hundreds of lines. We don’t need to see how you’re getting your data. We only need a [mre] that builds your user interface. I can’t tell if you want to fit the blue and black background on the screen, or the three windows with the Bell Nursery logo, or both. – VGR Jul 16 '19 at 16:26
  • @VGR Thank you for the link, I followed the instructions and updated my original question. – Dawson Naccarato Jul 16 '19 at 17:50
  • `setLayout(null);` ← That is the cause of your problem. Use a LayoutManager. This is exactly why LayoutManagers exist: to handle different desktop environments, including different screen sizes, different monitor dot pitches, different fonts, and different host system look-and-feel preferences. – VGR Jul 16 '19 at 17:56
  • Just as a debugging step, and because few people have the same setup (with 4 minitors) that could allow them to reproduce the issue: You could `System.out.println` the sizes that are reported by the different options on the source- and target PCs. Also try and print the results from the code snippet at the top of https://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsDevice.html - there may be **multiple** screen devices as of https://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsEnvironment.html#getScreenDevices-- , and `.getDefaultScreenDevice` may return the wrong one for you. – Marco13 Jul 16 '19 at 18:15

1 Answers1

-2

There is no other way to get the current size of a monitor. If Toolkit is not working for some reason (probably an issue in your code), then the only other way to make the frame fit your screen is to hardcode the size of the screen.