无法在 JFrame 上显示 JLabel

Cannot display JLabel on JFrame

--情况--

我想在 JFrame 上创建一个 JLabel。我一直在使用 3 类:

1) Window.java - Window (JFrame);

2) Main.java - 程序可运行脚本

3) GUI.java - 文本字段、标签、按钮

--问题--

我得到的只是一个 JFrame。上面没有标签。

--源代码--

Window.java:

    package GUI;

import java.awt.FlowLayout;

import javax.swing.JFrame;

public class Window extends JFrame {

    private static final long serialVersionUID = 1L;

    public Window(int width, int height, String title, boolean resizable, int operation) {

        JFrame gui = new JFrame();
        gui.setLayout(new FlowLayout());
        gui.setDefaultCloseOperation(operation);
        gui.setSize(width, height);
        gui.setVisible(true);
        gui.setTitle(title);
        gui.setResizable(resizable);
        gui.setLocationRelativeTo(null);

    }

}

Main.java:

package Main;

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

import Func.Func;
import GUI.GUI;
import GUI.Window;

public class Main{

    /**
     * 
     */
    static Func func = new Func();
    static GUI gui = new GUI();
    static Window window1 = new Window(1200, 800, "JFrame Example", false, JFrame.EXIT_ON_CLOSE);

    public static void main(String[] args) {

        JLabel label = gui.createLabel("Hi dude!!!!!", 0, 0);
        window1.add(label);

    }

}

GUI.java:

package GUI;

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {

    /**
     * Author Sculptor86
     */


    public GUI() {

    }

    private static final long serialVersionUID = 1L;

    public JLabel createLabel(String text, int AY, int AX) {

        JLabel label = new JLabel(text, JLabel.CENTER);
        label.setAlignmentX(AX);
        label.setAlignmentY(AY);
        label.setVisible(true);
        return label;

    }

    public JTextField createTextBox(String text, Color fg, Color bg, int Max) {

        JTextField textField;
        textField = new JTextField(text, Max);
        textField.setForeground(fg);
        textField.setBackground(bg);
        textField.setVisible(true);
        return textField;

    }

    public JButton createButton(String text, Color fg, Color bg) {

        JButton button;
        button = new JButton(text);
        button.setForeground(fg);
        button.setBackground(bg);
        button.setVisible(true);
        return button;

    }

}

希望有人能帮忙, 弗拉德

您正在将标签添加到您的 Window 对象,而不是您在 Window 的构造函数中创建的 JFrame。

这是更正后的 Main class:

// Main class, which just creates a window and adds a label and shows it
public class Main{
    public static void main(String[] args) {
        Window window = new Window(1200, 800, "JFrame Example", false, JFrame.EXIT_ON_CLOSE);
        JLabel label = window.createLabel("Hi dude!!!!!", 0, 0);
        window.add(label);
        window.setVisible(true);
    }
}

这是更正后的 Window class(为了简洁起见,我已经完全删除了 GUI class 并将工厂方法的行为纳入此 class):

// The basic window class (pretty much a constructor facade around JFrame)
public class Window extends JFrame {
    // constructor for creating windows
    public Window(int width, int height, String title, boolean resizable, int operation) {
        setLayout(new FlowLayout());
        setDefaultCloseOperation(operation);
        setSize(width, height);
        setTitle(title);
        setResizable(resizable);
        setLocationRelativeTo(null);
    }

    // factory method creating labels
    public static JLabel createLabel(String text, int AY, int AX) {
        JLabel label = new JLabel(text, JLabel.CENTER);
        label.setAlignmentX(AX);
        label.setAlignmentY(AY);
        label.setVisible(true);
        return label;
    }

    // factory method creating text fields
    public static JTextField createTextBox(String text, Color fg, Color bg, int max) {
        JTextField textField = new JTextField(text, max);
        textField.setForeground(fg);
        textField.setBackground(bg);
        textField.setVisible(true);
        return textField;
    }

    // factory method creating buttons
    public static JButton createButton(String text, Color fg, Color bg) {
        JButton button = new JButton(text);
        button.setForeground(fg);
        button.setBackground(bg);
        button.setVisible(true);
        return button;
    }
}