码迷,mamicode.com
首页 > 其他好文 > 详细

键盘事件KeyListener( )

时间:2018-08-13 20:59:23      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:component   相等   you   地方   文本框   无法   为什么   强制转换   nta   

说明:按下实体按键,相应虚拟按键变绿色,释放按键,变白色。

技术分享图片 技术分享图片

public class Demo extends JFrame {

    private List<JButton> list;//定义变量,否则自定义方法类中,不识别
    
    //    JPanel p1; 边界布局,中心区域放置文本框tf
//    JPanel p2; 绝对布局,放置面板p21,p22,p23
//    JPanel p21; 流布局,第一行键盘按钮
//    JPanel p22; 流布局,第二行键盘按钮
//    JPanel p23; 流布局,第三行键盘按钮
    public Demo() {
        //设置窗体大小,不可更改。
        setBounds(100, 100, 548, 280);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container c = getContentPane();
        c.setLayout(null);//绝对布局,上下两个Jpanel面板
//p1 边界布局,中心区域放置文本框tf
        //p2 绝对布局,放置面板p21,p22,p23
        //p21 流布局,第一行键盘按钮
        //p22 流布局,第二行键盘按钮
        //p23 流布局,第三行键盘按钮
        JPanel p1 = new JPanel();
        p1.setBounds(0, 0, 540, 45);
        p1.setBackground(Color.WHITE);
        p1.setBorder(BorderFactory.createTitledBorder("文本显示区"));
        p1.setLayout(new BorderLayout());//边界布局
        JTextField tf = new JTextField();
        p1.add(tf, BorderLayout.CENTER);//文本框放中间区域
        c.add(p1);
        //面板2
        //绝对布局,放置面板p21,p22,p23
        JPanel p2 = new JPanel();
        p2.setBounds(0, 45, 540, 205);
        p2.setBackground(Color.WHITE);
        p2.setLayout(null);
        c.add(p2);
        //面板21
        //流布局,第一行键盘按钮
        JPanel p21 = new JPanel();
        p21.setBounds(0, 10, 540, 55);
        p21.setLayout(new FlowLayout());
        p2.add(p21);
        //面板22
        //流布局,第二行键盘按钮
        JPanel p22 = new JPanel();
        p22.setBounds(0, 75, 540, 55);
        p22.setLayout(new FlowLayout());
        p2.add(p22);
        //面板23
        //流布局,第三行键盘按钮
        JPanel p23 = new JPanel();
        p23.setBounds(0, 140, 540, 55);
        p23.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
        p2.add(p23);
        /**按钮*/
        JButton btnQ = new JButton("Q");
        btnQ.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnQ.setBackground(Color.WHITE);
        //btnQ.setSize(45,45);//在一些布局中,setSize失效,不起作用。
        btnQ.setPreferredSize(new Dimension(45, 45));
        p21.add(btnQ);

        JButton btnW = new JButton("W");
        btnW.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnW.setBackground(Color.WHITE);
        btnW.setPreferredSize(new Dimension(46, 45));
        p21.add(btnW);

        JButton btnE = new JButton("E");
        btnE.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnE.setBackground(Color.WHITE);
        btnE.setPreferredSize(new Dimension(45, 45));
        p21.add(btnE);

        JButton btnR = new JButton("R");
        btnR.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnR.setBackground(Color.WHITE);
        btnR.setPreferredSize(new Dimension(45, 45));
        p21.add(btnR);

        JButton btnT = new JButton("T");
        btnT.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnT.setBackground(Color.WHITE);
        btnT.setPreferredSize(new Dimension(45, 45));
        p21.add(btnT);

        JButton btnY = new JButton("Y");
        btnY.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnY.setBackground(Color.WHITE);
        btnY.setPreferredSize(new Dimension(45, 45));
        p21.add(btnY);

        JButton btnU = new JButton("U");
        btnU.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnU.setBackground(Color.WHITE);
        btnU.setPreferredSize(new Dimension(45, 45));
        p21.add(btnU);

        JButton btnI = new JButton("I");
        btnI.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnI.setBackground(Color.WHITE);
        btnI.setPreferredSize(new Dimension(45, 45));
        p21.add(btnI);

        JButton btnO = new JButton("O");
        btnO.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnO.setBackground(Color.WHITE);
        btnO.setPreferredSize(new Dimension(45, 45));
        p21.add(btnO);

        JButton btnP = new JButton("P");
        btnP.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnP.setBackground(Color.WHITE);
        btnP.setPreferredSize(new Dimension(45, 45));
        p21.add(btnP);

        JButton btnA = new JButton("A");
        btnA.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnA.setBackground(Color.WHITE);
        btnA.setPreferredSize(new Dimension(45, 45));
        p22.add(btnA);

        JButton btnS = new JButton("S");
        btnS.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnS.setBackground(Color.WHITE);
        btnS.setPreferredSize(new Dimension(45, 45));
        p22.add(btnS);

        JButton btnD = new JButton("D");
        btnD.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnD.setBackground(Color.WHITE);
        btnD.setPreferredSize(new Dimension(45, 45));
        p22.add(btnD);

        JButton btnF = new JButton("F");
        btnF.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnF.setBackground(Color.WHITE);
        btnF.setPreferredSize(new Dimension(45, 45));
        p22.add(btnF);

        JButton btnG = new JButton("G");
        btnG.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnG.setBackground(Color.WHITE);
        btnG.setPreferredSize(new Dimension(45, 45));
        p22.add(btnG);

        JButton btnH = new JButton("H");
        btnH.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnH.setBackground(Color.WHITE);
        btnH.setPreferredSize(new Dimension(45, 45));
        p22.add(btnH);

        JButton btnJ = new JButton("J");
        btnJ.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnJ.setBackground(Color.WHITE);
        btnJ.setPreferredSize(new Dimension(45, 45));
        p22.add(btnJ);

        JButton btnK = new JButton("K");
        btnK.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnK.setBackground(Color.WHITE);
        btnK.setPreferredSize(new Dimension(45, 45));
        p22.add(btnK);

        JButton btnL = new JButton("L");
        btnL.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnL.setBackground(Color.WHITE);
        btnL.setPreferredSize(new Dimension(45, 45));
        p22.add(btnL);

        JButton btnZ = new JButton("Z");
        btnZ.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnZ.setBackground(Color.WHITE);
        btnZ.setPreferredSize(new Dimension(45, 45));
        p23.add(btnZ);

        JButton btnX = new JButton("X");
        btnX.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnX.setBackground(Color.WHITE);
        btnX.setPreferredSize(new Dimension(45, 45));
        p23.add(btnX);

        JButton btnC = new JButton("C");
        btnC.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnC.setBackground(Color.WHITE);
        btnC.setPreferredSize(new Dimension(45, 45));
        p23.add(btnC);

        JButton btnV = new JButton("V");
        btnV.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnV.setBackground(Color.WHITE);
        btnV.setPreferredSize(new Dimension(45, 45));
        p23.add(btnV);

        JButton btnB = new JButton("B");
        btnB.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnB.setBackground(Color.WHITE);
        btnB.setPreferredSize(new Dimension(45, 45));
        p23.add(btnB);

        JButton btnN = new JButton("N");
        btnN.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnN.setBackground(Color.WHITE);
        btnN.setPreferredSize(new Dimension(45, 45));
        p23.add(btnN);

        JButton btnM = new JButton("M");
        btnM.setVerticalAlignment(SwingConstants.TOP);//位于顶部
        btnM.setBackground(Color.WHITE);
        btnM.setPreferredSize(new Dimension(45, 45));
        p23.add(btnM);

        tf.addKeyListener(new MyKeyListener());//调用自定义方法

        setVisible(true);

        /**为什么要用 List list = new ArrayList(),而不用 ArrayList list = new ArrayList()呢?
         List接口有多个实现类,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类,如
         LinkedList或Vector等,这时你只要改变这一行就行了:List list = new LinkedList();
         其它使用了list地方的代码不需要改动。*/
        list = new ArrayList<>();//创建list,用来存放所有按钮控件
        Component[] items1 = p21.getComponents(); //返回容器中所有控件
        Component[] items2 = p22.getComponents();//
        Component[] items3 = p23.getComponents();
        //p2.getComponents()只能识别到上一级子控件,控件中的控件无法识别。
        //创建空的items数组,依次存放三个数组,达到合并数组的目的。
        Component[] items = new Component[items1.length + items2.length + items3.length];
        System.arraycopy(items1, 0, items, 0, items1.length);
        System.arraycopy(items2, 0, items, items1.length, items2.length);
        System.arraycopy(items3, 0, items, items1.length + items2.length, items3.length);

        for (Component item : items) {//找出items数组中的按钮控件
            if (item instanceof JButton) {//如果这个控件是按钮,放入list中
                list.add((JButton) item);//Component类强制转换为JButton类
            }
        }
    }

