Java 无法从其他方法添加gui元素,如果statmens

Java 无法从其他方法添加gui元素,如果statmens,java,if-statement,methods,Java,If Statement,Methods,嘿,我有一个问题,我是GUI新手,所以当我想使用其他方法或if语句向窗口添加元素时,我需要一些帮助。我没有收到错误,但它没有显示。听到我标记的代码问题,我在java中工作,顺便说一句,这不是整个程序,但这只是一个问题 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame{ private JTextField usernameTF; priva

嘿,我有一个问题,我是GUI新手,所以当我想使用其他方法或if语句向窗口添加元素时,我需要一些帮助。我没有收到错误,但它没有显示。听到我标记的代码问题,我在java中工作,顺便说一句,这不是整个程序,但这只是一个问题

import java.awt.*;  

import java.awt.event.*;

import javax.swing.*;

public class Gui extends JFrame{

private JTextField usernameTF;
private JPasswordField passwordField;
private String username,password;
private JRadioButton A,B,C,D,F;

//private JComboBox box;
private JLabel logo,errorPic,promt;
private JButton logIn;
private boolean value;
private Apples function= new Apples();

public Gui(){
    super ("Awsome Progrma");
    setLayout(new FlowLayout());

    Icon errorIcon = new  ImageIcon(getClass().getResource("wrong.png"));
    errorPic = new JLabel(errorIcon);

    usernameTF = new JTextField(10);
    usernameTF.setToolTipText("Enter your user name hear");
    add(usernameTF);

    passwordField = new JPasswordField(10);
    passwordField.setToolTipText("Enter your password hear");
    add(passwordField);

    logIn = new JButton("Log IN");
    add(logIn);

    usernameTF.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    username = event.getActionCommand();
                    password = passwordField.getText();
                    value = function.chack(username,password);
                    if (value == true){add(errorPic);}                      // this is a problem JLabel dosn't show up in my window 
                }
            }
        );
    passwordField.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    username = usernameTF.getText();;
                    password = event.getActionCommand();
                    value = function.chack(username,password);
                    if (value == true){add(errorPic);}                          // this is a problem JLabel dosn't show up in my window 
                }
            }
        );
    logIn.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    username = usernameTF.getText();
                    password = passwordField.getText();
                    value = function.chack(username,password);
                    if (value == true){add(errorPic);}                                  // this is a problem JLabel dosn't show up in my window 
                    }
                }
            );
    }

}

唯一不会显示的GUI元素是
Jlabel
errorPic
。这是因为添加组件后需要验证容器。您需要拨打:

revalidate();
repaint();
添加
JLabel
后。更好的方法是在向
JFrame
添加组件时添加一个没有图像的
JLabel
,然后只需调用
JLabel.setIcon
即可更新标签


一些旁注:

  • 不要扩展
    JFrame
    。而是直接创建窗口组件的实例
  • JPassword.getText
    不推荐使用。使用
    JPassword.getPassword
    更安全
  • 考虑在应用程序启动时使用

顺便说一句,您应该使用
.getPassword()
作为密码
.getText()
已被弃用。您是否已调试应用程序以检查该语句是否为真?是的,我检查它是否返回true@mrninjaman当前位置如果这个答案解决了你的问题,请投票并接受它。