用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

工具栏

2017-12-26 作者: 柯侧耳倾听者举报

[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();
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...