【发布时间】: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