Java Jpanel没有出现

Java Jpanel没有出现,java,swing,Java,Swing,Java Jpanel在运行时不显示,在MeFirstApp类中引用(getContentPane().add(new MeFirstApp());)几秒钟后见底 /* *文件:MeFirstPanel.java * *描述:此类在JPanel中定义一个GUI,其中包含 *两个JButton,初始标签为“Me first!”和“Me next!”。 *按下任一按钮都会导致标签交换。 * *分配:1)在面板上添加第三个按钮,标签为“第三个” *2)每次按下任何按钮时,标签 *应将一个位置向右移动第

Java Jpanel在运行时不显示,在MeFirstApp类中引用(getContentPane().add(new MeFirstApp());)几秒钟后见底

/* *文件:MeFirstPanel.java * *描述:此类在JPanel中定义一个GUI,其中包含 *两个JButton,初始标签为“Me first!”和“Me next!”。 *按下任一按钮都会导致标签交换。 * *分配:1)在面板上添加第三个按钮,标签为“第三个” *2)每次按下任何按钮时,标签 *应将一个位置向右移动第一->第二->第三 *当其中一个按钮按下时,将切换到第三->第一->第二 *被逼 */


您从未将
MeFirstPanel\u Wallace
添加到
MeFirstApp

一般来说,您应该从
JFrame
进行扩展,而不是向其添加任何新功能。相反,您可以使用更像

/*
 * File: MeFirstApp.java
 *
 * Description: This app creates a MeFirstPanel and
 *  adds it to the app's content pane.
 *
 * Assignment: see MeFirstPanel.java
 *
 */
import java.awt.EventQueue;
import javax.swing.*;

public class MeFirstApp {

    public static void main(String args[]) {
        new MeFirstApp();
    } // main()

    public MeFirstApp() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MeFirstPanel_Wallace());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

} // MeFirstApp class

例如

您从未将
MeFirstPanel\u Wallace
添加到
MeFirstApp
/*
 * File: MeFirstApp.java
 *
 * Description: This app creates a MeFirstPanel and
 *  adds it to the app's content pane.
 *
 * Assignment: see MeFirstPanel.java
 *
 */
import java.awt.EventQueue;
import javax.swing.*;

public class MeFirstApp {

    public static void main(String args[]) {
        new MeFirstApp();
    } // main()

    public MeFirstApp() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MeFirstPanel_Wallace());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

} // MeFirstApp class