简体   繁体   English

无法在JFrame上显示JLabel

[英]Cannot display JLabel on JFrame

--Situation-- - 情况 -

I wanted to make a JLabel on a JFrame. 我想在JFrame上创建一个JLabel。 I have been using 3 classes: 我一直在使用3个班级:

1) Window.java - Window (JFrame); 1)Window.java - Window(JFrame);

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

3) GUI.java - TextFields, Labels, Buttons 3)GUI.java - TextFields,Labels,Buttons

--Question-- - 题 -

All I get is a JFrame. 我得到的只是一个JFrame。 No label on it. 没有标签。

--Source Code-- - 源代码 -

Window.java: 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: 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: 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;

    }

}

Hope someone can help, Vlad 弗拉德希望有人可以提供帮助

You are adding the label to your Window object not to the JFrame you create in the constructor of Window. 您将标签添加到Window对象,而不是您在Window的构造函数中创建的JFrame。

Here is the corrected 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);
    }
}

Here is the corrected Window class (I have completely removed the GUI class and subsumed factory methods' behavior into this class for brevity): 这是更正后的Window类(为了简洁起见,我已完全删除了GUI类并将工厂方法包含在此类中):

// 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;
    }
}

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

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