import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Rectangle; import javax.swing.FocusManager; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.border.SoftBevelBorder; import java.awt.AWTException; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.Font; import java.awt.Robot; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; public class KeyboardFrame extends JFrame implements ActionListener, FocusListener { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JPanel jPanel = null; private JPanel jPanel1 = null; private JButton jButton = null; private JTextField jTextField = null; private JTextField jTextField1 = null; private JTextField jTextField2 = null; private JTextField jTextField3 = null; private JPanel jPanel2 = null; boolean flag = true; private JButton[] keyboardButton; /** * This method initializes jPanel * * @return javax.swing.JPanel */ private JPanel getJPanel() { if (jPanel == null) { jPanel = new JPanel(); jPanel.setLayout(null); jPanel.setBounds(new Rectangle(0, 0, 795, 518)); jPanel.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED)); jPanel.add(getJTextField(), null); jPanel.add(getJTextField1(), null); jPanel.add(getJTextField2(), null); jPanel.add(getJTextField3(), null); } return jPanel; } /** * This method initializes jPanel1 * * @return javax.swing.JPanel */ private JPanel getJPanel1() { if (jPanel1 == null) { jPanel1 = new JPanel(); jPanel1.setLayout(null); jPanel1.setBounds(new Rectangle(0, 518, 795, 50)); jPanel1.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED)); jPanel1.setBackground(Color.lightGray); jPanel1.add(getJButton(), null); } return jPanel1; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new Rectangle(335, 16, 89, 27)); jButton.setText("Close"); } return jButton; } /** * This method initializes jTextField * * @return javax.swing.JTextField */ private JTextField getJTextField() { if (jTextField == null) { jTextField = new JTextField(); jTextField.setBounds(new Rectangle(60, 46, 285, 29)); } return jTextField; } /** * This method initializes jTextField1 * * @return javax.swing.JTextField */ private JTextField getJTextField1() { if (jTextField1 == null) { jTextField1 = new JTextField(); jTextField1.setBounds(new Rectangle(59, 195, 286, 33)); } return jTextField1; } /** * This method initializes jTextField2 * * @return javax.swing.JTextField */ private JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new JTextField(); jTextField2.setBounds(new Rectangle(60, 465, 287, 30)); } return jTextField2; } /** * This method initializes jTextField3 * * @return javax.swing.JTextField */ private JTextField getJTextField3() { if (jTextField3 == null) { jTextField3 = new JTextField(); jTextField3.setBounds(new Rectangle(59, 330, 286, 33)); } return jTextField3; } /** * This method initializes jPanel2 * * @return javax.swing.JPanel */ private JPanel getJPanel2() { if (jPanel2 == null) { GridLayout gridLayout = new GridLayout(); gridLayout.setRows(4); gridLayout.setColumns(10); gridLayout.setVgap(2); gridLayout.setHgap(2); jPanel2 = new JPanel(); jPanel2.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED)); jPanel2.setFont(new Font("Dialog", Font.BOLD, 12)); jPanel2.setLayout(gridLayout); jPanel2.setBounds(new Rectangle(5, 611, 780, 150)); keyboardButton = new JButton[50]; int buttonNumber = 0; for (int i = 49; i < 58; i++) { createButtonInPanel(buttonNumber, "" + (char) i); buttonNumber++; } createButtonInPanel(buttonNumber, "0"); buttonNumber++; for (int i = 97; i < 123; i++) { createButtonInPanel(buttonNumber, "" + (char) i); buttonNumber++; } createButtonInPanel(buttonNumber++, "OK"); createButtonInPanel(buttonNumber++, "CHANGE"); createButtonInPanel(buttonNumber++, "SHIT"); createButtonInPanel(buttonNumber++, "BACKSPACE"); } return jPanel2; } /** * ������д���һ��ť * * @param buttonNumber * @param buttonText */ private void createButtonInPanel(int buttonNumber, String buttonText) { JButton button = new JButton(buttonText); keyboardButton[buttonNumber] = button; button.setFocusable(false); button.addActionListener(this); button.setBackground(Color.LIGHT_GRAY); jPanel2.add(button); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SwingUtilities.invokeLater(new Runnable() { public void run() { KeyboardFrame thisClass = new KeyboardFrame(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.jTextField.addFocusListener(thisClass); thisClass.jTextField1.addFocusListener(thisClass); thisClass.jTextField2.addFocusListener(thisClass); thisClass.jTextField3.addFocusListener(thisClass); // for (int i = 0; i < thisClass.keyboardButton.length; i++) { // JButton button = (JButton) thisClass.keyboardButton[i]; // button.addActionListener(thisClass); // } thisClass.setVisible(true); } }); } /** * This is the default constructor */ public KeyboardFrame() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setContentPane(getJContentPane()); JPanel glass = (JPanel) this.getGlassPane(); glass.setLayout(null); glass.add(getJPanel2()); glass.setVisible(true); this.setTitle("JFrame"); this.setBounds(new Rectangle(0, 0, 800, 600)); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJPanel(), null); jContentPane.add(getJPanel1(), null); // jContentPane.add(getJPanel2(), null); } return jContentPane; } @Override public void actionPerformed(ActionEvent e) { Component component = FocusManager.getCurrentManager().getFocusOwner(); JButton jbutton = (JButton) e.getSource(); try { Robot r = new Robot(); if (jbutton.getText().toUpperCase().equals("1")) { r.keyPress(KeyEvent.VK_1); r.keyRelease(KeyEvent.VK_1); } else if (jbutton.getText().toUpperCase().equals("2")) { r.keyPress(KeyEvent.VK_2); r.keyRelease(KeyEvent.VK_2); } else if (jbutton.getText().toUpperCase().equals("3")) { r.keyPress(KeyEvent.VK_3); r.keyRelease(KeyEvent.VK_3); } else if (jbutton.getText().toUpperCase().equals("4")) { r.keyPress(KeyEvent.VK_4); r.keyRelease(KeyEvent.VK_4); } else if (jbutton.getText().toUpperCase().equals("5")) { r.keyPress(KeyEvent.VK_5); r.keyRelease(KeyEvent.VK_5); } else if (jbutton.getText().toUpperCase().equals("6")) { r.keyPress(KeyEvent.VK_6); r.keyRelease(KeyEvent.VK_6); } else if (jbutton.getText().toUpperCase().equals("7")) { r.keyPress(KeyEvent.VK_7); r.keyRelease(KeyEvent.VK_7); } else if (jbutton.getText().toUpperCase().equals("8")) { r.keyPress(KeyEvent.VK_8); r.keyRelease(KeyEvent.VK_8); } else if (jbutton.getText().toUpperCase().equals("9")) { r.keyPress(KeyEvent.VK_9); r.keyRelease(KeyEvent.VK_9); } else if (jbutton.getText().toUpperCase().equals("0")) { r.keyPress(KeyEvent.VK_0); r.keyRelease(KeyEvent.VK_0); } else if (jbutton.getText().toUpperCase().equals("A")) { r.keyPress(KeyEvent.VK_A); r.keyRelease(KeyEvent.VK_A); } else if (jbutton.getText().toUpperCase().equals("B")) { r.keyPress(KeyEvent.VK_B); r.keyRelease(KeyEvent.VK_B); } else if (jbutton.getText().toUpperCase().equals("C")) { r.keyPress(KeyEvent.VK_C); r.keyRelease(KeyEvent.VK_C); } else if (jbutton.getText().toUpperCase().equals("D")) { r.keyPress(KeyEvent.VK_D); r.keyRelease(KeyEvent.VK_D); } else if (jbutton.getText().toUpperCase().equals("E")) { r.keyPress(KeyEvent.VK_E); r.keyRelease(KeyEvent.VK_E); } else if (jbutton.getText().toUpperCase().equals("F")) { r.keyPress(KeyEvent.VK_F); r.keyRelease(KeyEvent.VK_F); } else if (jbutton.getText().toUpperCase().equals("G")) { r.keyPress(KeyEvent.VK_G); r.keyRelease(KeyEvent.VK_G); } else if (jbutton.getText().toUpperCase().equals("H")) { r.keyPress(KeyEvent.VK_H); r.keyRelease(KeyEvent.VK_H); } else if (jbutton.getText().toUpperCase().equals("I")) { r.keyPress(KeyEvent.VK_I); r.keyRelease(KeyEvent.VK_I); } else if (jbutton.getText().toUpperCase().equals("J")) { r.keyPress(KeyEvent.VK_J); r.keyRelease(KeyEvent.VK_J); } else if (jbutton.getText().toUpperCase().equals("K")) { r.keyPress(KeyEvent.VK_K); r.keyRelease(KeyEvent.VK_K); } else if (jbutton.getText().toUpperCase().equals("L")) { r.keyPress(KeyEvent.VK_L); r.keyRelease(KeyEvent.VK_L); } else if (jbutton.getText().toUpperCase().equals("M")) { r.keyPress(KeyEvent.VK_M); r.keyRelease(KeyEvent.VK_M); } else if (jbutton.getText().toUpperCase().equals("N")) { r.keyPress(KeyEvent.VK_N); r.keyRelease(KeyEvent.VK_N); } else if (jbutton.getText().toUpperCase().equals("O")) { r.keyPress(KeyEvent.VK_O); r.keyRelease(KeyEvent.VK_O); } else if (jbutton.getText().toUpperCase().equals("P")) { r.keyPress(KeyEvent.VK_P); r.keyRelease(KeyEvent.VK_P); } else if (jbutton.getText().toUpperCase().equals("Q")) { r.keyPress(KeyEvent.VK_Q); r.keyRelease(KeyEvent.VK_Q); } else if (jbutton.getText().toUpperCase().equals("R")) { r.keyPress(KeyEvent.VK_R); r.keyRelease(KeyEvent.VK_R); } else if (jbutton.getText().toUpperCase().equals("S")) { r.keyPress(KeyEvent.VK_S); r.keyRelease(KeyEvent.VK_S); } else if (jbutton.getText().toUpperCase().equals("T")) { r.keyPress(KeyEvent.VK_T); r.keyRelease(KeyEvent.VK_T); } else if (jbutton.getText().toUpperCase().equals("A")) { r.keyPress(KeyEvent.VK_A); r.keyRelease(KeyEvent.VK_A); } else if (jbutton.getText().toUpperCase().equals("U")) { r.keyPress(KeyEvent.VK_U); r.keyRelease(KeyEvent.VK_U); } else if (jbutton.getText().toUpperCase().equals("V")) { r.keyPress(KeyEvent.VK_V); r.keyRelease(KeyEvent.VK_V); } else if (jbutton.getText().toUpperCase().equals("W")) { r.keyPress(KeyEvent.VK_W); r.keyRelease(KeyEvent.VK_W); } else if (jbutton.getText().toUpperCase().equals("X")) { r.keyPress(KeyEvent.VK_X); r.keyRelease(KeyEvent.VK_X); } else if (jbutton.getText().toUpperCase().equals("Y")) { r.keyPress(KeyEvent.VK_Y); r.keyRelease(KeyEvent.VK_Y); } else if (jbutton.getText().toUpperCase().equals("Z")) { r.keyPress(KeyEvent.VK_Z); r.keyRelease(KeyEvent.VK_Z); } else if (jbutton.getText().toUpperCase().equals("OK")) { r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); // jPanel.setBounds(new Rectangle(0, 0, 795, 518)); // jPanel2.setBounds(new Rectangle(5, 611, 780, 150)); } else if (jbutton.getText().toUpperCase().equals("CHANGE")) { r.keyPress(KeyEvent.VK_ALT); r.keyPress(KeyEvent.VK_SHIFT); r.keyRelease(KeyEvent.VK_ALT); r.keyRelease(KeyEvent.VK_SHIFT); } else if (jbutton.getText().toUpperCase().equals("BACKSPACE")) { r.keyPress(KeyEvent.VK_BACK_SPACE); r.keyRelease(KeyEvent.VK_BACK_SPACE); } else if (jbutton.getText().toUpperCase().equals("SHIT")) { // if (!jbutton.isSelected()) { // r.keyRelease(KeyEvent.VK_SHIFT); // jbutton.setSelected(true); // jbutton.setBackground(Color.LIGHT_GRAY); // } else { // jbutton.setBackground(Color.DARK_GRAY); // jbutton.setSelected(false); // r.keyPress(KeyEvent.VK_SHIFT); // } if (flag) { keyboardButton[0].setText("!"); keyboardButton[1].setText("@"); keyboardButton[2].setText("#"); keyboardButton[3].setText("$"); keyboardButton[4].setText("%"); keyboardButton[5].setText("^"); keyboardButton[6].setText("&"); keyboardButton[7].setText("*"); keyboardButton[8].setText("("); keyboardButton[9].setText(")"); for (int i = 10; i < 36; i++) { keyboardButton[i].setText(keyboardButton[i].getText() .toUpperCase()); } r.keyPress(KeyEvent.VK_CAPS_LOCK); r.keyRelease(KeyEvent.VK_CAPS_LOCK); flag = false; // JOptionPane.showMessageDialog(null, // "注意:数字键已经切换到符号操作,字母已切换到大写!"); } else { keyboardButton[0].setText("1"); keyboardButton[1].setText("2"); keyboardButton[2].setText("3"); keyboardButton[3].setText("4"); keyboardButton[4].setText("5"); keyboardButton[5].setText("6"); keyboardButton[6].setText("7"); keyboardButton[7].setText("8"); keyboardButton[8].setText("9"); keyboardButton[9].setText("0"); for (int i = 10; i < 36; i++) { keyboardButton[i].setText(keyboardButton[i].getText() .toLowerCase()); } r.keyPress(KeyEvent.VK_CAPS_LOCK); r.keyRelease(KeyEvent.VK_CAPS_LOCK); // JOptionPane.showMessageDialog(null, // "注意:数字键已经切换到数字操作,字母已切换到小写!"); flag = true; } } else { if (component instanceof JTextField) { JTextField jtextField = (JTextField) component; jtextField .setText(jtextField.getText() + jbutton.getText()); } } } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void focusGained(FocusEvent e) { int yFocus = FocusManager.getCurrentManager().getFocusOwner().getY(); int heightFocus = FocusManager.getCurrentManager().getFocusOwner() .getHeight(); int yKeyBoard = 365; System.out.println("-----" + yFocus); if (yFocus > yKeyBoard) { jPanel.setBounds(new Rectangle(0, yKeyBoard - yFocus - heightFocus, 795, 518)); } jPanel2.setBounds(new Rectangle(5, yKeyBoard, 780, 150)); } @Override public void focusLost(FocusEvent e) { jPanel.setBounds(new Rectangle(0, 0, 795, 518)); jPanel2.setBounds(new Rectangle(5, 611, 780, 150)); } } // @jve:decl-index=0:visual-constraint="10,10"