Carefulle
Carefulle

Reputation: 11

Program compiles but does not display GUI on Mac

So far:

  1. I've tried different IDEs-IntelliJ, VS Code, and Net Bean-although I didn't think this was the issue.
  2. I tried making the constructor TFDemo public.
  3. I tried running via terminal after compiling.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingDemo {

    SwingDemo() {

        // Create a new JFrame container
        JFrame jfrm = new JFrame("A Simple Swing Application");

        // Gives the frame an initial size
        jfrm.setSize(275, 100);

        // Terminate the program when the user closes the application
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a text-based label
        JLabel jlab = new JLabel(" GUI programming with Swing.");

        // Add the label to the content pane
        jfrm.add(jlab);

        // Display the frame
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        // Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
           @Override
            public void run() {
            }
        });
    }
}

Upvotes: 0

Views: 96

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201409

You didn't create the frame at the commented block!

public static void main(String[] args) {
    // Create the frame on the event dispatching thread
    SwingUtilities.invokeLater(new Runnable() {
       @Override
        public void run() {
           new SwingDemo();
        }
    });
}

Also, here it would seem a good idea to let your program exit when you close the primary frame... so add something like,

// Add the label to the content pane
jfrm.add(jlab);

// Exit on close
jfrm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

// Display the frame
jfrm.setVisible(true);

And running on a mac

Application Screen Shot

Upvotes: 1

Related Questions