问答题

本题程序的功能是主窗口里有一个文本框和两个按钮“开始”和“关闭”。单击“开始”按钮后该按钮处于按下状态,文本框中的数开始从0计数到49,每间隔50ms增加1,计数完毕后“开始”按钮变为可使用状态,单击“关闭”按钮则退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simple

public static void main (String[] args)

JFrame frame = new CounterFrame();
frame.setDefaultCloseOperation(JFrame.EXIT ON CLOSE);
frame.show();


class CounterFrame extends JFrame

public static final int WIDTH = 250;
public static final int HEIGHT = 150;
private TextField textField = new TextField(20);
public CounterFrame()

setSize(WIDTH,HEIGHT);
setTitle("simple");
Container contentPane = getContentPane();
JPanel textPanel = new JPanel();
textPanel.add(textField);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel,"开始",new ActionListener()
public void actionPerformed(ActionEvent evt)

addCounter();

);
addButton(buttonPanel,"关闭",new ActionListener()
public void actionPerformed(ActionEvent evt)

______;

);
contentPane.add(textPanel,BorderLayout.NORTH);
contentPane.add(buttonPanel,BorderLayout.SOUTH);

public void addButton(Container c,String title,ActionListener listener)

JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);

public void addCeunter()

for (int i = 0; i<50; i++)

try

textField.setText(Integer.toString(i));
______;
catch (InterruptedException e)




【参考答案】

System.exit(0)。
Thread.sleep(50)。