第4题
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ex7_3.class" width=800 height=400 >
</applet>
*/
public class ex7_3 extends JApplet {
private JTextField jtf = new JTextField(15);
private JButton Hold = new JButton("Hold"),resume = new JButton("Resume");
private ex7_3th obj7_3th = new ex7_3th();
class ex7_3th extends Thread {
private int cnt = 0;
private boolean bIsHold = false;
public ex7_3th() { start(); }
public void hold() {
bIsHold = true;
}
public synchronized void fauxResume() {
bIsHold = false;
wait();
}
public void run() {
while (true) {
try {
sleep(150);
synchronized(this) {
while (bIsHold)
not i fy ( );
}
} catch(InterruptedException ie) {
System.err.println("Interrupted");
}
jtf.setText(cnt);
}
}
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jtf);
Hold.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
obj7_3th.hold();
}
});
cp.add(Hold);
resume.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
obj7_3th.fauxResume();
}
});
cp.add(resume);
public static void main(String[] args) {
ex7_3 obj7_3=new ex7_3();
String str= obi7_3.getClass().toString();
if(str.indexOf("class") != -1)
str= str.substring(6);
JFrame. frm = new JFrame(str);
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(O);
}
});
frm.getContentPane().add(obj7_3);
frm.setSize(300, 200);
obj7_3.init();
第5题
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ex19_3.class" width=800 height=400>
</applet>
*/
public class ex19_3 extends JApplet
{
private JTextField jtf=new JTextField(15);
private JButton Hold=new JButton("Hold");
private JButton resume=new JButton("Resume");
private ex19_3th obj19_3th=new ex19_3th();
class ex19_3th extends Thread
{
private int cnt=0;
private boolean bIsHold=false;
public ex19_3th() {start();}
public void hold()
{
bIsHold=true;
}
public synchronized void fauxResume()
{
bIsHold=false;
wait ();
}
public void run()
{
while (true)
{
try {
sleep(150);
synchronized(this)
{
while(bIsHold)
notify();
}
}catch(InterruptedException ie)
{
System.err.println("Interrupted");
}
jtf.setText(cnt);
}
}
}
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jtf);
Hold.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
obj19_3th.hold ( );
}
});
cp.add(Hold);
resume.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
obj19_3th.fauxResume();
}
&n
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!