java 笔试题

[功能]: 编写程序包含一个标签和一个按钮。
 标签初始显示“你好”
 在单击按钮时,标签上会显示“再见”
 再单击按钮,标签上显示“你好”,也就是标签的内容在"你好"和"再见"之间切换。
(2)、编译并运行程序,将编写的代码和运行结果截图贴至下方。

[code="java"]import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class TestLabel {
public static void main(String args[]) {
ApplicationFrame AF = new ApplicationFrame();
}
}

class ApplicationFrame extends JFrame implements ActionListener
{
Label prompt;
Button btn;

ApplicationFrame()
{
super("我的窗口");
prompt = new Label("你好");
btn = new Button("显示");

setLayout(new FlowLayout());
add(prompt);
add(btn);

btn.addActionListener(this);

setVisible(true);
setSize(500, 600);
setLocation(300, 400);
}

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "显示")
{

if(prompt.getText().equals("你好"))
prompt.setText("再见");
else if (prompt.getText().equals("再见"))
prompt.setText("你好");
}

}

} [/code]