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

/ *
     *文件:MeFirstPanel.java
     *
     *说明:此类在JPanel中定义了一个GUI,其中包含
     *两个带有初始标签“ Me first!”的JButton。和“我下一个!”。
     *按下任一按钮将交换标签。
     *
     *分配:1)在面板上添加第三个按钮,标签为“ third”
     * 2)每次按任何一个按钮,标签
     *应该将一个位置向右移第一->第二->第三
     *当其中一个按钮出现时,将转换为第三→第一→第二
     *被按下
     * /

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MeFirstPanel_Wallace extends JPanel implements ActionListener
{
    private JButton aButton;
    private JButton bButton;
    private JButton cButton;
    // add button here

    String aText = "first";
    String bText = "second";
    String cText = "third";
    // add string here

    String tempText; // To use to exchange labels

    public MeFirstPanel_Wallace()
    {
        aButton = new JButton(aText);
        aButton.addActionListener(this); // add event handler
        bButton = new JButton(bText);
        bButton.addActionListener(this); // add event handler
        cButton = new JButton(cText);
        cButton.addActionListener(this); // add event handler

        add(aButton); // add button to JPanel
        add(bButton); // add button to JPanel
        add(cButton); // add button to JPanel

    } // MeFirstPanel()


    public void actionPerformed(ActionEvent e)
    {
            tempText = aText;  // Exchange the strings
            aText = bText;
            bText = cText;
            cText = tempText;
            // add code here
            aButton.setText(aText); // Set button labels
            bButton.setText(bText);
            cButton.setText(cText);
            // add code here

    } // actionPeformed()
} // MeFirstPanel class

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

import javax.swing.*;

public class MeFirstApp extends JFrame
{   public MeFirstApp()
    {
        setSize(200,200);
        getContentPane().add(new MeFirstApp());
        //register 'Exit upon closing' as a default close operation
        setDefaultCloseOperation( EXIT_ON_CLOSE );
    }

    public static void main(String args[]) {
        MeFirstApp b = new MeFirstApp();
        b.setVisible(true);
    } // main()

} // MeFirstApp class

最佳答案

您永远不会将MeFirstPanel_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


例如

09-25 16:48