问答题

下面程序是一个计时器,从1000秒开始倒计时,直到为0结束。在界面上有两个按钮,一个可以暂停计时,另一个可以继续已经暂停的计时。请更正题中带下划线的部分。 注意:不改动程序的结构,不得增行或删行 import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Example3_4 extends Applet { public Color color = Color.red; private int num= 1000; public Counter theCounter; private Button stop; private Button start; public void init() { stop = new Button("暂停"); start = new Button ("继续"); theCounter = new Counter(this); stop.addActionListener(new Lst() implements ActionListener{ public void actionPerformed(ActionEvent e) { theCounter.sus(); } }); start.addActionListener(new SuspenListener()); add(start); add(stop); theCounter.start(); } public void paint(Graphics g) { g.setCotor(color); g.drawString(String.valueOf(num),50,50); } public void setInt(int i) { num=i; } class SuspenListener implements ActionListener { public void actionPerformed(ActionEvent e) { theCounter.conti (); } } } public class Counter extends Thread { Example3_4 example; boolean isHold; Counter (Example3_4 ex) { this.example = ex; isHold = false; } public void sus() { isHold = true; } public synchronized void conti() { isHold = false; notify(); } public void run() { for (int i = 1000; i>0; i--) { if (i%2 == 1) example.color = Color.red; else example.color = Color.blue; example.setInt(i); example.repaint(); try { sleep(1000); synchronized { while(isHold) wait ( ); } } catch (InterruptedException ie) {} } } } <HTML> <HEAD> <TITLE>Example3_4</TITLE> </HEAD> <BODY> <applet code="Example3_4.class" width=300 height=400> </applet> </BODY> </HTML>

【参考答案】

①stop.addActionListener(new ActionListener(){②class Counter ......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
下列程序用来显示用户要求打开的图片。在名为“读取图像”的JFrame框架中有一个单行文本框,用户可以在其中输入图片文件的文件名称,按下“浏览图片”按钮以后,新生成一个对话框,将图片显示在这个对话框中,运行结果如下图所示。请填写横线处的内容。注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class Example2_9{public static void main(String[] args){JFrame frame = new FileNameFrame( 读取图像 );frame.setDefaultCloseOperation(JFrame. EXIT ON CLOSE);frame.setSize(300, 100);frame.show();}}class FileNameFrame extends JFram implements ActionListener{JLabel inputLabel;JTextField fileNameText;JButton containtButton;public FileNameFrame(String titleText){super(titleText);inputLabel = new Jnabel( 请输入图像文件的名称 );fileNameText = new JTextField(10);containtButton = new JButton( 浏览图片 );fileNameText.addActionListener(this);containtButton.addActionListener(this);JPanel panel = new JPanel();panel.add(fileNameText);panel.add(containtButton);Container containt = getContentPane();containt.setLayout(new BorderLayout(3,3));containt.add(inputLabel, BorderLayout.NORTH);containt.add(panel, BorderLayout.CENTER);}public void actionPerformed(ActionEvent e){String fileName = fileNameText.getText();ImageIcon image = ______;ImageDialog dlg = new ImageDialog(this);dlg.setImage(image);dlg.setTitle(fileName);dlg.show();}class ImageDialog extends JDialog{JLabel imageLabel;public ImageDialog(JFrame frame){super(frame);imageLabel = new JLabel();getContentPane().add(imageLabel);this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){hide ();}});this.setSize(200, 200);}public void setImage(ImageIcon icon){imageLabel. ______(icon);}}}