import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TtpDemo extends JFrame 
{
TtpDemo()
{
super("卡片选项页面示例");
setSize(300,200);
setVisible(true);
JTabbedPane jtp=new JTabbedPane();
ImageIcon icon1=new ImageIcon("c1.gif");
ImageIcon icon2=new ImageIcon("c2.gif");
ImageIcon icon3=new ImageIcon("c3.gif");
jtp.addTab("城市",icon1,new CitiesPanel(),"城市名称");
jtp.addTab("文学",icon2,new BookPanel(),"文学书目");
jtp.addTab("网站",icon3,new NetPanel(),"精选网址");
getContentPane().add(jtp);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
}
class CitiesPanel extends JPanel implements ActionListener
{
CitiesPanel()
{
JButton b1=new JButton("北京");
JButton b2=new JButton("上海");
JButton b3=new JButton("深圳");
JButton b4=new JButton("厦门");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
add(b1);add(b2);add(b3);add(b4);
}
public void actonPerformed(ActionEvent e)
{
if(e.getSource==b1)
{
String URL="\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.163.com"; 
Process pp=Runtime.getRuntime().exec(URL);
}
if(e.getSource==b2)
{
String URL="\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sina.com"; 
Process pp=Runtime.getRuntime().exec(URL);
}
if(e.getSource==b3)
{
String URL="\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.126.com"; 
Process pp=Runtime.getRuntime().exec(URL);
}
if(e.getSource==b4)
{
String URL="\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sohu.com"; 
Process pp=Runtime.getRuntime().exec(URL);
}
}
class BookPanel extends JPanel
{
BookPanel()
{
JCheckBox cb1=new JCheckBox("西游记");
JCheckBox cb2=new JCheckBox("三国演义");
JCheckBox cb3=new JCheckBox("红楼梦");
add(cb1);add(cb2);add(cb3);
}
}
class NetPanel extends JPanel
{
NetPanel()
{
JComboBox jcb=new JComboBox();
jcb.addItem("思维论坛");
jcb.addItem("百度摸索");
jcb.addItem("爱好者");
add(jcb);
}
}
public class Example5_7
{
public static void main(String args[])
{new TtpDemo();}
}
请问错在哪里呢?
编译时出现:进行语法解释时已到达文件结尾 的错误.

解决方案 »

  1.   

    第一个问题,class CitiesPanel没有}。
    第二个问题,e.getSource应该是e.getSource()
    第三个问题,b1,b2,b3,b4应该定义为类的变量,而不是定义的构造函数里。
    第四个问题,CitiesPanel还应该实现public void actionPerformed(ActionEvent arg0)方法。
      

  2.   

    import java.awt.*;
    import java.awt.event.*;import javax.swing.*;
    class TtpDemo extends JFrame 
    {
        TtpDemo()
        {
            super("卡片选项页面示例");
            setSize(300,200);
            setVisible(true);
            JTabbedPane jtp=new JTabbedPane();
            ImageIcon icon1=new ImageIcon("c1.gif");
            ImageIcon icon2=new ImageIcon("c2.gif");
            ImageIcon icon3=new ImageIcon("c3.gif");
            jtp.addTab("城市",icon1,new CitiesPanel(),"城市名称");
            jtp.addTab("文学",icon2,new BookPanel(),"文学书目");
            jtp.addTab("网站",icon3,new NetPanel(),"精选网址");
            getContentPane().add(jtp);
            validate();
            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {System.exit(0);}
            });
        }
    }
    class CitiesPanel extends JPanel implements ActionListener
    {
        JButton b1=new JButton("北京");
        JButton b2=new JButton("上海");
        JButton b3=new JButton("深圳");
        JButton b4=new JButton("厦门");    CitiesPanel()
        {
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            add(b1);add(b2);add(b3);add(b4);
        }
        public void actonPerformed(ActionEvent e)
        {
         try {
    if (e.getSource() == b1) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.163.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b2) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sina.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b3) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.126.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b4) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sohu.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    }catch(Exception ex){
        
         }
    }
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }
    }
    class BookPanel extends JPanel
    {
        BookPanel()
        {
            JCheckBox cb1=new JCheckBox("西游记");
            JCheckBox cb2=new JCheckBox("三国演义");
            JCheckBox cb3=new JCheckBox("红楼梦");
            add(cb1);add(cb2);add(cb3);
        }
    }
    class NetPanel extends JPanel
    {
        NetPanel()
        {
            JComboBox jcb=new JComboBox();
            jcb.addItem("思维论坛");
            jcb.addItem("百度摸索");
            jcb.addItem("爱好者");
            add(jcb);
        }
    }
    public class Example5_7
    {
        public static void main(String args[])
        {new TtpDemo();}
    }
      

  3.   

    不行哦,编译是过了,可是打不开网页啊?
    能不能解释一下:CitiesPanel还应该实现public void actionPerformed(ActionEvent arg0)方法呢?
      

  4.   

    因为CitiesPanel实现ActionListener接口,所以接口里定义的方法都要实现。这是java的编程规则。
    我这里可以打开了啊。你把:
    }catch(Exception ex){ 
        
        } 
    改成
    }catch(Exception ex){ 
        ex.printStackTrace();

    看看运行的时候抛了什么异常。
      

  5.   

    这个可以用了import java.awt.*;
    import java.awt.event.*;import javax.swing.*;class TtpDemo extends JFrame {
    TtpDemo() {
    super("卡片选项页面示例");
    setSize(300, 200);
    setVisible(true);
    JTabbedPane jtp = new JTabbedPane();
    ImageIcon icon1 = new ImageIcon("c1.gif");
    ImageIcon icon2 = new ImageIcon("c2.gif");
    ImageIcon icon3 = new ImageIcon("c3.gif");
    jtp.addTab("城市", icon1, new CitiesPanel(), "城市名称");
    jtp.addTab("文学", icon2, new BookPanel(), "文学书目");
    jtp.addTab("网站", icon3, new NetPanel(), "精选网址");
    getContentPane().add(jtp);
    validate();
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }
    }class CitiesPanel extends JPanel implements ActionListener {
    JButton b1 = new JButton("北京");
    JButton b2 = new JButton("上海");
    JButton b3 = new JButton("深圳");
    JButton b4 = new JButton("厦门"); CitiesPanel() {
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    } public void actionPerformed(ActionEvent e) {
    try {
    if (e.getSource() == b1) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.163.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b2) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sina.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b3) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.126.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    if (e.getSource() == b4) {
    String URL = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"http://www.sohu.com";
    Process pp = Runtime.getRuntime().exec(URL);
    }
    } catch (Exception ex) { }
    }
    }class BookPanel extends JPanel {
    BookPanel() {
    JCheckBox cb1 = new JCheckBox("西游记");
    JCheckBox cb2 = new JCheckBox("三国演义");
    JCheckBox cb3 = new JCheckBox("红楼梦");
    add(cb1);
    add(cb2);
    add(cb3);
    }
    }class NetPanel extends JPanel {
    NetPanel() {
    JComboBox jcb = new JComboBox();
    jcb.addItem("思维论坛");
    jcb.addItem("百度摸索");
    jcb.addItem("爱好者");
    add(jcb);
    }
    }public class Example5_7 {
    public static void main(String args[]) {
    new TtpDemo();
    }
    }
      

  6.   

    actonPerformed方法应该改为actionPerformed.
      

  7.   

    你是说通过url打开IE的方法吗?这个方法挺好的。以前参与过一个美国生物仪器的软件,也是通过这样来调它的主页的。不过它是从注册表里面取的IE的路径。这样更科学一些。因为windows各个版本的IE路径有可能不太一样。