日期:2014-05-20  浏览次数:20765 次

JFrame中无法显示图像界面
public class JEditorPane1 {

public static void main(String[] args) 
{
new LinkWin();
}

}
class LinkWin extends JFrame implements ActionListener,Runnable,HyperlinkListener
{ Button button ;
  URL url;
  TextField text;
  JEditorPane editPane;
  byte b[]=new byte[118];
  Thread thread;
  public LinkWin()
  { text=new TextField(20);
  editPane=new JEditorPane();
  editPane.setEditable(false);
  editPane.add(this);
button=new Button("确定");
button.addActionListener(this);
thread=new Thread(this);  
Panel p=new Panel();
p.add(new Label("输入网址:"));
p.add(text);
p.add(button);
ScrollPane scroll=new ScrollPane();
scroll.add(editPane);
add(scroll,BorderLayout.CENTER);
add(p,BorderLayout.NORTH);
setBounds(160,60,360,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
}

  
 public void hyperlinkUpdate(HyperlinkEvent e)
  { if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
  { try{editPane.setPage(e.getURL());} 
  catch(Exception ee) { text.setText(""+url);}
 
  }
  }

 public void actionPerformed(ActionEvent e)
  {
if(!thread.isAlive())
  { thread=new Thread(this);
 
  }
  try
  { thread.start();
  }
  catch(Exception ee)
  { text.setText("正在读取"+url);
  }
  }
 public void run()
 {try
  { editPane.setText(null);
url=new URL(text.getText().trim());
editPane.setPage(url);
  }
  catch(Exception el){text.setText(""+el);return;}
  
 }
}
上面的程序是JEditor对象显示URL资源,导入的类这儿被我省了,运行程序时出现:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:483)
at java.awt.Container.addImpl(Container.java:1084)
at java.awt.Container.add(Container.java:410)
at javaweb.LinkWin.<init>(JEditorPane1.java:39)
at javaweb.JEditorPane1.main(JEditorPane1.java:24)
,这儿无法显示图像界面,这儿有JFrame容器,怎么还需要窗口??

------解决方案--------------------
editPane.add(this);
??
这句话去掉。怎么在editPanel里add JFrame呢。