Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*; // import all javax.swing
- import java.awt.*; // import all java.awt
- import java.awt.event.*;
- import java.util.HashMap;
- import java.util.Map;
- public class Keyboard extends JFrame implements KeyListener { // start of the class
- private JTextArea field = new JTextArea(10,62); // create an instance of JTextField
- private JPanel mainPanel = new JPanel(); // create an instance of JPanel
- private JPanel TopLabPan = new JPanel(); // create an instance of JPanel
- private JPanel TopFieldPan = new JPanel();
- private JPanel MainBtnsPan = new JPanel();
- private JPanel FirstRowPan = new JPanel();
- private JPanel SecondRowPan = new JPanel();
- private JPanel ThirdRowPan = new JPanel();
- private JPanel FourthRowPan = new JPanel();
- private JPanel FifthRowPan = new JPanel();
- private JLabel TopLab = new JLabel("Type some text using your keyboard. The keys press will be highlighted and the text will be display");
- 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
- private static String[] FirstRow = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "Backspace"};
- private static String[] SecondRow = {"TAB", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"};
- private static String[] ThirdRow = {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "Enter"};
- private static String[] FourthRow = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "^"};
- JPanel firstBtn = new JPanel();
- JPanel secondBtn = new JPanel();
- JPanel thirdBtn = new JPanel();
- JPanel fourthBtn = new JPanel();
- JPanel fifthBtn = new JPanel();
- private JButton Space = new JButton("");
- private Map<String, JButton> buttonMap = new HashMap<String, JButton>();
- public Keyboard(){ // start of the weather constructor
- for (int i = 0; i < FirstRow.length-1; i++) {
- JButton btn = new JButton(FirstRow[i]);
- buttonMap.put(FirstRow[i].toLowerCase(), btn);
- btn.setPreferredSize(new Dimension(45,45));
- firstBtn.add(btn);
- }
- JButton Bspc = new JButton("Backspace");
- Bspc.setPreferredSize(new Dimension(100,45));
- firstBtn.add(Bspc);
- JButton Tabs = new JButton("Tab");
- Tabs.setPreferredSize(new Dimension(70,45));
- secondBtn.add(Tabs);
- for (int i = 1; i < SecondRow.length; i++) {
- JButton btn = new JButton(SecondRow[i]);
- buttonMap.put(SecondRow[i].toLowerCase(), btn);
- btn.setPreferredSize(new Dimension(46,45));
- secondBtn.add(btn);
- }
- JButton Caps = new JButton("Caps");
- Caps.setPreferredSize(new Dimension(70,45));
- thirdBtn.add(Caps);
- for (int i = 1; i < ThirdRow.length-1; i++) {
- JButton btn = new JButton(ThirdRow[i]);
- btn.setPreferredSize(new Dimension(45,45));
- thirdBtn.add(btn);
- }
- JButton Enter = new JButton("Enter");
- Enter.setPreferredSize(new Dimension(90,45));
- thirdBtn.add(Enter);
- JButton Shift = new JButton("Shift");
- Shift.setPreferredSize(new Dimension(95,45));
- fourthBtn.add(Shift);
- for (int i = 1; i < FourthRow.length - 1; i++) {
- JButton btn = new JButton(FourthRow[i]);
- btn.setPreferredSize(new Dimension(45,45));
- fourthBtn.add(btn);
- }
- JButton up = new JButton("^");
- up.setPreferredSize(new Dimension(45,45));
- fourthBtn.add(Box.createRigidArea(new Dimension(21,0)));
- fourthBtn.add(up);
- fifthBtn.add(Box.createRigidArea(new Dimension(154,0)));
- Space.setPreferredSize(new Dimension(280,45));
- fifthBtn.add(Space);
- JButton left = new JButton("<");
- fifthBtn.add(Box.createRigidArea(new Dimension(60,0)));
- left.setPreferredSize(new Dimension(45,45));
- fifthBtn.add(left);
- JButton down = new JButton("v");
- down.setPreferredSize(new Dimension(45,45));
- fifthBtn.add(down);
- JButton right = new JButton(">");
- right.setPreferredSize(new Dimension(45,45));
- fifthBtn.add(right);
- TopLabPan.setLayout(new BoxLayout(TopLabPan, BoxLayout.Y_AXIS));
- TopLabPan.add(TopLab);
- TopLabPan.add(BotLab);
- TopFieldPan.add(field);
- FirstRowPan.setLayout(new BoxLayout(FirstRowPan, BoxLayout.X_AXIS));
- FirstRowPan.add(firstBtn);
- firstBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
- SecondRowPan.setLayout(new BoxLayout(SecondRowPan, BoxLayout.X_AXIS));
- SecondRowPan.add(secondBtn);
- secondBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
- ThirdRowPan.setLayout(new BoxLayout(ThirdRowPan, BoxLayout.X_AXIS));
- ThirdRowPan.add(thirdBtn);
- thirdBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
- FourthRowPan.setLayout(new BoxLayout(FourthRowPan, BoxLayout.X_AXIS));
- FourthRowPan.add(fourthBtn);
- fourthBtn.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
- FifthRowPan.setLayout(new BoxLayout(FifthRowPan, BoxLayout.X_AXIS));
- FifthRowPan.add(fifthBtn);
- fifthBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
- FlowLayout flowLayout = (FlowLayout) mainPanel.getLayout();
- flowLayout.setAlignment(FlowLayout.LEFT);
- MainBtnsPan.setLayout(new GridLayout(5, 5, 0, 0));
- MainBtnsPan.add(FirstRowPan);
- MainBtnsPan.add(SecondRowPan);
- MainBtnsPan.add(ThirdRowPan);
- MainBtnsPan.add(FourthRowPan);
- MainBtnsPan.add(FifthRowPan);
- mainPanel.add(TopLabPan);
- mainPanel.add(TopFieldPan);
- mainPanel.add(MainBtnsPan);
- this.add(mainPanel);
- //Space.setSelected(true);
- field.addKeyListener(this); // important !!!
- this.addKeyListener(this); // important !!!
- TopFieldPan.addKeyListener(this); // important !!!
- setTitle("Typing Tutor"); // set the title to the frame
- setSize(710,490); // set the fixed size
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set to terminate after exit
- setLocationRelativeTo(null); // the pop-up window will be at the center of the screen
- setVisible(true); // make it visible
- } // ends of the constructor
- public void keyPressed(KeyEvent evt)
- {
- if(evt.getKeyChar() == 'a' || evt.getKeyChar() == 'A')
- {
- Space.setBackground(Color.green);
- }
- else if(evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
- {
- Space.setBackground(Color.red);
- }
- }
- public void keyReleased(KeyEvent evt)
- {
- Space.setBackground(null);
- }
- public void keyTyped(KeyEvent evt) {
- String keyPressed = new String(""+evt.getKeyChar()).toLowerCase();
- JButton tmp = buttonMap.get(keyPressed);
- if(null != tmp){
- tmp.doClick();
- }
- }
- public static void main(String[] args) { // start of the main method
- new Keyboard();
- } // ends of main method
- } // ends of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement