Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. package teclado;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.util.Arrays;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. /**
  18. * Simple swing based typing tutor using frame
  19. * @author
  20. * @date
  21. */
  22. public class TypingTutor extends JFrame implements KeyListener
  23. {
  24. //Individual keyboard rows
  25. String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","<<<<<"};//BackSpace
  26. String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
  27. String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"};
  28. String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?"," ^" };
  29. String fifthRow[]={" " ,"<" ,"\\/",">" };
  30.  
  31. //all keys without shift key press
  32. String noShift="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
  33. //special charactors on keyboard that has to be addressed duing keypress
  34. String specialChars ="~-+[]\\;',.?";
  35.  
  36. //Jbuttons corresponting to each individual rows
  37. JButton first[];
  38.  
  39. JButton second[];
  40.  
  41. JButton third[];
  42.  
  43. JButton fourth[];
  44.  
  45. JButton fifth[];
  46.  
  47. //default color of the button to be repainted when key released
  48. Color cc = new JButton().getBackground();
  49.  
  50. /*
  51. * Invoked when a key has been pressed.
  52. * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
  53. */
  54. public void keyPressed(KeyEvent evt)
  55. {
  56.  
  57.  
  58. }//end of keypressed
  59.  
  60. /**
  61. * Invoked when a key has been released.
  62. */
  63. public void keyReleased(KeyEvent evt)
  64. {
  65.  
  66. }
  67.  
  68. /*
  69. * Driver main method to start the application
  70. */
  71. public static void main(String[] args) {
  72. //launch typing tutor
  73. new TypingTutor().setVisible(true);
  74. }
  75. /*
  76. * No argument construtor to create frame
  77. */
  78. public TypingTutor()
  79. {
  80. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81. //set non resizable
  82. this.setResizable(false);
  83. //super.setSize(500,300);
  84. //set size of the content pane ie frame
  85. this.getContentPane().setPreferredSize(new Dimension(1000,600));
  86. //super.getContentPane().setSize(800,400);
  87. //set location for the frame
  88. this.setLocation(50,50);
  89.  
  90. //init and paint frame
  91. initWidgets();
  92. }
  93.  
  94. /**
  95. * Method to initialize frame component
  96. */
  97. private void initWidgets()
  98. {
  99. //set the text area on top
  100. JTextArea text = new JTextArea();
  101. text.setPreferredSize(new Dimension(800,200));
  102. //JScrollPane scrollPane = new JScrollPane(text);
  103. //scrollPane.setPreferredSize(new Dimension(800, 200));
  104.  
  105. // add(typingArea, BorderLayout.PAGE_START);
  106. // add(scrollPane, BorderLayout.CENTER);
  107. //set the info label on top
  108. JLabel info = new JLabel("<html>Type some text using your keyboard.The keys you press will be highlighted and text will be displayed.<br> Note : Clicking the buttons with your mouse will not perform any action. <br><br> </html>" );
  109. //set the bold font for info
  110. info.setFont(new Font("Verdana",Font.BOLD,14));
  111.  
  112. /*set the layout and place compomnet in place and pack it */
  113. setLayout(new BorderLayout());
  114. /*Various panel for the layout */
  115. JPanel jpNorth = new JPanel();
  116. JPanel jpCenter = new JPanel();
  117. JPanel jpKeyboard = new JPanel();
  118. JPanel jpNote = new JPanel();
  119. add( jpNorth, BorderLayout.NORTH);
  120. add( jpNote);
  121. add( jpCenter, BorderLayout.CENTER);
  122. add(jpKeyboard, BorderLayout.SOUTH);
  123.  
  124. jpNorth.setLayout(new BorderLayout());
  125. jpNorth.add(info, BorderLayout.WEST);
  126. jpNorth.add(info, BorderLayout.SOUTH);
  127.  
  128. jpCenter.setLayout( new BorderLayout());
  129. jpCenter.add(text, BorderLayout.WEST);
  130. jpCenter.add(text, BorderLayout.CENTER);
  131.  
  132. //add(text,BorderLayout.WEST);
  133. // add(scrollPane,BorderLayout.CENTER);
  134.  
  135. //layout for keyboard
  136. jpKeyboard.setLayout(new GridLayout(5,1));
  137. //pack the components
  138. pack();
  139.  
  140. /*paint first keyboard row and add it to the keyboard*/
  141. first = new JButton[firstRow.length];
  142. //get the panel for the row
  143. JPanel p = new JPanel(new GridLayout(1, firstRow.length));
  144. for(int i = 0; i < firstRow.length; ++i)
  145. {
  146. JButton b= new JButton(firstRow[i]);
  147. b.setPreferredSize(new Dimension(100,50));
  148. first[i] = b;
  149. p.add(first[i]);
  150. }
  151. jpKeyboard.add(p);
  152.  
  153. /*paint second keyboard row and add it to the keyboard*/
  154. second = new JButton[secondRow.length];
  155. //get the panel for the row
  156. p = new JPanel(new GridLayout(1, secondRow.length));
  157. for(int i = 0; i < secondRow.length; ++i)
  158. {
  159. second[i] = new JButton(secondRow[i]);
  160. p.add(second[i]);
  161.  
  162. }
  163. jpKeyboard.add(p);
  164.  
  165. /*paint third keyboard row and add it to the keyboard*/
  166.  
  167. third = new JButton[thirdRow.length];
  168. //get the panel for the row
  169. p = new JPanel(new GridLayout(1, thirdRow.length));
  170. for(int i = 0; i < thirdRow.length; ++i)
  171. {
  172. third[i] = new JButton(thirdRow[i]);
  173. p.add(third[i]);
  174. }
  175. jpKeyboard.add(p);
  176.  
  177. /*paint fourth keyboard row and add it to the keyboard*/
  178. fourth = new JButton[fourthRow.length];
  179. //get the panel for the row
  180. p = new JPanel(new GridLayout(1, fourthRow.length));
  181. for(int i = 0; i < fourthRow.length; ++i)
  182. {
  183. fourth[i] = new JButton(fourthRow[i]);
  184. p.add(fourth[i]);
  185. if(i==fourthRow.length-2)
  186. p.add(new JPanel());
  187.  
  188. }
  189. p.add(new JPanel());
  190. jpKeyboard.add(p);
  191.  
  192. //paint the fifth row
  193. fifth = new JButton[fifthRow.length];
  194. //get the panel for the row
  195. p = new JPanel(new GridLayout(1, fifthRow.length));
  196. /*put empty panel for layout adjustments */
  197. for(int i = 0; i < 1; ++i)
  198. {
  199. JPanel spacePanel = new JPanel();
  200. p.add(spacePanel);
  201. }
  202. /*draw the buttons */
  203. for(int i = 0; i < fifthRow.length; ++i)
  204. {
  205. if(i==1) //space bar panel
  206. {
  207. JButton b = new JButton(fifthRow[i]);
  208. b.setPreferredSize(new Dimension(400,10));
  209. b.setBounds(10, 10, 600, 100);
  210. fifth[i]=b;
  211. //add empty panels for layout
  212. p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());
  213. }
  214. else
  215. {
  216. fifth[i]=new JButton(fifthRow[i]);
  217. }
  218. if(i==0) //first black panel
  219. {
  220. //place a black panel at first
  221. JPanel spacePanel = new JPanel();
  222. p.add(spacePanel);
  223. }
  224.  
  225. p.add(fifth[i]);
  226.  
  227. }
  228. jpKeyboard.add(p);
  229. /*add listeners */
  230. getContentPane().addKeyListener(this);
  231. text.addKeyListener(this);
  232. /*add listeners to all the button */
  233. for(JButton b : first)
  234. b.addKeyListener(this);
  235.  
  236. for(JButton b : second)
  237. b.addKeyListener(this);
  238. for(JButton b : third)
  239. b.addKeyListener(this);
  240.  
  241. for(JButton b : fourth)
  242. b.addKeyListener(this);
  243.  
  244. for(JButton b : fifth)
  245. b.addKeyListener(this);
  246.  
  247. } //end of initWidgets
  248.  
  249. @Override
  250. public void keyTyped(KeyEvent e) {
  251. }
  252. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement