import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Graphics;
import java.net.*;public class HelloApplet extends Applet implements ActionListener
{
public void init ()
{
GridLayout layout = new GridLayout (3, 4, 15, 5);
setLayout (layout);
Font font = new Font ("TimesRoman", Font.PLAIN, 24);
setFont (font);
Button button = new Button ("sun1");
button.setActionCommand ("http://www.sum.com");
button.addActionListener(this);
add (button);
}

public void paint (Graphics g)
{
g.drawString ("Hello World!", 0, 50);
}

public void actionPerformed (ActionEvent event)
{
String pageName = event.getActionCommand ();

try
{
URL url = new URL (pageName);
AppletContext context = getAppletContext ();
context.showDocument (url, "_blank");
}
catch (MalformedURLException e)
{
repaint ();
}
}
}

解决方案 »

  1.   

    我想问的是:javax.swing.JTextField的setActionCommand()方法不是java.awt.Button的。
      

  2.   

    有人用过吗? cuizm(射天狼),重新举个例子啊!
      

  3.   

    我晕了,兴趣一返三啊~~~~~JTextField txt = new JTextField ();
    txt.setActionCommand("ddddd");
    txt.addActionListener(new ActionListener ()
    {
    public void actionPerformed (ActionEvent arg0)
    {
    System.out.println (arg0.getActionCommand());
    }
    });
      

  4.   

    我在一个网友发的java API里面没找到JTextField.setActionCommand(String)方法!!怎么回事啊?
      

  5.   

    有这个方法的
    void setActionCommand(String command) 
              Sets the command string used for action events.