2017年8月10日

Swing 快捷键设置(交互设计,流程图,demo源码)

一、交互设计

(by Dream@帆软)

 

二、流程图

看到交互图,立马开写,然而逻辑比较复杂,大脑混乱。就先画个流程图,理清逻辑。(发现 Mac 上画流程图神器,OmniGraffle)

三、完整代码

基本按照流程图实现。发现流程有 bug,微调了下。

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
import java.util.List;

/**
 * Created by plough on 2017/8/7.
 */
public class HotkeyTest extends JFrame {
    private JTextField textField;
    private String hotkeyStr = "";
    private List<String> hotkeyList = new ArrayList<>();
    private int lastPressedKey = -1;
    private int[] specialKeys;
    private Map<String, Integer> keyWeight;  // 权重

    public HotkeyTest() {

        initSpecialKeys();
        JPanel contentPane = new JPanel(new BorderLayout());
        JLabel label = new JLabel("快捷键测试", JLabel.CENTER);
        contentPane.add(label, BorderLayout.NORTH);

        JPanel textPane = new JPanel();
        textField = new JTextField("直接按键盘进行设置");
        textField.setEditable(false);
        textField.setPreferredSize(new Dimension(300, textField.getPreferredSize().height));
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                doKeyPressed(e.getKeyCode());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                doKeyReleased(e.getKeyCode());
            }
        });
        textPane.add(textField);

        contentPane.add(textPane, BorderLayout.CENTER);

        this.setContentPane(contentPane);
        this.setSize(500, 200);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void initSpecialKeys() {
        specialKeys = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_ALT};
        keyWeight = new HashMap<>();
        // 顺序决定权重,数字越小越靠前
        for (int i = 0; i < specialKeys.length; i++) {
            keyWeight.put(KeyEvent.getKeyText(specialKeys[i])+"+", i);
        }
    }

    public boolean isSpecialKey(int keyCode) {
        for (int key : specialKeys) {
            if (key == keyCode) {
                return true;
            }
        }
        return false;
    }

    public void doKeyPressed(int keyCode) {
        if (lastPressedKey == keyCode || keyCode == 0) {
            return;
        }
        lastPressedKey = keyCode;
        String keyText = KeyEvent.getKeyText(keyCode);
        if (isSpecialKey(keyCode)) {
            if (hotkeyStr.endsWith("+")) {
                hotkeyList.add(keyText + "+");
                // 排序
                hotkeyList.sort((String o1, String o2) -> keyWeight.get(o1) - keyWeight.get(o2));
            } else {
                hotkeyList.clear();
                hotkeyList.add(keyText + "+");
            }
        } else if (keyCode == KeyEvent.VK_BACK_SPACE && !hotkeyStr.endsWith("+")) {
            hotkeyList.clear();
        } else {
            if (!hotkeyStr.endsWith("+")) {
                hotkeyList.clear();
            }
            hotkeyList.add(keyText);
        }
        updateTextField();
    }

    public void doKeyReleased(int keyCode) {
        if (keyCode == lastPressedKey || keyCode == 0) {
            lastPressedKey = -1;
        }
        if (isSpecialKey(keyCode)) {
            if (hotkeyStr.endsWith("+")) {
                hotkeyList.clear();
                updateTextField();
            }
        }
    }

    public void updateTextField() {
        hotkeyStr = String.join("", hotkeyList);
        textField.setText(hotkeyStr);
    }


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

四、运行截图

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注


*