到目前为止:
- 我尝试过不同的 IDE(IntelliJ、VS Code 和 Net Bean),但我认为这不是问题所在。
- 我尝试将构造函数设为公共(public)
TFDemo
。 - 我尝试编译后通过终端运行。
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 上运行
关于java - 程序编译但在 Mac 上不显示 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58894255/