- <P>package Test1;</P>
- <P>import java.awt.*;
- import java.awt.event.*;
- import java.awt.datatransfer.*;</P>
- <P>@SuppressWarnings("serial")
- public class simple extends Frame implements ActionListener {
- MenuBar menubar;
- Menu menu;
- MenuItem copy, cut, paste;
- TextArea text1, text2;
- Clipboard clipboard = null;</P>
- <P> public simple() {
- <FONT color=red>/*出现你这个错误一般情况下是 pulic 后面的void被你取消了,因为eclipse中会有提示,估计是你点错了。*/
- </FONT> clipboard = getToolkit().getSystemClipboard();//</P>
- <P> 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);</P>
- <P> 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);
- }
- });
- }</P>
- <P> 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) {
- }
- }
- }</P>
- <P> public static void main(String args[]) {
- @SuppressWarnings("unused")
- simple win = new simple();
- System.out.println(win);
- }
- }
- </P>
复制代码 取消之后的错误就是跟你一样的了。
希望可以帮到你。
|
-
2.jpg
(91.2 KB, 下载次数: 6)
-
1.jpg
(37.96 KB, 下载次数: 5)
|