Swing怎么实现点击一个按钮,自动复制jtextarea里面的文本? RT点击一个按钮后,就把jtextarea里面的文本信息复制一下,相当于选中后CTRL+C? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给按钮加个ActionListener,再加一个String,比如String copyText ,点按钮后copyText = JTextArea.getText(); 你说的剪贴板功能啊,看看Clipboard类把,顺便给你个例子:import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextArea;public class CutAndPaste extends JFrame { private JMenuBar mb = new JMenuBar(); private JMenu edit = new JMenu("Edit"); private JMenuItem cut = new JMenuItem("Cut"), copy = new JMenuItem("Copy"), paste = new JMenuItem("Paste"); private JTextArea text = new JTextArea(20, 20); private Clipboard clipbd = getToolkit().getSystemClipboard(); public CutAndPaste() { cut.addActionListener(new CutL()); copy.addActionListener(new CopyL()); paste.addActionListener(new PasteL()); edit.add(cut); edit.add(copy); edit.add(paste); mb.add(edit); setJMenuBar(mb); getContentPane().add(text); } class CopyL implements ActionListener { public void actionPerformed(ActionEvent e) { String selection = text.getSelectedText(); if (selection == null) return; StringSelection clipString = new StringSelection(selection); clipbd.setContents(clipString, clipString); } } class CutL implements ActionListener { public void actionPerformed(ActionEvent e) { String selection = text.getSelectedText(); if (selection == null) return; StringSelection clipString = new StringSelection(selection); clipbd.setContents(clipString, clipString); text.replaceRange("", text.getSelectionStart(), text .getSelectionEnd()); } } class PasteL implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable clipData = clipbd.getContents(CutAndPaste.this); try { String clipString = (String) clipData .getTransferData(DataFlavor.stringFlavor); text.replaceRange(clipString, text.getSelectionStart(), text .getSelectionEnd()); } catch (Exception ex) { System.err.println("Not String flavor"); } } } public static void main(String[] args) { run(new CutAndPaste(), 300, 200); } public static void run(JFrame frame, int width, int height) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); }} 简单的JAVA问题 问个和反射有关的问题 POI操作excel,如何删除或隐藏一个sheet? 请教各位Integer.toHexString(byteArray[i]&0xFF)的含义??? 最简单的问题我不会:怎样将字母转换成ascii比较大小? 如何使JAVA程序在没有JRE平台的机器上运行 高手来帮帮忙,在线等!(30分) 很简单的一个问题:线程之间的简单通信 java api 也是类库中的一部分。对否? 关于aptana插件的安装问题 如何将String a="92,95" 转换成字符串数组形式String[] b={"92","95"} java如何文本修改写入
点按钮后copyText = JTextArea.getText();
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;public class CutAndPaste extends JFrame {
private JMenuBar mb = new JMenuBar(); private JMenu edit = new JMenu("Edit"); private JMenuItem cut = new JMenuItem("Cut"), copy = new JMenuItem("Copy"),
paste = new JMenuItem("Paste"); private JTextArea text = new JTextArea(20, 20); private Clipboard clipbd = getToolkit().getSystemClipboard(); public CutAndPaste() {
cut.addActionListener(new CutL());
copy.addActionListener(new CopyL());
paste.addActionListener(new PasteL());
edit.add(cut);
edit.add(copy);
edit.add(paste);
mb.add(edit);
setJMenuBar(mb);
getContentPane().add(text);
} class CopyL implements ActionListener {
public void actionPerformed(ActionEvent e) {
String selection = text.getSelectedText();
if (selection == null)
return;
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
}
} class CutL implements ActionListener {
public void actionPerformed(ActionEvent e) {
String selection = text.getSelectedText();
if (selection == null)
return;
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
text.replaceRange("", text.getSelectionStart(), text
.getSelectionEnd());
}
} class PasteL implements ActionListener {
public void actionPerformed(ActionEvent e) {
Transferable clipData = clipbd.getContents(CutAndPaste.this);
try {
String clipString = (String) clipData
.getTransferData(DataFlavor.stringFlavor);
text.replaceRange(clipString, text.getSelectionStart(), text
.getSelectionEnd());
} catch (Exception ex) {
System.err.println("Not String flavor");
}
}
} public static void main(String[] args) {
run(new CutAndPaste(), 300, 200);
} public static void run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
}