简体   繁体   English

如何在Java Swing中创建一个hello世界? 我的代码有什么问题?

[英]How to create an hello world in Java Swing? What is wrong in my code?

I am studying Java Swing and I have some problem with the following simple code: 我正在研究Java Swing,下面的简单代码有一些问题:

package com.techub.exeute;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class Main{

    public static void main(String[] args) {

        JFrame frame = new JFrame("FrameDemo");
        frame.setMinimumSize(new Dimension(800, 400));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

        JLabel myLabel = new JLabel("Hello World !!!", SwingConstants.CENTER);
        myLabel.setFont(new Font("Serif", Font.BOLD, 22));
        myLabel.setBackground(Color.blue);
        myLabel.setOpaque(true);
        myLabel.setPreferredSize(new Dimension(100, 80));

        frame.getContentPane().add(myLabel, BorderLayout.NORTH);

    }
}

My idea is to create a JFrame object and insert into it an Hello World JLabel object settings some property. 我的想法是创建一个JFrame对象,并向其中插入一个Hello World JLabel对象,该对象设置一些属性。

I do it into the main() method. 我将其放入main()方法中。 The problem is that when I execute the program I don't see anything !!! 问题是,当我执行程序时,我什么都看不到! Why? 为什么? What is wrong in my code? 我的代码有什么问题?

Tnx 特纳克斯

Andrea 安德里亚

You are creating the frame but you are not displaying it. 您正在创建框架,但没有显示它。 Call 呼叫

frame.setVisible(true);

to display it. 显示它。

Another thing: you should not manipulate GUI components in the main thread. 另一件事:您不应在主线程中操纵GUI组件。 Instead, create a new method for creating the frame and setting up the components, and run that method in the event dispatch thread, like in the example from the official tutorial : 而是创建一个用于创建框架和设置组件的新方法,然后在事件分发线程中运行该方法,如官方教程的示例所示

import javax.swing.*;        

public class HelloWorldSwing {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Just add 只需添加

frame.setVisible(true);

to your code 到你的代码

See the steps to Creating and Showing Java Swing Frames 请参阅创建和显示Java Swing框架的步骤

//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");

//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//4. Size the frame.
frame.pack();

//5. Show it.
frame.setVisible(true);

You missed out #5 你错过了#5

You need a 你需要一个

frame.setVisible(true);

call in your code. 调用您的代码。

As others mentioned you should not use the main Thread for gui operations. 正如其他人提到的那样,您不应将主Thread用于gui操作。 I suggest you should refer to the official tutorials of SWING, they are rather helpful and you'll see examples there for proper threading. 我建议您参考SWING的官方教程 ,它们非常有帮助,并且您会在其中看到有关正确线程化的示例。

在您的方法中保持这一行

frame.setVisible(true);

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

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