【问题标题】:Cannot display JLabel on JFrame无法在 JFrame 上显示 JLabel
【发布时间】:2015-03-01 17:49:44
【问题描述】:

--情况--

我想在 JFrame 上制作一个 JLabel。我一直在使用 3 个类:

1) Window.java - 窗口(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;

    }

}

希望有人能帮忙, 弗拉德

【问题讨论】:

  • 不要依赖静态

标签: java swing jlabel


【解决方案1】:

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

这是更正后的 Main 类:

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

【讨论】:

  • @Reimeus 你说得对,这不好。但真正的问题是他将构造函数中的参数添加到 window 中的新 JFrame 中。
  • 现在您已经确定了问题所在,您能否建议操作人员解决问题?
  • @MadProgrammer 解决问题将涉及重大重构。如果 OP 坚持,我会非常乐意这样做......但我认为答案是 OP 需要单独解决问题的方向。
  • 鉴于 OP 没有认识到他们正在创建两个不同的框架这一事实,我怀疑他们会知道如何修复它。
  • @MadProgrammer 好吧,你可能是对的。我将在答案中粘贴一个简单的解决方案。
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多