import java.awt.*;import java.awt.event.*;import java.applet.*;
class SampleDialog extends Dialog implements Actionlistener  //这里编译出错 代码A
{
SampleDialog(Frame parent,String title){super(parent,title,false);setLayout(new FlowLayout());
setSize(300,200);
add(new Label("Press this button"));
Button b;
add(b=new Button("按钮"));
b.addActionListener(this);//这里编译出错 代码B
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g)
{
g.drawString("this is in the dialog box",10,70);
}
}
class MenuFrame extends Frame
{
String msg="";
CheckboxMenuItem debug,test;
MenuFrame(String title)
{
super(title);
MenuBar mbar=new MenuBar();
setMenuBar(mbar);
Menu file=new Menu("文件");
MenuItem item1,item2,item3,item4;
file.add(item1=new MenuItem("新建"));
file.add(item2=new MenuItem("打开"));
file.add(item3=new MenuItem("关闭"));
file.add(new MenuItem("-"));
file.add(item4=new MenuItem("退出"));
mbar.add(file);Menu edit=new Menu("编辑");
MenuItem item5,item6,item7;
edit.add(item5=new MenuItem("剪切"));
edit.add(item6=new MenuItem("复制"));
edit.add(item7=new MenuItem("粘贴"));
edit.add(new MenuItem("-"));Menu sub=new Menu("Special",true);
MenuItem item8,item9,item10;
sub.add(item8=new MenuItem("frist"));
sub.add(item9=new MenuItem("second"));
sub.add(item10=new MenuItem("third"));
edit.add(sub);
debug=new CheckboxMenuItem("debug");
edit.add(debug);
test=new CheckboxMenuItem("testing");
edit.add(test);
MyMenuHandler handler=new MyMenuHandler(this);
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
debug.addItemListener(handler);
test.addItemListener(handler);
MyWindowAdapter adapter=new MyWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on.",10,220);
else 
g.drawString("Debug is off.",10,220);
if(test.getState())
g.drawString("testing is on.",10,240);
else 
g.drawString("testing is off.",10,240);
}
}
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void windowCloing(WindowEvent we)
{
menuFrame.dispose();
}
}class MyMenuHandler implements ActionListener,ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void actionPerformed(ActionEvent ae)
{
String msg="You select";
String arg=(String)ae.getActionCommand();
if(arg.equals("新建"))
{
msg+="New.";
SampleDialog d=new SampleDialog(menuFrame,"New Dialog Box");
d.setVisible(true);
}
else if(arg.equals("打开"))
msg+="Open.";
else if(arg.equals("关闭"))
msg+="Close.";
else if(arg.equals("退出"))
msg+="Quit."; 
else if(arg.equals("编辑"))
msg+="Edit.";
else if(arg.equals("剪切"))
msg+="Cut.";
else if(arg.equals("复制"))
msg+="Copy.";
else if(arg.equals("粘贴"))
msg+="Paste.";
else if(arg.equals("First"))
msg+="First.";
else if(arg.equals("Second"))
msg+="Second.";
else if(arg.equals("Third"))
msg+="Third.";
else if(arg.equals("Debug"))
msg+="Debug.";
else if(arg.equals("Testing"))
msg+="Testing.";
menuFrame.msg=msg;
menuFrame.repaint();
}
public void itemStateChanged(ItemEvent ie)
{
menuFrame.repaint();
}
public class rt extends Applet 
{
Frame f;
public void init()
{
f=new MenuFrame("Menu Demo");
int width=Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
setSize(width,height);
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
}
}
A:Cannot resolve symbol
B:addActionListener<java.awt.event.ActionListener>in java.awt.Button cannot be applied to <Sampledialog>

