简体   繁体   中英

Java Jpanel is not showing up

Java Jpanel is not showing up when run, bottoms out after several seconds referencing (getContentPane().add(new MeFirstApp());) in MeFirstApp class

/* * File: MeFirstPanel.java * * Description: This class defines a GUI in a JPanel which contains * two JButton with initial labels "Me first!" and "Me next!". * Pressing either button causes the labels to be exchanged. * * Assignment: 1) Add a third button to the panel, with the label "third" * 2) Every time any of the buttons are pressed, the labels * should shift one place to the right first->second->third * would shift to third->first->second when one of the buttons * was pressed */

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

You never add MeFirstPanel_Wallace to MeFirstApp

As a general rule of thumb, you should be extending from JFrame , you're not actually adding any new functionality to it. Instead, you might use something more like...

/*
 * 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

for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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