【问题标题】:Program compiles but does not display GUI on Mac程序编译但在 Mac 上不显示 GUI
【发布时间】:2019-11-17 14:58:21
【问题描述】:

到目前为止:

  1. 我尝试了不同的 IDE——IntelliJ、VS Code 和 Net Bean——尽管我认为这不是问题所在。
  2. 我尝试将构造函数 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() {
            }
        });
    }
}

【问题讨论】:

  • // Gives the frame an initial size 如果读取为// Gives the frame a totally guessed, and probably wrong(1) size 会更准确。在这种情况下,将 EmptyBorder 添加到 JLabel 并添加后,pack() JFrame。然后框架将是显示标签和边框所需的最小尺寸。 1) 此外,根据每个操作系统中使用的“框架装饰”,它的大小会有所不同。

标签: java swing user-interface


【解决方案1】:

你没有在评论区创建框架!

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 上运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2014-11-12
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多