简体   繁体   English

程序编译但在 Mac 上不显示 GUI

[英]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.我尝试过不同的 IDE——IntelliJ、VS Code 和 Net Bean——尽管我认为这不是问题所在。
  2. I tried making the constructor TFDemo public.我尝试将构造函数TFDemo公开。
  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() {
            }
        });
    }
}

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并在 Mac 上运行

应用程序屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM