简体   繁体   English

关于Java SWING和Swing应用程序框架的一些疑问

[英]Some doubts about Java SWING and Swing Application Framework

I have this simple Main class that use swing to show an Hello World label but I have some doubt about this code because this is my first time that I create GUI in Java: 我有这个简单的Main类,它使用swing来显示Hello World标签但是我对这段代码有一些疑问,因为这是我第一次用Java创建GUI:

import javax.swing.JLabel;

import org.jdesktop.application.SingleFrameApplication;

public class Main extends SingleFrameApplication {

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        show(new JLabel("Hello World"));
    }

    public static void main(String[] args) {
        Main a = new Main();
        a.startup();
    }

}

My doubts are: 我的怀疑是:

  1. From what I have understand the JLabel() method is a pure Swing method that simply create a textual label showing the Hello World message. 根据我的理解, JLabel()方法是一个纯粹的Swing方法,只需创建一个显示Hello World消息的文本标签。 This code use also the startup() method that, from what I have understand, is a method of the **SingleFrameApplication class that belong to the so called Swing Application Framework ...but...what exactly is this Swing Application Framework ? 这段代码也使用了startup()方法,根据我的理解,这是一个属于所谓的Swing应用程序框架 的** SingleFrameApplication的方法 ......但是......这个Swing应用程序框架究竟是什么? Is it a separate project from Swing? 它是一个与Swing分开的项目吗? What give me? 什么给我?

  2. When I run the application as a classic Java application the Hello World message is show but also appear to me the following error message in the Eclipse console: 当我将应用程序作为经典Java应用程序运行时,Hello World消息显示,但在Eclipse控制台中也出现以下错误消息:

set 23, 2013 12:35:37 PM org.jdesktop.application.ResourceManager getApplicationResourceMap Avvertenza: getApplicationResourceMap(): no Application class set 23, 2013 12:35:38 PM org.jdesktop.application.SingleFrameApplication initRootPaneContainer Avvertenza: couldn't restore session [mainFrame.session.xml] java.lang.NullPointerException at org.jdesktop.application.LocalStorage.getApplicationId(LocalStorage.java:254) at org.jdesktop.application.LocalStorage.getDirectory(LocalStorage.java:274) at org.jdesktop.application.LocalStorage$LocalFileIO.getFile(LocalStorage.java:450) at org.jdesktop.application.LocalStorage$LocalFileIO.openInputFile(LocalStorage.java:417) at org.jdesktop.application.LocalStorage.openInputFile(LocalStorage.java:68) at org.jdesktop.application.LocalStorage.load(LocalStorage.java:188) at org.jdesktop.application.SessionStorage.restore(SessionStorage.java:381) at org.jdesktop.application.SingleFrameApplication.initRootPaneContainer(SingleFrameApplication.java:210) org.jdesktop.application.ResourceManager getApplicationResourceMap Avvertenza:getApplicationResourceMap():no Application class set 23,2013 12:35:38 PM org.jdesktop.application.SingleFrameApplication initRootPaneContainer Avvertenza:不能org.jdesktop.application.LocalStorage.getApplicationId(LocalStorage.java:254)中的org.jdesktop.application.LocalStorage.getDirectory(LocalStorage.java:274)中的ord还原会话[mainFrame.session.xml] java.lang.NullPointerException .jdesktop.application.LocalStorage $ LocalFileIO.getFile(LocalStorage.java:450)org.jdesktop.application.LocalStorage $ LocalFileIO.openInputFile(LocalStorage.java:417)at org.jdesktop.application.LocalStorage.openInputFile(LocalStorage.java) :68)org.jdesktop.application.LocalStorage.load(LocalStorage.java:188)org.jdesktop.application.SessionStorage.restore(SessionStorage.java:381)org.jdesktop.application.SingleFrameApplication.initRootPaneContainer(SingleFrameApplication。 Java的:210) at org.jdesktop.application.SingleFrameApplication.show(SingleFrameApplication.java:268) at Main.startup(Main.java:11) at Main.main(Main.java:19) 在Main.main的Main.startup(Main.java:11)​​的org.jdesktop.application.SingleFrameApplication.show(SingleFrameApplication.java:268)(Main.java:19)

Why? 为什么? What it exactly means? 它究竟意味着什么?

Tnx TNX

Andrea 安德里亚

To create and show a window with a Hello World label, you need to do the following at the minimum: 要创建并显示带有Hello World标签的窗口,您至少需要执行以下操作:

  • Instantiate a JFrame ( link ). 实例化JFrame( 链接 )。
  • Add your JLabel to it. 将JLabel添加到它。
  • Show the frame. 显示框架。

The oracle tutorial on how to make frames ( link ) shows you exactly how to do that. 关于如何制作框架( 链接 )的oracle教程向您展示了如何做到这一点。 For easy reference, I copied the specific excerpt that defines/shows your window from there. 为了便于参考,我从那里复制了定义/显示窗口的特定摘录。

JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);

您正在扩展SingleFrameApplication ,它是一个为您执行UI设置和初始化的框架类,但您没有提供任何必要的配置信息。

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

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