Advertisement
Guest User

Untitled

a guest
Mar 25th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.29 KB | None | 0 0
  1. import javax.swing.*; // import all javax.swing
  2. import java.awt.*; // import all java.awt
  3. import java.awt.event.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class Keyboard extends JFrame implements KeyListener { // start of the class
  8.  
  9.     private JTextArea field = new JTextArea(10,62); // create an instance of JTextField
  10.     private JPanel mainPanel = new JPanel(); // create an instance of JPanel
  11.     private JPanel TopLabPan = new JPanel(); // create an instance of JPanel
  12.     private JPanel TopFieldPan = new JPanel();
  13.     private JPanel MainBtnsPan = new JPanel();
  14.     private JPanel FirstRowPan = new JPanel();
  15.     private JPanel SecondRowPan = new JPanel();
  16.     private JPanel ThirdRowPan = new JPanel();
  17.     private JPanel FourthRowPan = new JPanel();
  18.     private JPanel FifthRowPan = new JPanel();
  19.     private JLabel TopLab = new JLabel("Type some text using your keyboard. The keys press will be highlighted and the text will be display");
  20.     private JLabel BotLab = new JLabel("Note: clicking the buttons with your mouse will not perform any action");  // create an instance of JLabel with a default text
  21.  
  22.  
  23.     private static String[] FirstRow = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "Backspace"};
  24.     private static String[] SecondRow = {"TAB", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"};
  25.     private static String[] ThirdRow = {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "Enter"};
  26.     private static String[] FourthRow = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "^"};
  27.  
  28.     JPanel firstBtn = new JPanel();
  29.     JPanel secondBtn = new JPanel();
  30.     JPanel thirdBtn = new JPanel();
  31.     JPanel fourthBtn = new JPanel();
  32.     JPanel fifthBtn = new JPanel();
  33.  
  34.  
  35.     private JButton Space = new JButton("");
  36.  
  37.     private Map<String, JButton> buttonMap = new HashMap<String, JButton>();
  38.  
  39.  
  40.     public Keyboard(){ // start of the weather constructor
  41.  
  42.  
  43.         for (int i = 0; i < FirstRow.length-1; i++) {
  44.             JButton btn = new JButton(FirstRow[i]);
  45.             buttonMap.put(FirstRow[i].toLowerCase(), btn);
  46.             btn.setPreferredSize(new Dimension(45,45));
  47.             firstBtn.add(btn);
  48.         }
  49.  
  50.         JButton Bspc = new JButton("Backspace");
  51.         Bspc.setPreferredSize(new Dimension(100,45));
  52.         firstBtn.add(Bspc);
  53.  
  54.  
  55.         JButton Tabs = new JButton("Tab");
  56.         Tabs.setPreferredSize(new Dimension(70,45));
  57.         secondBtn.add(Tabs);
  58.  
  59.         for (int i = 1; i < SecondRow.length; i++) {
  60.             JButton btn = new JButton(SecondRow[i]);
  61.             buttonMap.put(SecondRow[i].toLowerCase(), btn);
  62.             btn.setPreferredSize(new Dimension(46,45));
  63.             secondBtn.add(btn);
  64.         }
  65.  
  66.         JButton Caps = new JButton("Caps");
  67.         Caps.setPreferredSize(new Dimension(70,45));
  68.         thirdBtn.add(Caps);
  69.  
  70.         for (int i = 1; i < ThirdRow.length-1; i++) {
  71.             JButton btn = new JButton(ThirdRow[i]);
  72.             btn.setPreferredSize(new Dimension(45,45));
  73.             thirdBtn.add(btn);
  74.         }
  75.  
  76.         JButton Enter = new JButton("Enter");
  77.         Enter.setPreferredSize(new Dimension(90,45));
  78.         thirdBtn.add(Enter);
  79.  
  80.         JButton Shift = new JButton("Shift");
  81.         Shift.setPreferredSize(new Dimension(95,45));
  82.         fourthBtn.add(Shift);
  83.  
  84.         for (int i = 1; i < FourthRow.length - 1; i++) {
  85.             JButton btn = new JButton(FourthRow[i]);
  86.             btn.setPreferredSize(new Dimension(45,45));
  87.             fourthBtn.add(btn);
  88.         }
  89.  
  90.         JButton up = new JButton("^");
  91.         up.setPreferredSize(new Dimension(45,45));
  92.         fourthBtn.add(Box.createRigidArea(new Dimension(21,0)));
  93.         fourthBtn.add(up);
  94.  
  95.         fifthBtn.add(Box.createRigidArea(new Dimension(154,0)));
  96.         Space.setPreferredSize(new Dimension(280,45));
  97.         fifthBtn.add(Space);
  98.  
  99.         JButton left = new JButton("<");
  100.         fifthBtn.add(Box.createRigidArea(new Dimension(60,0)));
  101.         left.setPreferredSize(new Dimension(45,45));
  102.         fifthBtn.add(left);
  103.  
  104.         JButton down = new JButton("v");
  105.         down.setPreferredSize(new Dimension(45,45));
  106.         fifthBtn.add(down);
  107.  
  108.         JButton right = new JButton(">");
  109.         right.setPreferredSize(new Dimension(45,45));
  110.         fifthBtn.add(right);
  111.  
  112.  
  113.         TopLabPan.setLayout(new BoxLayout(TopLabPan, BoxLayout.Y_AXIS));
  114.  
  115.         TopLabPan.add(TopLab);
  116.         TopLabPan.add(BotLab);
  117.         TopFieldPan.add(field);
  118.         FirstRowPan.setLayout(new BoxLayout(FirstRowPan, BoxLayout.X_AXIS));
  119.         FirstRowPan.add(firstBtn);
  120.         firstBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
  121.         SecondRowPan.setLayout(new BoxLayout(SecondRowPan, BoxLayout.X_AXIS));
  122.         SecondRowPan.add(secondBtn);
  123.         secondBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  124.         ThirdRowPan.setLayout(new BoxLayout(ThirdRowPan, BoxLayout.X_AXIS));
  125.         ThirdRowPan.add(thirdBtn);
  126.         thirdBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  127.         FourthRowPan.setLayout(new BoxLayout(FourthRowPan, BoxLayout.X_AXIS));
  128.         FourthRowPan.add(fourthBtn);
  129.         fourthBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  130.         FifthRowPan.setLayout(new BoxLayout(FifthRowPan, BoxLayout.X_AXIS));
  131.         FifthRowPan.add(fifthBtn);
  132.         fifthBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
  133.         FlowLayout flowLayout = (FlowLayout) mainPanel.getLayout();
  134.         flowLayout.setAlignment(FlowLayout.LEFT);
  135.         MainBtnsPan.setLayout(new GridLayout(5, 5, 0, 0));
  136.  
  137.         MainBtnsPan.add(FirstRowPan);
  138.         MainBtnsPan.add(SecondRowPan);
  139.         MainBtnsPan.add(ThirdRowPan);
  140.         MainBtnsPan.add(FourthRowPan);
  141.         MainBtnsPan.add(FifthRowPan);
  142.  
  143.         mainPanel.add(TopLabPan);
  144.         mainPanel.add(TopFieldPan);
  145.         mainPanel.add(MainBtnsPan);
  146.  
  147.  
  148.         this.add(mainPanel);
  149.  
  150.         //Space.setSelected(true);
  151.         field.addKeyListener(this);   // important !!!
  152.  
  153.         this.addKeyListener(this);  // important !!!
  154.         TopFieldPan.addKeyListener(this);   // important !!!
  155.  
  156.         setTitle("Typing Tutor"); // set the title to the frame
  157.         setSize(710,490); // set the fixed size
  158.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set to terminate after exit
  159.         setLocationRelativeTo(null); // the pop-up window will be at the center of the screen
  160.         setVisible(true); // make it visible
  161.  
  162.     } // ends of the constructor
  163.  
  164.     public void keyPressed(KeyEvent evt)
  165.     {
  166.  
  167.         if(evt.getKeyChar() == 'a' || evt.getKeyChar() ==  'A')
  168.         {
  169.             Space.setBackground(Color.green);
  170.         }
  171.         else if(evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
  172.         {
  173.             Space.setBackground(Color.red);
  174.         }
  175.  
  176.  
  177.  
  178.     }
  179.     public void keyReleased(KeyEvent evt)
  180.     {
  181.         Space.setBackground(null);
  182.     }
  183.     public void keyTyped(KeyEvent evt) {
  184.         String keyPressed = new String(""+evt.getKeyChar()).toLowerCase();
  185.         JButton tmp = buttonMap.get(keyPressed);
  186.         if(null != tmp){
  187.             tmp.doClick();
  188.         }
  189.     }
  190.  
  191.     public static void main(String[] args) { // start of the main method
  192.  
  193.         new Keyboard();
  194.  
  195.     } // ends of main method
  196.  
  197. } // ends of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement