A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈红建 中级黑马   /  2012-8-2 14:43  /  1291 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

 import java.awt.*;import java.awt.event.*; 
 import java.awt.datatransfer.*; 
 @SuppressWarnings("serial") 
 public class simple extends Frame implements 
ActionListener {  
 MenuBar menubar;Menu menu;  
 MenuItem copy,cut,paste;  
 TextArea text1,text2;  
 Clipboard clipboard=null;  
 public void simple (){  
 clipboard = getToolkit().getSystemClipboard();//
获取系统剪贴板  
 menubar=new MenuBar();  
 menu=new Menu("Edit");copy=new MenuItem("
paste");  
 text1=new TextArea(20,20);text2=new TextArea(20
,20);  
 copy.addActionListener(this);cut.addActionListener
(this);  
 paste.addActionListener(this);  
 setLayout(new FlowLayout());  
 menubar.add(menu);  
 menu.add(copy);menu.add(cut);menu.add(paste);
  
 setMenuBar(menubar);  
 add(text1);add(text2);  
 setBounds(100,100,200,250);setVisible(true);pack()
;  
 addWindowListener(new WindowAdapter(){  
 public void windowClosing(WindowEvent e){  
 System.exit(0);  
 }  
 });}  
 public void actionPerformed(ActionEvent e){  
 if(e.getSource()==copy){  
 String temp=text1.getSelectedText();  
 StringSelection text=new StringSelection(temp);  
 clipboard.setContents(text,null);  
 }    
 else if(e.getSource()==cut){  
 String temp=text1.getSelectedText();  
 StringSelection text=new StringSelection(temp);  
 clipboard.setContents(text, null);  
 int start=text1.getSelectionStart();  
 int end=text1.getSelectionEnd();  
 text1.replaceRange("", start, end);  
 }  
 else if(e.getSource()==paste){  
 Transferable contents=clipboard.getContents(this)
;  
 DataFlavor flavor=DataFlavor.stringFlavor;  
 if(contents.isDataFlavorSupported(flavor))  
 try{  
 String str;  
 str=(String)contents.getTransferData(flavor);  
 text2.append(str);  
 }  
 catch(Exception ee){}  
 }  
 }  
 public static void main(String args[]){  
 @SuppressWarnings("unused")  
 simple win=new simple();    
 } 
 }  
 如上代码运行时发生错误如下: 
 D:\core>appletviewer simple.html 
 java.lang.ClassCastException: simple cannot be 
cast to java.applet.Applet 
  at sun.applet.AppletPanel.createApplet(
AppletPanel.java:785)
  at sun.applet.AppletPanel.runLoader(
AppletPanel.java:714)
  at sun.applet.AppletPanel.run(AppletPanel.java
:368)
  at java.lang.Thread.run(Thread.java:619)
 Eclipse 运行错误如下 
 Exception in thread "main" java.lang.
NullPointerException  
 at simple.<init>(simple.java:14)  
 at simple.main(simple.java:56)  
 求高手解答

评分

参与人数 1技术分 +1 收起 理由
杨志 + 1 下次不要只贴代码了!不然真心看不清楚!加.

查看全部评分

3 个回复

倒序浏览
  1. package Test1;

  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.datatransfer.*;

  5. @SuppressWarnings("serial")
  6. public class simple extends Frame implements ActionListener {
  7. MenuBar menubar;
  8. Menu menu;
  9. MenuItem copy, cut, paste;
  10. TextArea text1, text2;
  11. Clipboard clipboard = null;

  12. public void simple() {
  13. clipboard = getToolkit().getSystemClipboard();//

  14. menubar = new MenuBar();
  15. menu = new Menu("Edit");
  16. copy = new MenuItem("paste");
  17. text1 = new TextArea(20, 20);
  18. text2 = new TextArea(20, 20);
  19. copy.addActionListener(this);
  20. cut.addActionListener(this);
  21. paste.addActionListener(this);
  22. setLayout(new FlowLayout());
  23. menubar.add(menu);
  24. menu.add(copy);
  25. menu.add(cut);
  26. menu.add(paste);

  27. setMenuBar(menubar);
  28. add(text1);
  29. add(text2);
  30. setBounds(100, 100, 200, 250);
  31. setVisible(true);
  32. pack();
  33. addWindowListener(new WindowAdapter() {
  34. public void windowClosing(WindowEvent e) {
  35. System.exit(0);
  36. }
  37. });
  38. }

  39. public void actionPerformed(ActionEvent e) {
  40. if (e.getSource() == copy) {
  41. String temp = text1.getSelectedText();
  42. StringSelection text = new StringSelection(temp);
  43. clipboard.setContents(text, null);
  44. } else if (e.getSource() == cut) {
  45. String temp = text1.getSelectedText();
  46. StringSelection text = new StringSelection(temp);
  47. clipboard.setContents(text, null);
  48. int start = text1.getSelectionStart();
  49. int end = text1.getSelectionEnd();
  50. text1.replaceRange("", start, end);
  51. } else if (e.getSource() == paste) {
  52. Transferable contents = clipboard.getContents(this);
  53. DataFlavor flavor = DataFlavor.stringFlavor;
  54. if (contents.isDataFlavorSupported(flavor))
  55. try {
  56. String str;
  57. str = (String) contents.getTransferData(flavor);
  58. text2.append(str);
  59. } catch (Exception ee) {
  60. }
  61. }
  62. }

  63. public static void main(String args[]) {
  64. @SuppressWarnings("unused")
  65. simple win = new simple();
  66. System.out.println(win);
  67. }
  68. }
复制代码
System.out.println(win); 我在这加了一个输出的语句,没有任何错误的代码提示,你看下是不是你运行环境的问题。
希望可以帮到你。

1.jpg (34.08 KB, 下载次数: 4)

1.jpg
回复 使用道具 举报

  1. <P>package Test1;</P>
  2. <P>import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.datatransfer.*;</P>
  5. <P>@SuppressWarnings("serial")
  6. public class simple extends Frame implements ActionListener {
  7. MenuBar menubar;
  8. Menu menu;
  9. MenuItem copy, cut, paste;
  10. TextArea text1, text2;
  11. Clipboard clipboard = null;</P>
  12. <P> public simple() {
  13. <FONT color=red>/*出现你这个错误一般情况下是 pulic 后面的void被你取消了,因为eclipse中会有提示,估计是你点错了。*/
  14. </FONT>  clipboard = getToolkit().getSystemClipboard();//</P>
  15. <P>  menubar = new MenuBar();
  16.   menu = new Menu("Edit");
  17.   copy = new MenuItem("paste");
  18.   text1 = new TextArea(20, 20);
  19.   text2 = new TextArea(20, 20);
  20.   copy.addActionListener(this);
  21.   cut.addActionListener(this);
  22.   paste.addActionListener(this);
  23.   setLayout(new FlowLayout());
  24.   menubar.add(menu);
  25.   menu.add(copy);
  26.   menu.add(cut);
  27.   menu.add(paste);</P>
  28. <P>  setMenuBar(menubar);
  29.   add(text1);
  30.   add(text2);
  31.   setBounds(100, 100, 200, 250);
  32.   setVisible(true);
  33.   pack();
  34.   addWindowListener(new WindowAdapter() {
  35.    public void windowClosing(WindowEvent e) {
  36.     System.exit(0);
  37.    }
  38.   });
  39. }</P>
  40. <P> public void actionPerformed(ActionEvent e) {
  41.   if (e.getSource() == copy) {
  42.    String temp = text1.getSelectedText();
  43.    StringSelection text = new StringSelection(temp);
  44.    clipboard.setContents(text, null);
  45.   } else if (e.getSource() == cut) {
  46.    String temp = text1.getSelectedText();
  47.    StringSelection text = new StringSelection(temp);
  48.    clipboard.setContents(text, null);
  49.    int start = text1.getSelectionStart();
  50.    int end = text1.getSelectionEnd();
  51.    text1.replaceRange("", start, end);
  52.   } else if (e.getSource() == paste) {
  53.    Transferable contents = clipboard.getContents(this);
  54.    DataFlavor flavor = DataFlavor.stringFlavor;
  55.    if (contents.isDataFlavorSupported(flavor))
  56.     try {
  57.      String str;
  58.      str = (String) contents.getTransferData(flavor);
  59.      text2.append(str);
  60.     } catch (Exception ee) {
  61.     }
  62.   }
  63. }</P>
  64. <P> public static void main(String args[]) {
  65.   @SuppressWarnings("unused")
  66.   simple win = new simple();
  67.   System.out.println(win);
  68. }
  69. }
  70. </P>
复制代码
取消之后的错误就是跟你一样的了。
希望可以帮到你。

2.jpg (91.2 KB, 下载次数: 4)

2.jpg

1.jpg (37.96 KB, 下载次数: 4)

1.jpg

评分

参与人数 1技术分 +1 收起 理由
杨志 + 1

查看全部评分

回复 使用道具 举报
标题求加分啊  好不容易研究明白了 谢谢楼上
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马