Swing之QQ软键盘

2024-06-01 06:38
文章标签 qq swing 软键盘

本文主要是介绍Swing之QQ软键盘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

完全实现QQ登录界面的软件盘功能!

直接上代码,以下代码非常完整,copy后能直接运行:

[java]  view plain copy
  1. package swing.component.popup;  
  2.   
  3. import java.awt.AlphaComposite;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Composite;  
  7. import java.awt.Container;  
  8. import java.awt.Cursor;  
  9. import java.awt.Dimension;  
  10. import java.awt.FlowLayout;  
  11. import java.awt.Font;  
  12. import java.awt.GradientPaint;  
  13. import java.awt.Graphics;  
  14. import java.awt.Graphics2D;  
  15. import java.awt.GridLayout;  
  16. import java.awt.Image;  
  17. import java.awt.Paint;  
  18. import java.awt.Polygon;  
  19. import java.awt.RenderingHints;  
  20. import java.awt.RenderingHints.Key;  
  21. import java.awt.event.ActionEvent;  
  22. import java.awt.event.ActionListener;  
  23. import java.awt.event.MouseAdapter;  
  24. import java.awt.event.MouseEvent;  
  25. import java.awt.image.BufferedImage;  
  26. import java.util.ArrayList;  
  27. import java.util.Arrays;  
  28. import java.util.Random;  
  29.   
  30. import javax.swing.BorderFactory;  
  31. import javax.swing.ImageIcon;  
  32. import javax.swing.JFrame;  
  33. import javax.swing.JLabel;  
  34. import javax.swing.JPanel;  
  35. import javax.swing.JPasswordField;  
  36. import javax.swing.JPopupMenu;  
  37. import javax.swing.JToolTip;  
  38. import javax.swing.border.Border;  
  39. import javax.swing.border.CompoundBorder;  
  40. import javax.swing.border.EmptyBorder;  
  41.   
  42. import sun.swing.SwingUtilities2;  
  43.   
  44. //软键盘弹出菜单  
  45. public class SoftKeyBoardPopup extends JPopupMenu {  
  46.   
  47.     private static final long serialVersionUID = 1L;  
  48.   
  49.     public static void main(String[] args) {  
  50.   
  51.         final JFrame frame = new JFrame();  
  52.         frame.setLayout(new FlowLayout(FlowLayout.CENTER, 2020));  
  53.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  54.         frame.setSize(500500);  
  55.         frame.setLocationRelativeTo(null);  
  56.   
  57.         final JPanel passwordPanel = new JPanel(new BorderLayout());  
  58.         passwordPanel.setBackground(Color.WHITE);  
  59.         passwordPanel.setPreferredSize(new Dimension(20230));  
  60.         passwordPanel.setLayout(new BorderLayout());  
  61.         passwordPanel.setBorder(BorderFactory.createEmptyBorder(3333));  
  62.   
  63.         frame.add(passwordPanel);  
  64.   
  65.         final JPasswordField password = new JPasswordField();  
  66.         password.setSelectedTextColor(Color.BLACK);// 颜色障眼法,产生不能去选中密码框的任何东西的幻觉,其实已经选中了但你不知道  
  67.         password.setSelectionColor(Color.WHITE);// 颜色障眼法,产生不能去选中密码框的任何东西的幻觉,其实已经选中了但你不知道  
  68.         password.setForeground(Color.BLACK);  
  69.         password.setFont(password.getFont().deriveFont(22f));  
  70.         // password.setEchoChar('●');  
  71.         password.setBorder(new EmptyBorder(5303));// 左间隙  
  72.         passwordPanel.add(password, BorderLayout.CENTER);  
  73.   
  74.         final SoftKeyBoardPopup keyPopup = new SoftKeyBoardPopup(password);  
  75.         final JLabel keyBoard = new JLabel("软件盘");  
  76.         keyBoard.setOpaque(true);  
  77.         keyBoard.setBackground(Color.WHITE);  
  78.         keyBoard.setBorder(new EmptyBorder(0000));  
  79.         keyBoard.setToolTipText("打开软键盘");  
  80.         keyBoard.setPreferredSize(new Dimension(4223));  
  81.         keyBoard.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
  82.   
  83.         keyBoard.addMouseListener(new MouseAdapter() {  
  84.             public void mouseClicked(MouseEvent e) {  
  85.                 if (!keyPopup.isVisible()) {  
  86.                     keyPopup.show(passwordPanel, 0, passwordPanel.getPreferredSize().height);  
  87.                     keyPopup.getSoftKeyBoardPanel().reset();  
  88.                     keyPopup.repaint();  
  89.                 }  
  90.             }  
  91.         });  
  92.         passwordPanel.add(keyBoard, BorderLayout.EAST);  
  93.   
  94.         frame.setVisible(true);  
  95.     }  
  96.   
  97.     private static Color transparentColor = new Color(2552552550);  
  98.     // private static Dimension popupSize = new Dimension(360, 110);//QQ软键盘大小  
  99.     private static Dimension popupSize = new Dimension(365110);  
  100.     private static Color backColor = new Color(23127194);  
  101.     private static Random random = new Random();  
  102.   
  103.     protected SoftKeyBoardPanel softKeyBoardPanel;  
  104.   
  105.     public SoftKeyBoardPopup(JPasswordField passwordField) {  
  106.         softKeyBoardPanel = new SoftKeyBoardPanel(passwordField, this);  
  107.         softKeyBoardPanel.setPreferredSize(popupSize);  
  108.         softKeyBoardPanel.setBorder(BorderFactory.createEmptyBorder());  
  109.   
  110.         add(softKeyBoardPanel);  
  111.         setBorder(BorderFactory.createEmptyBorder(0000));// 空边框  
  112.         setOpaque(false);// 透明  
  113.     }  
  114.   
  115.     public static void gc() {  
  116.         popupSize = null;  
  117.         backColor = null;  
  118.         random = null;  
  119.         System.gc();  
  120.     }  
  121.   
  122.     public static void resetValue() {  
  123.         popupSize = new Dimension(380110);  
  124.         backColor = new Color(23127194);  
  125.         random = new Random();  
  126.     }  
  127.   
  128.     public SoftKeyBoardPanel getSoftKeyBoardPanel() {  
  129.         return softKeyBoardPanel;  
  130.     }  
  131.   
  132.     // 软键盘面板  
  133.     public static class SoftKeyBoardPanel extends JPanel implements ActionListener {  
  134.   
  135.         JPasswordField passwordField;  
  136.         JPopupMenu popupMenu;  
  137.         RowPanel[] rows;  
  138.         KeyStatus status = KeyStatus.normal;  
  139.         Paint[] paints = new Paint[] { new Color(7067114), new Color(62192238), new Color(138180231) };  
  140.   
  141.         public SoftKeyBoardPanel(JPasswordField passwordField, JPopupMenu popupMenu) {  
  142.             this.passwordField = passwordField;  
  143.             this.popupMenu = popupMenu;  
  144.             initSoftKeyBoardPanel();  
  145.         }  
  146.   
  147.         // 初始化  
  148.         private void initSoftKeyBoardPanel() {  
  149.             setLayout(null);  
  150.             setBackground(backColor);  
  151.   
  152.             JPanel proxyPanel = new JPanel(new GridLayout(4101));// 4行一列,0水平间隙,1垂直间隙  
  153.             proxyPanel.setBackground(backColor);  
  154.             proxyPanel.setLocation(34);  
  155.             proxyPanel.setSize(popupSize.width - 6, popupSize.height - 7);  
  156.             add(proxyPanel);  
  157.   
  158.             rows = new RowPanel[] { new RowPanel(RowType.first), new RowPanel(RowType.second), new RowPanel(RowType.third), new RowPanel(RowType.fourth) };  
  159.             for (int i = 0; i < rows.length; i++) {  
  160.                 proxyPanel.add(rows[i]);  
  161.             }  
  162.         }  
  163.   
  164.         // 重写paint绘制想要的效果  
  165.         @Override  
  166.         public void paint(Graphics g) {  
  167.             super.paint(g);  
  168.             Graphics2D g2d = (Graphics2D) g;  
  169.             ImageTool.setAntiAliasing(g2d);// 抗锯齿  
  170.             ImageTool.drawRoundRect(g2d, getWidth(), getHeight(), 0null, paints);// 绘制软键盘成圆角和多层颜色边框  
  171.         }  
  172.   
  173.         // 处理所有软键盘点击事件  
  174.         @Override  
  175.         public void actionPerformed(ActionEvent e) {  
  176.             KeyLable keyLable = (KeyLable) e.getSource();  
  177.             if (keyLable.isShift() || keyLable.isCapsLock()) {  
  178.                 boolean pressed = keyLable.isPressed();  
  179.   
  180.                 if (keyLable.isShift()) {  
  181.                     clickShift();  
  182.                 } else if (keyLable.isCapsLock()) {  
  183.                     clickCapsLock();  
  184.                 }  
  185.                 pressed = !pressed;  
  186.                 keyLable.setPressed(pressed);  
  187.   
  188.                 notifyKeyLabel();  
  189.             } else if (keyLable.isBackSpace()) {  
  190.                 clickBackSpace();  
  191.             } else if (keyLable.isCommKey()) {  
  192.                 String key;  
  193.                 if (status == KeyStatus.shift || status == KeyStatus.shiftAndCapsLock) {  
  194.                     key = keyLable.getCenterKey();  
  195.                 } else if (status == KeyStatus.normal || status == KeyStatus.capsLock) {  
  196.                     key = keyLable.getLowerLeftKey() == null ? keyLable.getCenterKey() : keyLable.getLowerLeftKey();  
  197.                 } else {  
  198.                     key = "";  
  199.                 }  
  200.                 clickCommKey(key);  
  201.             }  
  202.         }  
  203.   
  204.         // 通知KeyLabel更新状态  
  205.         public void notifyKeyLabel() {  
  206.             for (RowPanel rowPanel : rows) {  
  207.                 for (KeyLable keyLable : rowPanel.getKeys()) {  
  208.                     keyLable.setStatus(status);  
  209.                 }  
  210.             }  
  211.         }  
  212.   
  213.         // 重置键盘, 清除按压状态,并将键盘恢复至初始状态  
  214.         public void reset() {  
  215.             for (RowPanel rowPanel : rows) {  
  216.                 for (KeyLable keyLable : rowPanel.getKeys()) {  
  217.                     keyLable.reset();  
  218.                 }  
  219.             }  
  220.             status = KeyStatus.normal;  
  221.         }  
  222.   
  223.         // 更改状态  
  224.         public void clickShift() {  
  225.             if (status == KeyStatus.capsLock) {  
  226.                 status = KeyStatus.shiftAndCapsLock;  
  227.             } else if (status == KeyStatus.shiftAndCapsLock) {  
  228.                 status = KeyStatus.capsLock;  
  229.             } else if (status == KeyStatus.shift) {  
  230.                 status = KeyStatus.normal;  
  231.             } else if (status == KeyStatus.normal) {  
  232.                 status = KeyStatus.shift;  
  233.             } else {  
  234.                 status = KeyStatus.normal;  
  235.             }  
  236.         }  
  237.   
  238.         // 更改状态  
  239.         public void clickCapsLock() {  
  240.             if (status == KeyStatus.capsLock) {  
  241.                 status = KeyStatus.normal;  
  242.             } else if (status == KeyStatus.shiftAndCapsLock) {  
  243.                 status = KeyStatus.shift;  
  244.             } else if (status == KeyStatus.shift) {  
  245.                 status = KeyStatus.shiftAndCapsLock;  
  246.             } else if (status == KeyStatus.normal) {  
  247.                 status = KeyStatus.capsLock;  
  248.             } else {  
  249.                 status = KeyStatus.normal;  
  250.             }  
  251.         }  
  252.   
  253.         // 点击了删除键, 删除一个字符  
  254.         public void clickBackSpace() {  
  255.             char[] password = passwordField.getPassword();  
  256.             if (password != null && password.length > 0) {  
  257.                 char[] copyOf = Arrays.copyOf(password, password.length - 1);  
  258.                 passwordField.setText(new String(copyOf));  
  259.                 System.out.println("已删除的字符:" + password[password.length - 1]);  
  260.                 System.out.println("删除后的的密码:" + new String(copyOf));  
  261.             }  
  262.         }  
  263.   
  264.         // 点击了普通的键,添加一个字符  
  265.         public void clickCommKey(String key) {  
  266.             if (key != null) {  
  267.                 if (key.length() > 1) {// 可有可无的检查  
  268.                     key = key.substring(0, key.length() - 1);  
  269.                 }  
  270.                 char[] password = passwordField.getPassword();  
  271.                 String string = (password == null ? "" : new String(password));  
  272.                 passwordField.setText(string + key);  
  273.                 System.out.println("新添加的字符:" + key);  
  274.                 System.out.println("添加后的密码:" + string + key);  
  275.             }  
  276.         }  
  277.   
  278.         public RowPanel[] getRows() {  
  279.             return rows;  
  280.         }  
  281.   
  282.         // 创建关闭图标  
  283.         public Image createCloseImage(Color fontColor, boolean isFocus) {  
  284.             int width = 12;  
  285.             BufferedImage bi = new BufferedImage(width, width, BufferedImage.TYPE_4BYTE_ABGR);  
  286.             Graphics2D g2d = bi.createGraphics();  
  287.   
  288.             ImageTool.setAntiAliasing(g2d);  
  289.   
  290.             // 画背景  
  291.             g2d.setPaint(transparentColor);  
  292.             g2d.fillRect(00, width, width);  
  293.   
  294.             int[] xpoints_1 = { 24810 };  
  295.             int[] ypoints_1 = { 221010 };  
  296.             int npoints_1 = 4;  
  297.             Polygon p_left = new Polygon(xpoints_1, ypoints_1, npoints_1);// 左上角到右下角图标  
  298.   
  299.             int[] xpoints_2 = xpoints_1;  
  300.             int[] ypoints_2 = { 101022 };  
  301.             int npoints_2 = 4;  
  302.             Polygon p_right = new Polygon(xpoints_2, ypoints_2, npoints_2);// 右上角到左下角图标  
  303.   
  304.             if (isFocus) {  
  305.                 g2d.setPaint(new GradientPaint(00, fontColor, 0, width, new Color(fontColor.getRed(), fontColor.getGreen(), fontColor.getBlue(), 50)));  
  306.             } else {  
  307.                 g2d.setPaint(fontColor);  
  308.             }  
  309.             // 画关闭图标("x")  
  310.             g2d.fillPolygon(p_left);  
  311.             g2d.fillPolygon(p_right);  
  312.   
  313.             return bi;  
  314.         }  
  315.   
  316.         public class RowPanel extends JPanel {  
  317.             RowType rowType;  
  318.             KeyLable[] keys;  
  319.   
  320.             public RowPanel(RowType rowType) {  
  321.                 this.rowType = rowType;  
  322.                 initRowPanel();  
  323.             }  
  324.   
  325.             private void initRowPanel() {  
  326.                 setOpaque(true);  
  327.                 setLayout(new FlowLayout(FlowLayout.CENTER, 10));// 水平间隙1,垂直间隙0  
  328.                 setBackground(backColor);  
  329.                 if (rowType == RowType.first) {  
  330.   
  331.                     KeyLable key1 = new KeyLable("!""1", SoftKeyBoardPanel.this);  
  332.                     KeyLable key2 = new KeyLable("@""2", SoftKeyBoardPanel.this);  
  333.                     KeyLable key3 = new KeyLable("#""3", SoftKeyBoardPanel.this);  
  334.                     KeyLable key4 = new KeyLable("$""4", SoftKeyBoardPanel.this);  
  335.                     KeyLable key5 = new KeyLable("%""5", SoftKeyBoardPanel.this);  
  336.                     KeyLable key6 = new KeyLable("^""6", SoftKeyBoardPanel.this);  
  337.                     KeyLable key7 = new KeyLable("&""7", SoftKeyBoardPanel.this);  
  338.                     KeyLable key8 = new KeyLable("*""8", SoftKeyBoardPanel.this);  
  339.                     KeyLable key9 = new KeyLable("(""9", SoftKeyBoardPanel.this);  
  340.                     KeyLable key10 = new KeyLable(")""0", SoftKeyBoardPanel.this);  
  341.                     KeyLable key11 = new KeyLable("~""`", SoftKeyBoardPanel.this);// 这个键的位置随机  
  342.                     KeyLable key12 = new KeyLable("BackSpace"true, SoftKeyBoardPanel.this);// 功能键,位置固定在最右  
  343.                     key12.setPreferredSize(new Dimension(7025));  
  344.   
  345.                     keys = new KeyLable[] { key4, key5, key6, key7, key8, key9, key10, key11, key1, key2, key3, key12 };  
  346.                     ArrayList<KeyLable> keylist = new ArrayList<KeyLable>(keys.length);  
  347.   
  348.                     for (KeyLable key : keys) {  
  349.                         if (key != key11) {// key11排除在外  
  350.                             keylist.add(key);  
  351.                         }  
  352.                     }  
  353.   
  354.                     int randomIndex = random.nextInt(keys.length - 1);// 排除最后一个留给key12的位置  
  355.                     keylist.add(randomIndex, key11);  
  356.   
  357.                     for (KeyLable key : keylist) {  
  358.                         this.add(key);  
  359.                     }  
  360.   
  361.                     // 关闭Label  
  362.                     final Image defaImage = createCloseImage(new Color(138180231), false);  
  363.                     final Image focusImage = createCloseImage(new Color(3090150), true);  
  364.                     final JLabel closeLabel = new JLabel(new ImageIcon(defaImage)) {  
  365.                         JToolTip toolTip;  
  366.   
  367.                         protected void paintComponent(Graphics g) {  
  368.                             super.paintComponent(g);  
  369.                             ImageIcon icon = (ImageIcon) getIcon();  
  370.                             if (icon != null) {  
  371.                                 g.drawImage(icon.getImage(), 001212001212null);  
  372.                             }  
  373.                         }  
  374.   
  375.                         public JToolTip createToolTip() {  
  376.                             JToolTip toolTip = new JToolTip();  
  377.                             Color color = new Color(118118118);  
  378.   
  379.                             toolTip.setComponent(this);  
  380.                             toolTip.setTipText(this.getToolTipText());  
  381.                             toolTip.setBackground(Color.WHITE);  
  382.                             toolTip.setForeground(color);  
  383.                             toolTip.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));  
  384.   
  385.                             Border outside = BorderFactory.createLineBorder(color);  
  386.                             Border inside = BorderFactory.createEmptyBorder(2323);  
  387.                             CompoundBorder border = BorderFactory.createCompoundBorder(outside, inside);  
  388.                             toolTip.setBorder(border);  
  389.                             return toolTip;  
  390.                         }  
  391.                     };  
  392.                     MouseAdapter mouseAdapter = new MouseAdapter() {  
  393.                         public void mouseClicked(MouseEvent e) {  
  394.                             popupMenu.setVisible(false);  
  395.                         }  
  396.   
  397.                         public void mouseEntered(MouseEvent e) {  
  398.                             closeLabel.setIcon(new ImageIcon(focusImage));  
  399.                         }  
  400.   
  401.                         public void mouseExited(MouseEvent e) {  
  402.                             closeLabel.setIcon(new ImageIcon(defaImage));  
  403.                         }  
  404.                     };  
  405.                     closeLabel.setToolTipText("关闭");  
  406.                     closeLabel.addMouseListener(mouseAdapter);  
  407.                     closeLabel.setPreferredSize(new Dimension(1212));  
  408.                     add(closeLabel);  
  409.                 } else if (rowType == RowType.second) {  
  410.                     // key1至key10是一个闭环顺序,通过随机数决定在哪个位置切断这个环从而使其变成单链  
  411.                     KeyLable key1 = new KeyLable("+""=", SoftKeyBoardPanel.this);  
  412.                     KeyLable key2 = new KeyLable("|""\\", SoftKeyBoardPanel.this);  
  413.                     KeyLable key3 = new KeyLable("{""[", SoftKeyBoardPanel.this);  
  414.                     KeyLable key4 = new KeyLable("}""]", SoftKeyBoardPanel.this);  
  415.                     KeyLable key5 = new KeyLable(":"";", SoftKeyBoardPanel.this);  
  416.                     KeyLable key6 = new KeyLable("\"""'", SoftKeyBoardPanel.this);  
  417.                     KeyLable key7 = new KeyLable("<"",", SoftKeyBoardPanel.this);  
  418.                     KeyLable key8 = new KeyLable(">"".", SoftKeyBoardPanel.this);  
  419.                     KeyLable key9 = new KeyLable("?""/", SoftKeyBoardPanel.this);  
  420.                     KeyLable key10 = new KeyLable("_""-", SoftKeyBoardPanel.this);  
  421.                     KeyLable key11 = new KeyLable("Shift"true, SoftKeyBoardPanel.this);// 功能键,位置固定在最左  
  422.                     key11.setPreferredSize(new Dimension(3525));  
  423.                     KeyLable key12 = new KeyLable("CapsLock"true, SoftKeyBoardPanel.this);// 功能键,位置固定在最右  
  424.                     key12.setPreferredSize(new Dimension(6525));  
  425.   
  426.                     keys = new KeyLable[] { key11, key2, key3, key4, key5, key6, key7, key8, key9, key10, key1, key12 };  
  427.                     ArrayList<KeyLable> keylist = new ArrayList<KeyLable>(keys.length);  
  428.                     int randomIndex = random.nextInt(keys.length - 2) + 1;// 随机切入点,排除key11和key12的位置  
  429.   
  430.                     keylist.add(key11);  
  431.                     for (int i = randomIndex; i < keys.length - 1; i++) {  
  432.                         keylist.add(keys[i]);  
  433.                     }  
  434.                     for (int i = 1; i < randomIndex; i++) {  
  435.                         keylist.add(keys[i]);  
  436.                     }  
  437.                     keylist.add(key12);  
  438.   
  439.                     for (KeyLable key : keylist) {  
  440.                         this.add(key);  
  441.                     }  
  442.                 } else if (rowType == RowType.third) {  
  443.                     // key1至key13是一个闭环顺序,通过随机数决定在哪个位置切断这个环从而使其变成单链  
  444.                     KeyLable key1 = new KeyLable("c", SoftKeyBoardPanel.this);  
  445.                     KeyLable key2 = new KeyLable("d", SoftKeyBoardPanel.this);  
  446.                     KeyLable key3 = new KeyLable("e", SoftKeyBoardPanel.this);  
  447.                     KeyLable key4 = new KeyLable("f", SoftKeyBoardPanel.this);  
  448.                     KeyLable key5 = new KeyLable("g", SoftKeyBoardPanel.this);  
  449.                     KeyLable key6 = new KeyLable("h", SoftKeyBoardPanel.this);  
  450.                     KeyLable key7 = new KeyLable("i", SoftKeyBoardPanel.this);  
  451.                     KeyLable key8 = new KeyLable("j", SoftKeyBoardPanel.this);  
  452.                     KeyLable key9 = new KeyLable("k", SoftKeyBoardPanel.this);  
  453.                     KeyLable key10 = new KeyLable("l", SoftKeyBoardPanel.this);  
  454.                     KeyLable key11 = new KeyLable("m", SoftKeyBoardPanel.this);  
  455.                     KeyLable key12 = new KeyLable("a", SoftKeyBoardPanel.this);  
  456.                     KeyLable key13 = new KeyLable("b", SoftKeyBoardPanel.this);  
  457.   
  458.                     keys = new KeyLable[] { key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13 };  
  459.                     ArrayList<KeyLable> keylist = new ArrayList<KeyLable>(keys.length);  
  460.                     int randomIndex = random.nextInt(keys.length);// 随机切入点  
  461.   
  462.                     for (int i = randomIndex; i < keys.length; i++) {  
  463.                         keylist.add(keys[i]);  
  464.                     }  
  465.                     for (int i = 0; i < randomIndex; i++) {  
  466.                         keylist.add(keys[i]);  
  467.                     }  
  468.   
  469.                     for (KeyLable key : keylist) {  
  470.                         this.add(key);  
  471.                     }  
  472.                 } else if (rowType == RowType.fourth) {  
  473.                     // key1至key13是一个闭环顺序,通过随机数决定在哪个位置切断这个环从而使其变成单链  
  474.                     KeyLable key1 = new KeyLable("n", SoftKeyBoardPanel.this);  
  475.                     KeyLable key2 = new KeyLable("o", SoftKeyBoardPanel.this);  
  476.                     KeyLable key3 = new KeyLable("p", SoftKeyBoardPanel.this);  
  477.                     KeyLable key4 = new KeyLable("q", SoftKeyBoardPanel.this);  
  478.                     KeyLable key5 = new KeyLable("r", SoftKeyBoardPanel.this);  
  479.                     KeyLable key6 = new KeyLable("s", SoftKeyBoardPanel.this);  
  480.                     KeyLable key7 = new KeyLable("t", SoftKeyBoardPanel.this);  
  481.                     KeyLable key8 = new KeyLable("u", SoftKeyBoardPanel.this);  
  482.                     KeyLable key9 = new KeyLable("v", SoftKeyBoardPanel.this);  
  483.                     KeyLable key10 = new KeyLable("w", SoftKeyBoardPanel.this);  
  484.                     KeyLable key11 = new KeyLable("x", SoftKeyBoardPanel.this);  
  485.                     KeyLable key12 = new KeyLable("y", SoftKeyBoardPanel.this);  
  486.                     KeyLable key13 = new KeyLable("z", SoftKeyBoardPanel.this);  
  487.   
  488.                     keys = new KeyLable[] { key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13 };  
  489.                     ArrayList<KeyLable> keylist = new ArrayList<KeyLable>(keys.length);  
  490.                     int randomIndex = random.nextInt(keys.length);// 随机切入点  
  491.   
  492.                     for (int i = randomIndex; i < keys.length; i++) {  
  493.                         keylist.add(keys[i]);  
  494.                     }  
  495.                     for (int i = 0; i < randomIndex; i++) {  
  496.                         keylist.add(keys[i]);  
  497.                     }  
  498.   
  499.                     for (KeyLable key : keylist) {  
  500.                         this.add(key);  
  501.                     }  
  502.                 }  
  503.             }  
  504.   
  505.             public KeyLable[] getKeys() {  
  506.                 return keys;  
  507.             }  
  508.         }  
  509.     }  
  510.   
  511.     // 键标签  
  512.     public static class KeyLable extends JLabel {  
  513.   
  514.         // 用String而不是char考虑有功能键显示的是文字,不想再多一个字段了  
  515.         String centerKey;  
  516.         String lowerLeftKey;  
  517.         boolean isBackSpace;  
  518.         boolean isCapsLock;  
  519.         boolean isShift;  
  520.         boolean isPressed;  
  521.         KeyStatus status = KeyStatus.normal;  
  522.         Dimension size = new Dimension(2424);  
  523.         Color keyBorderColor = new Color(54112184);  
  524.         Color keyBorderFocusColor = new Color(64194241);  
  525.         Color keyBackColor = new Color(253255255);  
  526.         Color keyBackFocusColor = new Color(28159228);  
  527.         Font boldFont = new Font("微软雅黑", Font.PLAIN, 12);  
  528.         Color boldColor = new Color(0057);  
  529.         Font plainFont = new Font("微软雅黑", Font.PLAIN, 10);  
  530.         Color plainColor = new Color(156157197);  
  531.   
  532.         public KeyLable(String centerKey, ActionListener action) {  
  533.             this(centerKey, null, action);  
  534.         }  
  535.   
  536.         public KeyLable(String centerKey, String lowerLeftKey, ActionListener action) {  
  537.             this(centerKey, lowerLeftKey, false, action);  
  538.         }  
  539.   
  540.         public KeyLable(String centerKey, boolean isFunctionKey, ActionListener action) {  
  541.             this(centerKey, null, isFunctionKey, action);  
  542.         }  
  543.   
  544.         public KeyLable(String centerKey, String lowerLeftKey, boolean isFunctionKey, final ActionListener action) {  
  545.             this.centerKey = centerKey;  
  546.             this.lowerLeftKey = lowerLeftKey;  
  547.             if (isFunctionKey) {// 这个变量主要是提高效率  
  548.                 if (centerKey.indexOf("Shift") >= 0) {  
  549.                     isShift = true;  
  550.                 } else if (centerKey.indexOf("Back") >= 0 || centerKey.indexOf("Space") >= 0) {  
  551.                     isBackSpace = true;  
  552.                 } else if (centerKey.indexOf("Caps") >= 0 || centerKey.indexOf("Lock") >= 0) {  
  553.                     isCapsLock = true;  
  554.                 }  
  555.             }  
  556.   
  557.             setOpaque(true);// 不透明  
  558.             setBackground(keyBackColor);  
  559.             setPreferredSize(size);  
  560.             setBorder(BorderFactory.createLineBorder(keyBorderColor));  
  561.             setFont(boldFont);  
  562.   
  563.             addMouseListener(new MouseAdapter() {  
  564.                 public void mouseEntered(MouseEvent e) {  
  565.                     KeyLable.this.setBackground(keyBackFocusColor);// 鼠标悬浮时的背景色  
  566.                 }  
  567.   
  568.                 public void mouseExited(MouseEvent e) {  
  569.                     // 如果不是Shift和CapsLock键则还原背景色,或者是Shift和CapsLock键但是不是按压状态也要还原背景色  
  570.                     if ((!KeyLable.this.isShift && !KeyLable.this.isCapsLock) || (!isPressed)) {  
  571.                         KeyLable.this.setBackground(keyBackColor);  
  572.                     }  
  573.                 }  
  574.   
  575.                 public void mouseClicked(MouseEvent e) {  
  576.                     // 创建一个ActionEvent将KeyLable对象作为Source  
  577.                     action.actionPerformed(new ActionEvent(KeyLable.this, ActionEvent.ACTION_PERFORMED, e.getID() + ""));  
  578.                 }  
  579.             });  
  580.         }  
  581.   
  582.         @Override  
  583.         protected void paintComponent(Graphics g) {  
  584.             super.paintComponent(g);// 完成背景色的绘制  
  585.   
  586.             Graphics2D g2d = (Graphics2D) g;  
  587.             ImageTool.setAntiAliasing(g2d);// 抗锯齿  
  588.             Container parent = getParent();  
  589.             ImageTool.clearAngle(g2d, parent != null ? parent.getBackground() : this.getBackground(), this.getWidth(), this.getHeight(), 4);// 清除角落变圆角  
  590.   
  591.             if (getMousePosition() != null) {// 如果鼠标正在这个键的范围内,绘制圆角边框  
  592.                 g2d.setPaint(keyBorderFocusColor);  
  593.                 g2d.drawRoundRect(11, getWidth() - 3, getHeight() - 344);  
  594.             }  
  595.   
  596.             if (status == KeyStatus.normal || status == KeyStatus.capsLock) {  
  597.                 if (lowerLeftKey == null) {  
  598.                     g2d.setFont(boldFont);  
  599.                     g2d.setPaint(boldColor);  
  600.                     // g2d.drawString(centerKey, isCommKey() ? 8 : 4, 17);  
  601.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, centerKey, -1, isCommKey() ? 8 : 417);  
  602.   
  603.                 } else {  
  604.                     g2d.setFont(plainFont);  
  605.                     g2d.setPaint(plainColor);  
  606.                     // g2d.drawString(centerKey, 12, 15);  
  607.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, centerKey, -11215);  
  608.   
  609.                     g2d.setFont(boldFont);  
  610.                     g2d.setPaint(boldColor);  
  611.                     // g2d.drawString(lowerLeftKey, 3, 20);  
  612.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, lowerLeftKey, -1320);  
  613.                 }  
  614.             } else if (status == KeyStatus.shift || status == KeyStatus.shiftAndCapsLock) {  
  615.                 if (lowerLeftKey == null) {  
  616.                     g2d.setFont(boldFont);  
  617.                     g2d.setPaint(boldColor);  
  618.                     // g2d.drawString(centerKey, isCommKey() ? 8 : 4, 17);  
  619.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, centerKey, -1, isCommKey() ? 8 : 417);  
  620.   
  621.                 } else {  
  622.                     g2d.setFont(boldFont);  
  623.                     g2d.setPaint(boldColor);  
  624.                     // g2d.drawString(centerKey, 10, 15);  
  625.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, centerKey, -11015);  
  626.   
  627.                     g2d.setFont(plainFont);  
  628.                     g2d.setPaint(plainColor);  
  629.                     // g2d.drawString(lowerLeftKey, 3, 20);  
  630.                     SwingUtilities2.drawStringUnderlineCharAt(this, g2d, lowerLeftKey, -1320);  
  631.                 }  
  632.             }  
  633.         }  
  634.   
  635.         public String getCenterKey() {  
  636.             return centerKey;  
  637.         }  
  638.   
  639.         public String getLowerLeftKey() {  
  640.             return lowerLeftKey;  
  641.         }  
  642.   
  643.         public boolean isBackSpace() {  
  644.             return isBackSpace;  
  645.         }  
  646.   
  647.         public boolean isCapsLock() {  
  648.             return isCapsLock;  
  649.         }  
  650.   
  651.         public boolean isShift() {  
  652.             return isShift;  
  653.         }  
  654.   
  655.         public void setPressed(boolean isPressed) {  
  656.             this.isPressed = isPressed;  
  657.         }  
  658.   
  659.         public boolean isPressed() {  
  660.             return isPressed;  
  661.         }  
  662.   
  663.         public boolean isCommKey() {  
  664.             return !isBackSpace && !isCapsLock && !isShift;  
  665.         }  
  666.   
  667.         // 重置  
  668.         public void reset() {  
  669.             this.isPressed = false;  
  670.             if (isShift || isCapsLock) {  
  671.                 KeyLable.this.setBackground(keyBackColor);  
  672.             } else if (isCommKey()) {  
  673.                 if (lowerLeftKey == null) {  
  674.                     centerKey = centerKey.toLowerCase();  
  675.                 }  
  676.             }  
  677.             status = KeyStatus.normal;  
  678.             repaint();  
  679.         }  
  680.   
  681.         // 设置状态  
  682.         public void setStatus(KeyStatus status) {  
  683.             if (isCommKey() && this.status != status) {  
  684.                 if (status == KeyStatus.shift || status == KeyStatus.capsLock) {  
  685.                     if (lowerLeftKey == null) {  
  686.                         if (Character.isUpperCase(centerKey.charAt(0))) {  
  687.                             centerKey = centerKey.toLowerCase();  
  688.                         } else {  
  689.                             centerKey = centerKey.toUpperCase();  
  690.                         }  
  691.                     }  
  692.                 } else if (status == KeyStatus.normal || status == KeyStatus.shiftAndCapsLock) {  
  693.                     if (lowerLeftKey == null) {  
  694.                         centerKey = centerKey.toLowerCase();  
  695.                     }  
  696.                 }  
  697.                 this.status = status;  
  698.                 repaint();  
  699.             }  
  700.         }  
  701.     }  
  702.   
  703.     public static enum RowType {  
  704.         first, second, third, fourth  
  705.     }  
  706.   
  707.     public static enum KeyStatus {  
  708.         normal, shift, capsLock, shiftAndCapsLock  
  709.     }  
  710. }  
  711.   
  712. class ImageTool {  
  713.   
  714.     // 设置Graphics2D抗锯齿,具体请查看RenderingHints类的API  
  715.     public static void setAntiAliasing(Graphics2D g2d) {  
  716.         setRenderingHint(g2d, RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  717.     }  
  718.   
  719.     public static void setRenderingHint(Graphics2D g2d, Key key, Object value) {  
  720.         if (g2d.getRenderingHints() == null) {  
  721.             g2d.setRenderingHints(new RenderingHints(key, value));  
  722.         } else {  
  723.             g2d.setRenderingHint(key, value);  
  724.         }  
  725.     }  
  726.   
  727.     // 绘制圆角  
  728.     public static void drawRoundRect(Graphics2D g2d, int width, int height, int r, Paint anglePaint, Paint[] borderPaints) {  
  729.         clearAngle(g2d, anglePaint, width, height, r);// 清除角落  
  730.         drawMultiBorder(g2d, width, height, r, anglePaint, borderPaints);// 画边框  
  731.     }  
  732.   
  733.     // 清除角落  
  734.     public static void clearAngle(Graphics2D g2d, Paint anglePaint, int width, int height, int r) {  
  735.         setAntiAliasing(g2d);  
  736.         Composite oldComposite = g2d.getComposite();  
  737.   
  738.         if (anglePaint == null) {  
  739.             g2d.setComposite(AlphaComposite.Clear);// 设置Composite为清空  
  740.         } else {  
  741.             g2d.setPaint(anglePaint);  
  742.         }  
  743.   
  744.         int npoints = 5;// 5点定位一个角落轨迹  
  745.         // 左上角  
  746.         int[] xpoints1 = { r, 00, r / 4, r / 2 };  
  747.         int[] ypoints1 = { 00, r, r / 2, r / 4 };  
  748.         Polygon polygon = new Polygon(xpoints1, ypoints1, npoints);  
  749.         g2d.fillPolygon(polygon);  
  750.         // 右上角  
  751.         int[] xpoints2 = { width - r, width, width, width - r / 4, width - (r / 2) };  
  752.         int[] ypoints2 = ypoints1;  
  753.         polygon = new Polygon(xpoints2, ypoints2, npoints);  
  754.         g2d.fillPolygon(polygon);  
  755.         // 右下角  
  756.         int[] xpoints3 = xpoints2;  
  757.         int[] ypoints3 = { height, height, height - r, height - (r / 2), height - r / 4 };  
  758.         polygon = new Polygon(xpoints3, ypoints3, npoints);  
  759.         g2d.fillPolygon(polygon);  
  760.         // 左下角  
  761.         int[] xpoints4 = xpoints1;  
  762.         int[] ypoints4 = ypoints3;  
  763.         polygon = new Polygon(xpoints4, ypoints4, npoints);  
  764.         g2d.fillPolygon(polygon);  
  765.         // 还原Composite  
  766.         g2d.setComposite(oldComposite);  
  767.     }  
  768.   
  769.     // 绘制有层次感的边框  
  770.     public static void drawMultiBorder(Graphics2D g2d, int width, int height, int r, Paint anglePaint, Paint[] borderPaints) {  
  771.         setAntiAliasing(g2d);  
  772.   
  773.         int roundx = r * 2;  
  774.         int roundy = roundx;  
  775.         int grow = 2;  
  776.         int x = 0;  
  777.         int y = 0;  
  778.         int w = width;  
  779.         int h = height;  
  780.   
  781.         // 从外层往内层开始画  
  782.         for (int i = 0; i < borderPaints.length; i++, x++, y++, w -= grow, h -= grow) {  
  783.             g2d.setPaint(borderPaints[i]);  
  784.             if (r > 0) {  
  785.                 g2d.drawRoundRect(x, y, w - 1, h - 1, roundx, roundy);  
  786.             } else {  
  787.                 g2d.drawRect(x, y, w - 1, h - 1);  
  788.             }  
  789.         }  
  790.     }  
  791. }  


运行效果:

这篇关于Swing之QQ软键盘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1020259

相关文章

Python利用qq邮箱发送通知邮件(已封装成model)

因为经常喜欢写一些脚本、爬虫之类的东西,有需要通知的时候,总是苦于没有太好的通知方式,虽然邮件相对于微信、短信来说,接收性差了一些,但毕竟免费,而且支持html直接渲染,所以,折腾了一个可以直接使用的sendemail模块。这里主要应用的是QQ发邮件,微信关注QQ邮箱后,也可以实时的接收到消息,肾好! 好了,废话不多说,直接上代码。 # encoding: utf-8import lo

第一次接触Swing

学习java版的HslCommunication发现使用的是Swing,所以了解了一下~ 了解: Swing是Java的标准库(Java Foundation Classes, JFC)的一部分,用于构建桌面应用程序的图形用户界面(GUI)。它是Java AWT(Abstract Window Toolkit)的增强版,提供了更多的组件、更好的外观和感觉,以及更丰富的功能。Swing使用

Spring Boot 实现微信、QQ 绑定登录

文章目录 1. 项目环境2. 创建Spring Boot项目3. 配置微信和QQ开发平台4. 配置Spring Security5. 配置Spring Security6. 创建登录和主页控制器7. 创建视图8. 运行项目9. 处理用户信息结论 🎉欢迎来到SpringBoot框架学习专栏~ ☆* o(≧▽≦)o *☆嗨~我是IT·陈寒🍹✨博客主页:IT·陈寒的博

Android 自定义软键盘实现 数字九宫格

最近项目在对接美团外卖功能 实现外面小哥凭取货码取货 对接完功能后 用户反馈 弹出的软键盘 很难输入 数字太小了 大概是下面这种显示方式 需求 组长说 要不搞一个自定义软键盘吧 数字搞大点 方便外卖员输入数字 我设置了输入EditText的输入格式为Number 还是不行 那就开搞吧 先来看下实现的效果图吧 实现效果GIF 实现代码 自定义View 一个NineNume

分享一个类似QQ屏保解锁的代码类

话不多说了,直接上代码: .h文件//// GestureCodeViewController.h// DailyRecord//// Created by sven on 13-6-20.// Copyright (c) 2013年 sven. All rights reserved.//#import <UIKit/UIKit.h>#define LOCK_POINT_

swing中把数据库的数据显示在界面表格中

private JFrame J;private DefaultTableModel model;private JTable table;Hangban() throws ClassNotFoundException ,SQLException{J = new JFrame();J.setTitle("航班信息");J.setDefaultCloseOperation(JFram

香橙派 5 PLUS 安装QQ(arm架构、Ubuntu系统)

1、下载QQ for Linux: 访问腾讯QQ官网,下载适用于香橙派 5 PLUS的arm架构Linux的QQ安装包。          比如:ARM版下载deb格式QQ安装包 ‘ QQ_3.2.9_240617_arm64_01.deb ’。  2、安装QQ for Linux: sudo dpkg -i [下载的文件名.deb] 3、运行QQ for Linux:

Android 软键盘的处理

转载:http://blog.csdn.net/mynameishuangshuai/article/details/51567357 软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog。 InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示。当我们点击

微信公众平台-微信发送朋友、分享到QQ、分享QQ空间、分享腾讯微博-JSSDk接口

jssdk.php: <?phpclass JSSDK {private $appId; //公众号的appidprivate $appSecret;//公众号的密钥public function __construct($appId, $appSecret) {$this->appId = $appId;$this->appSecret = $appSecret;}//获取签名pub

Llinux下安装网易云和QQ

Linux下安装软件对一个新手来说并不容易,下面来讲讲安装网易云和QQ的简单操作: (1)安装网易云音乐:下载地址点击打开链接 方法:在ubuntu的控制台命令窗口输入命令:sudo dpkg -i netease-cloud-music_1.0.0_amd64_ubuntu16.04.deb 会出现依赖问题 输入:sudo apt-get install -f重新输入上个命令