哪位大虾帮忙做做这个程序要对的哦

来源:百度知道 编辑:UC知道 时间:2024/05/15 08:05:09
写一个应用程序,要求编写Frame的子类MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器,当单击按钮时,程序获取文本框中的文本,并将该文本作为按钮的名称。然后再编写一个Frame的子类,即窗口。窗口的布局为BorderLayout布局。窗口中添加两个MyPanel面板,分别添加到窗口的东部区域和西部区域。

import java.awt.*;
import java.awt.event.*;

public class MyPanel extends Frame {
Button b = new Button("ButtonName");
double heigth, weigth;
double x, y;
TextField tf1 = new TextField(null, 10);

public MyPanel() {
super("MyPanel");
setLayout(new FlowLayout(FlowLayout.LEFT));
add(tf1);
add(b);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (tf1.getText() != null) {
try {
b.setLabel(tf1.getText());

} catch (NumberFormatException ex) {

b.setLabel("ButtonName");
}

}

}
}