JtextField getAction()

目录:

  • java源码
  • 效果图

java源码

package textfield;
import java.awt.BorderLayout;
import java.util.Hashtable;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;

public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();

    frame.add(textField, BorderLayout.NORTH);

    Action[] actions = textField.getActions();

    Action cutAction = findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = findAction(actions, DefaultEditorKit.pasteAction);

    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.SOUTH);

    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);

    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);

    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);

    frame.setSize(250, 250);
    frame.setVisible(true);
  }

  public static Action findAction(Action actions[], String key) {
    Hashtable<Object, Action> commands = new Hashtable<Object, Action>();
    for (int i = 0; i < actions.length; i++) {
      Action action = actions[i];
      commands.put(action.getValue(Action.NAME), action);
    }
    return commands.get(key);
  }
}

效果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值