[java]代码库
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class JToolBarTest
{
JFrame jf = new JFrame("测试工具条");
JTextArea jta = new JTextArea(6, 35);
JToolBar jtb = new JToolBar();
JMenuBar jmb = new JMenuBar();
Clipboard clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
Action pasteAction = new AbstractAction("粘贴"
, new ImageIcon("ico/paste.png"))
{
public void actionPerformed(ActionEvent e)
{
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))
{
try
{
String content = (String)clipboard.getData
(DataFlavor.stringFlavor);
jta.replaceRange(content , jta.getSelectionStart()
, jta.getSelectionEnd());
}
catch (Exception ee)
{
ee.printStackTrace();
}
}
}
};
Action copyAction = new AbstractAction("复制"
, new ImageIcon("ico/copy.png"))
{
public void actionPerformed(ActionEvent e)
{
StringSelection contents = new StringSelection(
jta.getSelectedText());
clipboard.setContents(contents, null);
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))
{
pasteAction.setEnabled(true);
}
}
};
public void init()
{
pasteAction.setEnabled(false); // ①
jf.add(new JScrollPane(jta));
JButton copyBn = new JButton(copyAction);
JButton pasteBn = new JButton(pasteAction);
JPanel jp = new JPanel();
jp.add(copyBn);
jp.add(pasteBn);
jf.add(jp , BorderLayout.SOUTH);
jtb.add(copyAction);
jtb.addSeparator();
jtb.add(pasteAction);
edit.add(copyAction);
edit.add(pasteAction);
jmb.add(edit);
jf.setJMenuBar(jmb);
jtb.setMargin(new Insets(20 ,10 , 5 , 30));
jf.add(jtb , BorderLayout.NORTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args)
{
new JToolBarTest().init();
}
}
by: 发表于:2018-01-04 11:23:40 顶(0) | 踩(0) 回复
??
回复评论