排错!
报错和程序如下,没改好,大家帮着看看,谢谢!
--------------------Configuration: j2sdk1.4.2_03 <Default>--------------------
C:\j2sdk1.4.2_03\mycode\ThreadTest\Counter2.java:48: actionPerformed(java.awt.event.ActionEvent) in cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to assign weaker access privileges; was public
void actionPerformed(ActionEvent e)
^
C:\j2sdk1.4.2_03\mycode\ThreadTest\Counter2.java:61: actionPerformed(java.awt.event.ActionEvent) in cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to assign weaker access privileges; was public
void actionPerformed(ActionEvent e)
^
2 errors
------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.applet.*;
public class Counter2
{
public static void main(String[] args)
{
Counter2Frame frame = new Counter2Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class Counter2Frame extends JFrame
{
public Counter2Frame()
{
Counter2Panel panel = new Counter2Panel();
getContentPane().add(panel);
setTitle("ThreadTest2");
setSize(300,200);
}
}
class Counter2Panel extends JPanel
{
private JTextField text;
private JButton on;
private JButton off;
private SubThread t1;
public Counter2Panel()
{
setLayout(new BorderLayout());
text = new JTextField(30);
add(text,BorderLayout.CENTER);
on = new JButton("start");
on.addActionListener(new
ActionListener(){
void actionPerformed(ActionEvent e)
{
if (t1 == null)
{
t1 = new SubThread();
t1.start();
}
}
});
off = new JButton("stop");
off.addActionListener(new
ActionListener(){
void actionPerformed(ActionEvent e)
{
if(!(t1 == null))
t1.invertFlag();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(on);
buttonPanel.add(off);
add(buttonPanel,BorderLayout.SOUTH);
}
private class SubThread extends Thread
{
private int counter =0;
private boolean runFlag = true;
public void run()
{
while(true)
{
try{
sleep(100);
}
catch(InterruptedException e){}
}
if(runFlag)
{
text.setText(Integer.toString(++counter));
}
}
public void invertFlag() { runFlag = !runFlag;}
}
}
-------------------------------------------------------------------------