Java JButton没有添加

Java JButton没有添加,java,swing,jpanel,jbutton,Java,Swing,Jpanel,Jbutton,我不确定为什么我的JButton在添加到JPanel类时没有显示:如果我的类扩展了JPanel,那么这个.add是否足够?如果我从另一个Jpanel中删除这个Jpanel类,那么连接到此面板的组件也应该消失,对吗?谢谢大家! public class Menu extends JPanel{ private static final long serialVersionUID = 1L; static boolean startGame = false; public static Str

我不确定为什么我的JButton在添加到JPanel类时没有显示:如果我的类扩展了JPanel,那么这个.add是否足够?如果我从另一个Jpanel中删除这个Jpanel类,那么连接到此面板的组件也应该消失,对吗?谢谢大家!

public class Menu extends JPanel{

private static final long serialVersionUID = 1L;

static boolean startGame = false;

public static String user;

public Menu(final JFrame frame) {
    JPanel userName = new JPanel(); // create new JPanel
    final JTextField text = new JTextField();
    text.setText("input your username here");

    //adds userName input box to the panel

    Action action = new AbstractAction(){

        private static final long serialVersionUID = 1L;

    @Override 
    public void actionPerformed(ActionEvent e) {
        user = text.getText();
        text.setVisible(false);
        Menu.startGame = true;
    }};

    text.addActionListener(action);

    userName.add(text);
    frame.add(userName, BorderLayout.SOUTH);
    frame.revalidate();
    frame.repaint();

    setFocusable(true);

    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            sndMenu newMenu = new sndMenu(frame);
            newMenu.setVisible(true);
        }
    });

/** 
  * this does not display help!
 */

    //add a button to the menu called "help"
    JButton help = new JButton("get instructions");
    this.add(help,BorderLayout.CENTER); 
    help.setVisible(true);

    help.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            instructions instr = new instructions(frame);
            frame.add(instr);
            frame.revalidate();
            frame.repaint();
        }
    });
}

public void paint (Graphics g) {    
    super.paint(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);  

    Font fnt0 = new Font("Manaspace", Font.BOLD, 40);
    Font fnt1 = new Font("Manaspace", Font.BOLD, 30);
    g.setFont(fnt0);
    g.setColor(Color.white);
    g.drawString("Welcome To Jumping Box!", 120, 300);
    g.setFont(fnt1);
    g.drawString("Click to play!", 260, 400);
}

}

您的
菜单
JPanel
从未添加到任何内容。尝试
frame.add(这是BorderLayout.CENTER)userName
面板(根据发布的代码)@CarlosHeuberger-yep,因此我有另一个名为Main.java的类,它将JPanel添加到框架中,该框架是在Main.java中启动的。我想将JButton添加到JPanel,因为我希望JButton只在添加JPanel时出现,而不是在Jframe设置为visible时出现