【问题标题】:Java Jpanel is not showing upJava Jpanel 没有出现
【发布时间】:2016-02-26 06:28:00
【问题描述】:

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

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

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

标签: java swing


【解决方案1】:

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

例如

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多