Java设置快捷键

/**
 *
 */
package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import test.GetKey;

;
/**
 * @author cyl
 */

//在JTextArea中输入B的快捷键,然后点击A,则B的快捷键生效。
public class model extends JPanel
{
 GetKey g = new GetKey();

 JButton jb1 = new JButton("a");

 JButton jb2 = new JButton("b");

 JTextArea jt1 = new JTextArea("wo lai la!");
 
 public void ac()
 
 {
  jb2.setToolTipText(g.format(jt1.getText()));
  jb2.setText("[" + g.format(jt1.getText()) + "]");
  KeyStroke bsearchStk_stroke = KeyStroke.getKeyStroke(g.dealString(jt1.getText())[2], g.dealString(jt1.getText())[1]);
  //KeyStroke bsearchStk_stroke = KeyStroke.getKeyStroke("HOME");
  this.registerKeyboardAction(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    jb2.doClick();
   }
  }, bsearchStk_stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
 }
 //居中
 public static void centerWindow(Component component)
 {
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  System.out.println(screenSize.getWidth() + "   " + screenSize.getHeight());
  Dimension componentSize = component.getSize();
  if (componentSize.height > screenSize.height)
  {
   componentSize.height = screenSize.height;
  }
  if (componentSize.width > screenSize.width)
  {
   componentSize.width = screenSize.width;
  }
  component.setLocation((screenSize.width - componentSize.width) / 2, (screenSize.height - componentSize.height) / 2);
  component.setVisible(true);
 }

 {
  setLayout(new BorderLayout());
  jb1.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    ac();
   }
  });
  
  jb2.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    JOptionPane.showConfirmDialog(null,"message");
   }
  });
  this.add(jb1, BorderLayout.NORTH);
  this.add(jt1, BorderLayout.CENTER);
  this.add(jb2, BorderLayout.SOUTH);
  this.setSize(new Dimension(400, 600));
  centerWindow(this);
 }

 public static void main(String[] args)
 {
  JFrame jf=new JFrame();
  model m=new model();
  jf.add(m);
  jf.setVisible(true);
 }
}


package test;

/**
 * convert key which customer can customize hotkey(shotcut) for general application using in setting shotcut.(CASE INSENSITIVE,follow as also)<br>
 * maintain single character such as:F,G<br>
 * maintain key combination:such as:ctrl+f,alt+f,shift+f and so on<br>
 * maintain fuction key:such as:F1~F12<br>
 *
 * @author:cyl
 * @version: 1.0
 */
public class GetKey
{
 /**
  * @function name:Convert
  * @param:Character
  * @return:int
  * @function:convert character to int
  */
 public int Convert(Character c)
 {
  return c.charValue();
 }

 /**
  * @函数名:dealString
  * @param:String args
  * @return:int[]
  * @fuction:处理字符串,并将其转换为整形数组 result[1]:存储功能键,默认值为0 result[2]:存储键盘字符所对应的键值
  */
 public int[] dealString(String args)
 {
  int[] result = new int[3];
  args = format(args);
  int length = args.length();
  if (length > 1)
  {
   if (args.contains("+"))
   {
    String sub1 = args.substring(0, args.indexOf("+"));
    String sub2 = args.substring(args.indexOf("+") + 1);
    // System.out.print("sub1 + sub2 : " + sub1 + " /t " + sub2 + "/t");
    result[0] = 2;
    if (sub1.equals("SHIFT") || sub1.equals("CTRL") || sub1.equals("ALT"))
    {
     if (sub1.equals("SHIFT"))
     {
      result[1] = 1;
     }
     if (sub1.equals("CTRL"))
     {
      result[1] = 2;
     }
     if (sub1.equals("ALT"))
     {
      result[1] = 8;
     }
     /*
      * System.out.println("args : " + args + "/n"); System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
      */
    }
    else
    {
     /* System.out.print("error!"); */
    }
    if (sub2.length() > 1)
    {
     if (sub2.equals("HOME"))
     {
      result[2] = 36;
     }
     else if (sub2.equals("END"))
     {
      result[2] = 35;
     }
     else if (sub2.equals("INSERT"))
     {
      result[2] = 155;
     }
     else if (sub2.equals("DELETE"))
     {
      result[2] = 127;
     }
     else if (sub2.equals("PAGE_UP"))
     {
      result[2] = 33;
     }
     else if (sub2.equals("PAGE_DOWN"))
     {
      result[2] = 34;
     }
     else if(sub2.equals("ESC"))
     {
      result[2]=27;
     }
     else if (sub2.startsWith("F"))
     {
      String sub2_temp = sub2.substring(1);
      //System.out.print("sub2_temp is :" + sub2_temp + "/n");
      if (IsInt(sub2_temp))
      {
       result[2] = 111 + Integer.parseInt(sub2_temp);
      }
      else
      {
       // System.out.println("can't convert to int because temp contain illegal charater!");
      }
      // System.out.println("args : " + args + "/n"); System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
     }
     else
     {
      /* System.out.print("input data error!"); */
     }
     //System.out.println("args : " + args + "/n");
     //System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
    }
    else
    {
     result[2] = sub2.charAt(0);
    }
   }
   else
   {
    result[0] = 1;
    result[1] = 0;
    if (args.equals("HOME"))
    {
     result[2] = 36;
    }
    else if (args.equals("END"))
    {
     result[2] = 35;
    }
    else if (args.equals("INSERT"))
    {
     result[2] = 155;
    }
    else if (args.equals("DELETE"))
    {
     result[2] = 127;
    }
    else if (args.equals("PAGE_UP"))
    {
     result[2] = 33;
    }
    else if (args.equals("PAGE_DOWN"))
    {
     result[2] = 34;
    }
    else if(args.equals("ESC"))
    {
     result[2]=27;
    }
    else if (args.startsWith("F"))
    {
     String temp = args.substring(1);
     //System.out.print("temp is :" + temp + "/n");
     if (IsInt(temp))
     {
      result[2] = 111 + Integer.parseInt(temp);
     }
     else
     {
      // System.out.println("can't convert to int because temp contain illegal charater!");
     }
     // System.out.println("args : " + args + "/n"); System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
    }
    else
    {
     /* System.out.print("input data error!"); */
    }
    //System.out.println("args : " + args + "/n");
    //System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
   }
  }
  else
  {
   result[0] = 1;
   result[1] = 0;
   result[2] = args.charAt(0);
   //System.out.println("args : " + args);
   //System.out.print("result : " + result[0] + " " + result[1] + " " + result[2]);
  }
  return result;
 }

 public boolean IsInt(String args)
 {
  for (int i = 0; i < args.length(); i++)
  {
   if (args.charAt(i) < 48 || args.charAt(i) > 57)
   {
    return false;
   }
  }
  return true;
 }

 public String format(String args)
 {
  return args.replaceAll(" ", "").toUpperCase();
 }

 public static void main(String[] args)
 {
  GetKey g = new GetKey();
  // System.out.println("/n test dealString: " + g.dealString(" F ") + "/n");
  // System.out.println(" test dealString: " + g.dealString(" c tr l + F "));
  //System.out.println(" test dealString: " + g.dealString(" F 5 "));
  //System.out.println(" test dealString: " + g.dealString(" home"));
 }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值