简体   繁体   English

Java GUI 不会显示 JLabel

[英]Java GUI won't display JLabel

I would like to create a simple GUI in Java.我想用 Java 创建一个简单的 GUI。 I know the basics of creating JLabel , etc. However, I cannot find why my JLabel is not displayed on the screen.我知道创建JLabel等的基础知识。但是,我找不到为什么我的JLabel没有显示在屏幕上。 Here is my code:这是我的代码:

package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class A1Panel extends JPanel implements ActionListener { 
    JLabel firstInt;

    public void init() {
        makeComponents();
        makeLayout();
    }

    private void makeComponents() {
        firstInt = new JLabel("First argument");
        firstInt.setFont(new Font("Helvetica", Font.BOLD, 16));
        firstInt.setBackground(Color.lightGray);
        firstInt.setVisible(true);
        firstInt.setHorizontalAlignment(SwingConstants.CENTER);
    }

    private void makeLayout() {
        add(firstInt);
    }

    public void actionPerformed(ActionEvent e) {    
    }
}

I then add my JPanel to my JFrame using a different class called GUI:然后我使用一个名为 GUI 的不同类将我的JPanel添加到我的JFrame

import test.A1Panel;

public class GUI {
public static void main(String[] args) {       
    JFrame frame = new JFrame("Testing GUI"); 
    frame.setLayout( new GridLayout(1,3));

    JPanel panel = new A1Panel();
    panel.setBorder( BorderFactory.createRaisedBevelBorder() );
    frame.add( panel);

    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.pack();
    }
}

When I hit compile, what I get is a simple frame with three empty panels.当我点击编译时,我得到的是一个带有三个面板的简单框架。 I do not understand why my JLabel is not in the first panel since I have added it to my frame.我不明白为什么我的JLabel不在第一个面板中,因为我已将它添加到我的框架中。 Am I missing something?我错过了什么吗?

After you instance A1Panel, you haven't called A1Panel.init()在你实例 A1Panel 之后,你还没有调用A1Panel.init()

I would suggest removing init() and adding all the code to the constructor of A1Panel .我建议删除init()并将所有代码添加到A1Panel的构造函数中。 If, however, you wanted to keep the init() function, you would want to call it after JPanel panel = new A1Panel()但是,如果您想保留init()函数,则需要在JPanel panel = new A1Panel()之后调用它

The frame is not empty, the panel is.框架不是空的,面板是。 Nowhere in your code do I see a call to the methods init() or makeComponents() .在您的代码中,我没有看到对init()makeComponents()方法的调用。 In fact, I would turn your init() method into a constructor, like so:事实上,我会把你的init()方法变成一个构造函数,就像这样:

public A1Panel() {
    makeComponents();
    makeLayout();
}

Another alternative to this would be to call panel.init() after declaring JPanel panel = new A1Panel()此另一种方法是调用panel.init()宣布之后JPanel panel = new A1Panel()

The code to add the label was not actually called in the main, was it?添加标签的代码实际上并没有在 main 中调用,是吗? So look carefully, when is init actually called?所以仔细看,init究竟是什么时候调用的?

Look at the看着那(这

private void makeLayout()

method.方法。

If I replace public void init() by A1Panel() , it does the job.如果我用A1Panel()替换public void init() A1Panel() ,它就可以完成工作。 Thank you for your help.感谢您的帮助。

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

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