    //在MyKeyListener上,Alt+Enter,快速创建方法。按下为绿,释放为白
    class MyKeyListener implements KeyListener {

        public void keyTyped(KeyEvent e) {

        }

        public void keyPressed(KeyEvent e) {
            char ch = e.getKeyChar();//获取实体按键的字符
            for (JButton btn : list) {
                String btnText = btn.getText();//获取虚拟按键的字符串
                //判断字符是否相等(忽略大小写)
                if (btnText.equalsIgnoreCase(String.valueOf(ch))) {
                    btn.setBackground(Color.GREEN);
                }
            }
        }

        public void keyReleased(KeyEvent e) {
            char ch = e.getKeyChar();//获取实体按键的字符
            for (JButton btn : list) {
                String btnText = btn.getText();//获取虚拟按键的字符串
                //判断字符是否相等(忽略大小写)
                if (btnText.equalsIgnoreCase(String.valueOf(ch))) {
                    btn.setBackground(Color.WHITE);
                }
            }
        }
/**单独按键,构造方法
 public void keyTyped(KeyEvent e) {//自动生成,不可删除
 }

 public void keyPressed(KeyEvent e) {
 int code = e.getKeyCode();//获得实体键盘上的按键编码,getKeyChar()方法也可以
 switch (code) {
 case KeyEvent.VK_A://如果是实体A键的编码,窗体上的btnA变绿
 btnA.setBackground(Color.GREEN);
 break;
 }
 }

 public void keyReleased(KeyEvent e) {
 int code = e.getKeyCode();
 switch (code) {
 case KeyEvent.VK_A:
 btnA.setBackground(Color.WHITE);
 break;
 }
 }
 */
    }

    public static void main(String[] args) {
        new Demo();
    }
}

 

键盘事件KeyListener( )

标签:component   相等   you   地方   文本框   无法   为什么   强制转换   nta   

原文地址:https://www.cnblogs.com/xixixing/p/9470326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!