简体   繁体   English

JLabel没有出现在JFrame中

[英]JLabel does not appear in the JFrame

I am having trouble implementing JLabel with JFrame. 我在用JFrame实现JLabel时遇到麻烦。 The program needs to show either "Hello" or "World" in the center of the screen when the button "study" is pressed. 当按下“学习”按钮时,程序需要在屏幕中央显示“ Hello”或“ World”。 Also with this being a flashcard program, when study is pressed a word is placed on the middle of the screen and the program is suppose to read from the text field for the user input and print whether it is right or wrong. 同样,这是一个抽认卡程序,当按下书房时,在屏幕中间放置一个单词,并假设该程序从文本字段中读取以供用户输入并打印是对还是错。 The problem is that the program is reading the text field after study is pressed so it is printing false before the user can input a answer. 问题在于,程序在按下检查键后正在读取文本字段,因此在用户输入答案之前打印错误。

Can someone briefly explain why this is not working and what I can do to fix this issue? 有人可以简要解释一下为什么它不起作用以及我该怎么做才能解决此问题?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class NoteCardGUI implements ActionListener {

public static JFrame frame;
public static JPanel panel;
public static JLabel label;
private NoteCard ex;
private JButton study;

public static Box box1 = new Box(), box2 = new Box(), box3 = new Box();

public NoteCardGUI() {
    ex = new NoteCard("Hello", "World");

    frame = new JFrame("Flash Card");
    panel = new JPanel();

    study = new JButton("Study");


    study.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String resp = NoteCard.getResponse(ex);
            String chal = NoteCard.getChallenge(ex);
            String a = text.getText();

            label = new JLabel(chal, label.CENTER);
            label.setAlignmentX(0);
            label.setAlignmentY(0);
            frame.add(label, BorderLayout.SOUTH);
            frame.revalidate();

            if(resp.compareTo(a) == 0)
            {
                label = new JLabel("Correct!");
            }
            label = new JLabel("Incorrect");
        }
    });

    panel.add(study);

    frame.add(panel);
    frame.setSize(500, 500);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

public static void main(String[] args) {

    new NoteCardGUI();
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

}

You are adding the label to your frame , but you already have added a JPanel on top of the frame . 您正在向frame添加标签,但是已经在frame顶部添加了一个JPanel The solution is to add the label to the panel instead of the frame . 解决方法是将标签添加到panel而不是frame

So change: frame.add(label); 因此更改: frame.add(label); to panel.add(label); panel.add(label);

By default, a JFrame (or rather, its content pane) has BorderLayout . 默认情况下, JFrame (或更确切地说,其内容窗格)具有BorderLayout This means that if you add components to it without specifying a constraint, they will be added at the CENTER . 这意味着,如果在不指定约束的情况下向其添加组件,则将在CENTER处添加它们。 But you can't add more than one element at any of the BorderLayout 's regions. 但是,您不能在BorderLayout的任何区域中添加多个元素。

So in order for this to work, you need to add the label somewhere else other than the center, or have the panel added with some other, explicit region. 因此,为了使其正常工作,您需要将标签添加到中心以外的其他位置,或者在面板上添加其他一些显式区域。

So if you change the add, for example, to: 因此,例如,将添加更改为:

frame.add(label, BorderLayout.NORTH);

It will work - but you must not forget to also add: 它将起作用-但您一定不要忘记添加以下内容:

frame.revalidate();

Whenever you add components to your GUI, you should call this when you've added them all, in order for it to rebuild the hierarchy of components as needed. 每当将组件添加到GUI时,都应在添加所有组件后调用它,以使它可以根据需要重建组件的层次结构。

Another option would be to change the layout manager of the Frame, or to add to the panel. 另一种选择是更改框架的布局管理器,或添加到面板。

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

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