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

标签 java swing user-interface

到目前为止:

  1. 我尝试过不同的 IDE(IntelliJ、VS Code 和 Net Bean),但我认为这不是问题所在。
  2. 我尝试将构造函数设为公共(public)TFDemo
  3. 我尝试编译后通过终端运行。
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() {
            }
        });
    }
}

最佳答案

您没有在评论 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();
        }
    });
}

此外,在这里,当您关闭主框架时让程序退出似乎是个好主意......所以添加类似的内容,

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

并在 Mac 上运行

Application Screen Shot

关于java - 程序编译但在 Mac 上不显示 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58894255/

相关文章:

java - MigLayout JTextArea 在与 linewrap=true 一起使用时不会缩小

user-interface - 人因设计(UI设计中满足心理需求)

java - 如何通过输入用户输入使用java构建二维数组?

java - 从更新的行中获取值

java - 检查是什么触发了 WindowClosing 事件

java - 使用 url 在 JFrame 中映射

java - 切换 Jlabel 和 JPanel 的可见性

user-interface - MATLAB gui创建者GUIDE是否像我想的那样糟糕?还有其他选择吗?

java - 我在函数中遇到空指针异常,不知道如何修复它

java - 为什么我在 Android 中需要 super() ?