解决方案 »

  1.   

    Actionlistener  --> ActionListener
      

  2.   

    to kingfish(八百里秦川@龙城异客:
    感谢你提醒,现在编译可以了,可是写一个html文件启动rt.class,显示上有错误,只有一个代叉的轮廓
    如何解决?
    html文件如下:
    <html>
    <head>
    <title>
    fghkkjhfyti
    </title>
    </head>
    <body>
    <applet code="rt" width=250 height=250>
    </applet>
    </body>
    </html>
      

  3.   

    这个跟浏览器有关的,用appletviewer就没问题了应该
      

  4.   

    to  believefym(feng):
    可以说细一点吗,我是新手
      

  5.   

    浏览器认为一些applet小程序有危险,就禁止了,所以有个框,然后打叉你进控制台,到你的html那个文件目录下,然后输入appletviewer xxx.html试试
      

  6.   

    to believefym(feng)
    我试过了,无效,急急急急急
      

  7.   

    楼主,你根本没理解什么是applet,给你看个简单的例子吧
    start、stop函数什么意思你都不知道
    init初始化是让你把applet本身初始化,而不是去初始化另外的container,然后通过你的applet来setVisible,你就是要把你需要的东西放到applet上就可以了
    import java.applet.*;
    import java.awt.*;public class TestApplet extends Applet{
    public void init(){
    this.add(new Button("ddd"));
    }
    }
      

  8.   

    //下面的可以运行了,不过我真怀疑这个是书上的例子
    //类名一般以大写开头import java.awt.*;import java.awt.event.*;import java.applet.*;
    class SampleDialog extends Dialog implements ActionListener  //这里编译出错 代码A
    {
    SampleDialog(Frame parent,String title){super(parent,title,false);setLayout(new FlowLayout());
    setSize(300,200);
    add(new Label("Press this button"));
    Button b;
    add(b=new Button("按钮"));
    b.addActionListener(this);//这里编译出错 代码B
    }
    public void actionPerformed(ActionEvent ae)
    {
    dispose();
    }
    public void paint(Graphics g)
    {
    g.drawString("this is in the dialog box",10,70);
    }
    }
    class MenuFrame extends Frame
    {
    String msg="";
    CheckboxMenuItem debug,test;
    MenuFrame(String title)
    {
    super(title);
    MenuBar mbar=new MenuBar();
    setMenuBar(mbar);
    Menu file=new Menu("文件");
    MenuItem item1,item2,item3,item4;
    file.add(item1=new MenuItem("新建"));
    file.add(item2=new MenuItem("打开"));
    file.add(item3=new MenuItem("关闭"));
    file.add(new MenuItem("-"));
    file.add(item4=new MenuItem("退出"));
    mbar.add(file);Menu edit=new Menu("编辑");
    MenuItem item5,item6,item7;
    edit.add(item5=new MenuItem("剪切"));
    edit.add(item6=new MenuItem("复制"));
    edit.add(item7=new MenuItem("粘贴"));
    edit.add(new MenuItem("-"));Menu sub=new Menu("Special",true);
    MenuItem item8,item9,item10;
    sub.add(item8=new MenuItem("frist"));
    sub.add(item9=new MenuItem("second"));
    sub.add(item10=new MenuItem("third"));
    edit.add(sub);
    debug=new CheckboxMenuItem("debug");
    edit.add(debug);
    test=new CheckboxMenuItem("testing");
    edit.add(test);
    MyMenuHandler handler=new MyMenuHandler(this);
    item1.addActionListener(handler);
    item2.addActionListener(handler);
    item3.addActionListener(handler);
    item4.addActionListener(handler);
    item5.addActionListener(handler);
    item6.addActionListener(handler);
    item7.addActionListener(handler);
    item8.addActionListener(handler);
    item9.addActionListener(handler);
    item10.addActionListener(handler);
    debug.addItemListener(handler);
    test.addItemListener(handler);
    MyWindowAdapter adapter=new MyWindowAdapter(this);
    addWindowListener(adapter);
    }
    public void paint(Graphics g)
    {
    g.drawString(msg,10,200);
    if(debug.getState())
    g.drawString("Debug is on.",10,220);
    else 
    g.drawString("Debug is off.",10,220);
    if(test.getState())
    g.drawString("testing is on.",10,240);
    else 
    g.drawString("testing is off.",10,240);
    }
    }
    class MyWindowAdapter extends WindowAdapter
    {
    MenuFrame menuFrame;
    public MyWindowAdapter(MenuFrame menuFrame)
    {
    this.menuFrame=menuFrame;
    }
    public void windowCloing(WindowEvent we)
    {
    menuFrame.dispose();
    }
    }class MyMenuHandler implements ActionListener,ItemListener
    {
    MenuFrame menuFrame;
    public MyMenuHandler(MenuFrame menuFrame)
    {
    this.menuFrame=menuFrame;
    }
    public void actionPerformed(ActionEvent ae)
    {
    String msg="You select";
    String arg=(String)ae.getActionCommand();
    if(arg.equals("新建"))
    {
    msg+="New.";
    SampleDialog d=new SampleDialog(menuFrame,"New Dialog Box");
    d.setVisible(true);
    }
    else if(arg.equals("打开"))
    msg+="Open.";
    else if(arg.equals("关闭"))
    msg+="Close.";
    else if(arg.equals("退出"))
    msg+="Quit."; 
    else if(arg.equals("编辑"))
    msg+="Edit.";
    else if(arg.equals("剪切"))
    msg+="Cut.";
    else if(arg.equals("复制"))
    msg+="Copy.";
    else if(arg.equals("粘贴"))
    msg+="Paste.";
    else if(arg.equals("First"))
    msg+="First.";
    else if(arg.equals("Second"))
    msg+="Second.";
    else if(arg.equals("Third"))
    msg+="Third.";
    else if(arg.equals("Debug"))
    msg+="Debug.";
    else if(arg.equals("Testing"))
    msg+="Testing.";
    menuFrame.msg=msg;
    menuFrame.repaint();
    }
    public void itemStateChanged(ItemEvent ie)
    {
    menuFrame.repaint();
    }}
    public class rt extends Applet 
    {
    Frame f;
    public void init()
    {
    f=new MenuFrame("Menu Demo");
    int width=Integer.parseInt(getParameter("width"));
    int height=Integer.parseInt(getParameter("height"));
    setSize(width,height);
    f.setSize(width,height);
    f.setVisible(true);
    this.add(f);
    }
    public void start()
    {
    f.setVisible(true);
    }
    public void stop()
    {
    f.setVisible(false);
    }
    }
      

  9.   

    to believefym(feng)
    我向你保证是书上的,我才学java,是冶金工业出版社的《Java 程序设计》 P304页的例子,看的我有些烦,而且有些地方不规范,可能是我买错的?你们入门买的是什么?
      

  10.   

    本人入门看的是《java how to program》,中文版
    现在看thinking in java第三版