问答题

本程序中,主窗口中有一个文本框和“开始”、“关闭”两个按钮,单击“开始”按钮后,“开始”按钮处于按下状态,文本框中的数开始从0计数到49,每增加1间隔50毫秒,计数完毕后“开始”按钮变为可使用状态,单击“关闭”按钮则退出程序,如图所示。


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class exam_46
{
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("exam_46");
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 addCounter()
{
for (int i=0; i < 50; i++)
{
try
{
textField.setText(Integer.toString i)) ;
______;
}
catch (InterruptedException e)
{}
}
}
}

【参考答案】

第1处:System.exit(0)
第2处:Thread.sleep(50)
热门 试题

问答题
本程序是一个Applet,页面上有一个“请点击”按钮,单击该按钮后弹出一个对话框,对话框上有三个按钮:“橙色”、“蓝色”和“红色”,单击其中任意一个按钮,则可以将对话框的背景色设置为按钮名称所对应的颜色,如图所示。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class exam_47 extends JApplet{private JFrame frame;______(){frame=new JFrame();frame.setTitle( exam_47 );frame.setSize(300, 200);frame.getContentPane().add(new ButtonPanel());JButton PopButton=new JButton( 请点击 );getContentPane().add(PopButton);PopButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt){if (frame.isVisible()) frame.setVisible(false);else______;}});}}class ButtonPanel extends JPanel{private class ColorAction implements ActionListener{private Color backgroundColor;public void actionPerformed(ActionEvent actionevent){setBackground(backgroundColor);repaint();}public ColorAction(Color color){backgroundColor=color;}}public ButtonPanel(){JButton jbutton=new JButton( 橙色 );JButton jbutton1=new JButton( 蓝色 );JButton jbutton2=new JButton( 红色 );add(jbutton);add(jbutton1);add(jbutton2);ColorAction coloraction=new ColorAction(Color.orange);ColorAction coloractionl=new ColorAction(Color.blue);ColorAction coloraction2=new ColorAction(Color.red);jbutton.addActionListener(coloraction);jbuttonl.addActionListener(coloractionl);jbutton2.addActionListener(coloraction2);}exam_47.htmh:<html><head><title>exam_47< title>< head><body><appletcode = exam_47.java width = 100 height = 50 < applet>< body>< html>