M BUZZ CRAZE NEWS
// general

Java Swing JFrame doesn't work in Ubuntu

By David Jones

I wrote the following program in Java:

import javax.swing.*;
import java.awt.*;
public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Test"); frame.setSize(new Dimension(800, 600)); frame.setVisible(true); }
}

However when I run it the icon shows up in the bar on the left, but no actual window is displayed. I use the openjdk-8 at the moment, installed via apt-get install openjdk-8-jdk. I also tried this with Oracle Java and it didn't work either.

How can I get my code to display a Frame?

3 Answers

Java should work everywhere. Try to download a simple Java application from Internet too see if it works or not.

I think you need to set the position of your JFrame. Try
frame.setLocation(x, y);

This run successfully on my ubuntu 20 with jdk-11.java

This answer on Stack Overflow by mapadj

fixed mine:

If you set setResizable false before setting the bounds, you will not have the problem. as Gergely Szilagyi stated before, you are getting rid of the scrollbars, but the size of the window is locked and therefore you end up with 9 or 10 pixels of extra space in the frame. I just had the same problem. thanks for the help.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy