Java Jpanel 没有出现 [英] Java Jpanel is not showing up

查看:45
本文介绍了Java Jpanel 没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

/** 文件:MeFirstPanel.java** 说明:该类在 JPanel 中定义了一个 GUI,其中包含* 两个带有初始标签我先!"的 JButton和我下一个!".* 按任一按钮会导致标签交换.** 作业:1) 向面板添加第三个按钮,标签为第三个"* 2) 每次按下任何按钮时,标签* 应该向右移动一个位置 first->second->third* 当其中一个按钮时,将切换到第三个->第一个->第二个* 被按下*/

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

You never add MeFirstPanel_Wallace to MeFirstApp

作为一般经验法则,您应该从 JFrame 进行扩展,您实际上并未向其添加任何新功能.相反,您可能会使用更像...

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

例如

这篇关于Java Jpanel 没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