无法在JFrame上显示JLabel

时间:2015-03-01 17:49:45

标签: java swing jlabel

- 现状 -

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

1)Window.java - Window(JFrame);

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

3)GUI.java - TextFields,Labels,Buttons

- 问题 -

我得到的只是一个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;

    }

}

希望有人可以提供帮助, 维拉德

1 个答案:

答案 0 :(得分:0)

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

以下是更正的主要课程:

// 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类(为了简洁,我已经完全删除了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;
    }
}