You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于JPanel的触屏键盘Shift与Caps键功能实现求助

触屏键盘Shift和Caps键实现方案

问题背景

正在为触屏应用开发Java Swing触屏键盘,现有代码已实现5×12布局的键盘,支持基础输入和回车输出,但Shift、Caps键功能未实现,需基于现有代码修改完成这两个键的功能。

实现步骤

  • 添加状态跟踪变量:在类中添加成员变量,记录Shift的单次激活状态、Caps的锁定状态,同时保存原始按键文本,避免切换时丢失初始数据。
  • 定义字符映射规则:建立小写、大写、Shift对应字符的映射关系,方便按键文本和输入字符的切换。
  • 优化事件处理逻辑:修改actionPerformed方法,处理Shift和Caps的状态切换,调用布局更新方法刷新按键显示,并根据当前状态转换输入字符。
  • 实现布局更新方法:编写updateKeyboardLayout方法,根据当前Shift/Caps状态遍历所有按键,更新显示文本。

修改后的完整代码

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Keyboard extends JFrame implements ActionListener {
    private JButton[] buttons;
    private JPanel keyboardPanel;
    public String userInput = "";
    // 保存原始按键文本,用于状态切换后恢复
    private ArrayList<String> originalKeys;
    public ArrayList<String> keyboardKeys = new ArrayList<>(Arrays.asList(
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=",
            "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "\\", "delete",
            "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "enter",
            "shift", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "shift",
            "caps", "tab", "space", "`", "~", "!", "@", "(", ")", "?", "[", "]"));
    
    // 状态变量:Shift单次激活,Caps锁定
    private boolean isShiftActive = false;
    private boolean isCapsLocked = false;
    
    // 字符映射:小写 -> 大写,小写 -> Shift对应字符
    private Map<String, String> lowerToUpper = new HashMap<>();
    private Map<String, String> lowerToShift = new HashMap<>();

    public Keyboard() {
        super("Keyboard");
        // 初始化原始按键文本副本
        originalKeys = new ArrayList<>(keyboardKeys);
        
        // 初始化字符映射
        initCharMaps();
        
        keyboardPanel = new JPanel();
        keyboardPanel.setLayout(new GridLayout(5, 12));
        buttons = new JButton[60];
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(keyboardKeys.get(i));
            buttons[i].addActionListener(this);
            keyboardPanel.add(buttons[i]);
        }
        add(keyboardPanel, BorderLayout.CENTER);
        setSize(900, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    private void initCharMaps() {
        // 字母大小写映射
        String[] lowers = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"};
        String[] uppers = {"Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"};
        for(int i=0; i<lowers.length; i++){
            lowerToUpper.put(lowers[i], uppers[i]);
        }
        
        // 数字/符号Shift映射
        lowerToShift.put("1", "!");
        lowerToShift.put("2", "@");
        lowerToShift.put("3", "#");
        lowerToShift.put("4", "$");
        lowerToShift.put("5", "%");
        lowerToShift.put("6", "^");
        lowerToShift.put("7", "&");
        lowerToShift.put("8", "*");
        lowerToShift.put("9", "(");
        lowerToShift.put("0", ")");
        lowerToShift.put("-", "_");
        lowerToShift.put("=", "+");
        lowerToShift.put("\\", "|");
        lowerToShift.put(";", ":");
        lowerToShift.put("'", "\"");
        lowerToShift.put(",", "<");
        lowerToShift.put(".", ">");
        lowerToShift.put("/", "?");
        lowerToShift.put("`", "~");
        lowerToShift.put("[", "{");
        lowerToShift.put("]", "}");
    }
    
    private void updateKeyboardLayout() {
        for(int i=0; i<buttons.length; i++){
            String originalText = originalKeys.get(i);
            String displayText = originalText;
            
            // 处理字母按键:Caps锁定时显示大写,Shift激活时也显示大写(优先级:Shift > Caps)
            if(lowerToUpper.containsKey(originalText)){
                if(isShiftActive || isCapsLocked){
                    displayText = lowerToUpper.get(originalText);
                }
            }
            // 处理数字/符号按键:Shift激活时显示Shift对应字符
            else if(lowerToShift.containsKey(originalText) && isShiftActive){
                displayText = lowerToShift.get(originalText);
            }
            
            buttons[i].setText(displayText);
        }
    }

    public void actionPerformed(ActionEvent event) {
        JButton source = (JButton) event.getSource();
        int buttonIndex = findButtonIndex(source);
        String originalText = originalKeys.get(buttonIndex);
        
        if(originalText.equals("shift"))
        {
            // Shift键:切换单次激活状态,更新布局
            isShiftActive = !isShiftActive;
            updateKeyboardLayout();
        }
        else if(originalText.equals("caps"))
        {
             // Caps键:切换锁定状态,更新布局
             isCapsLocked = !isCapsLocked;
             // 按下Caps时自动取消Shift激活状态
             isShiftActive = false;
             updateKeyboardLayout();
        }
        else if(originalText.equals("enter"))
        {
            System.out.println(userInput);
            userInput = "";
        }
        else if(originalText.equals("space"))
        {
            userInput += " ";
            // 按普通键后取消Shift激活状态
            isShiftActive = false;
            updateKeyboardLayout();
        }
        else if(originalText.equals("delete"))
        {
            if(userInput.length() > 0){
                userInput = userInput.substring(0, userInput.length()-1);
            }
            isShiftActive = false;
            updateKeyboardLayout();
        }
        else if(originalText.equals("tab"))
        {
            userInput += "\t";
            isShiftActive = false;
            updateKeyboardLayout();
        }
        else
        {
            // 根据当前状态获取输入字符
            String inputChar = originalText;
            if(isShiftActive){
                // Shift激活时,优先取Shift映射,没有则取大写
                if(lowerToShift.containsKey(originalText)){
                    inputChar = lowerToShift.get(originalText);
                } else if(lowerToUpper.containsKey(originalText)){
                    inputChar = lowerToUpper.get(originalText);
                }
                // 单次Shift后自动取消激活
                isShiftActive = false;
            } else if(isCapsLocked && lowerToUpper.containsKey(originalText)){
                inputChar = lowerToUpper.get(originalText);
            }
            
            userInput += inputChar;
            updateKeyboardLayout();
        }
    }
    
    // 根据按钮找对应索引
    private int findButtonIndex(JButton button){
        for(int i=0; i<buttons.length; i++){
            if(buttons[i] == button){
                return i;
            }
        }
        return -1;
    }

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

功能说明

  • Shift键:单次激活,按下后按键显示切换为Shift对应字符/大写字母,输入一次后自动恢复原状态;若Caps处于锁定状态,按下Shift临时切换为小写输入。
  • Caps键:锁定激活,按下后字母按键固定显示大写,再次按下取消锁定;按下Caps时自动取消Shift激活状态。
  • Delete、Tab键也已实现基础功能,符合触屏键盘使用逻辑。

内容的提问来源于stack exchange,提问作者Zee Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:37:25