Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4.  
  5.  
  6. public class OnScreenKeyboard implements ActionListener {
  7.  
  8. JFrame keyboard;
  9. static String keyboardKeys = "0123456789qwertyuiopasdfghjklzxcvbnm.< ";
  10. JButton[] keys = new JButton[39];
  11. GridLayout gl;
  12. FlowLayout fl;
  13. Dimension buttondimension;
  14. JPanel panel1, panel2;
  15.  
  16. JToggleButton capslock;
  17. private String message = "";
  18.  
  19. public String getMessage() {
  20. return message;
  21. }
  22.  
  23. public void setMessage(String message) {
  24. this.message = message;
  25. }
  26.  
  27. public OnScreenKeyboard() {
  28. buttondimension = new Dimension(45, 40);
  29.  
  30. fl = new FlowLayout();
  31.  
  32. capslock = new JToggleButton("capslock");
  33.  
  34. panel1 = new JPanel(fl);
  35. panel2 = new JPanel(fl);
  36.  
  37. char[] key = keyboardKeys.toCharArray();
  38.  
  39. for (int i = 0; i < 39; i++) {
  40. keys[i] = new JButton(String.valueOf(key[i]));
  41. keys[i].setFont(new Font("Arial", Font.PLAIN, 13));
  42. if (i == 38) {
  43. keys[i].setPreferredSize(new Dimension(100, 30));
  44. } else {
  45.  
  46. keys[i].setPreferredSize(buttondimension);
  47. }
  48. keys[i].addActionListener(this);
  49. }
  50.  
  51. keyboard = new JFrame("Keyboard");
  52. keyboard.setSize(720, 220);
  53. keyboard.setLocationRelativeTo(null);
  54. keyboard.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  55. keyboard.setResizable(false);
  56.  
  57. Container content = keyboard.getContentPane();
  58. content.setLayout(null);
  59. panel1.setBounds(1, 1, 500, 210);
  60. panel2.setBounds(510, 1, 200, 210);
  61.  
  62. for (int i = 0; i < 10; i++) {
  63. panel2.add(keys[i]);
  64. }
  65. for (int i = 10; i < 39; i++) {
  66. panel1.add(keys[i]);
  67. }
  68.  
  69. panel1.add(capslock);
  70.  
  71. content.add(panel1);
  72. content.add(panel2);
  73.  
  74. capslock.addActionListener(this);
  75.  
  76. keyboard.setVisible(true);
  77. }
  78.  
  79. public static void main(String[] args) {
  80. new OnScreenKeyboard();
  81.  
  82. }
  83.  
  84. public void reset(){
  85. message = "";
  86. }
  87.  
  88. @Override
  89. public void actionPerformed(ActionEvent e) {
  90.  
  91. for (int i = 0; i < 36; i++) {
  92. if (e.getSource() == keys[i]) {
  93. setMessage(getMessage() + keys[i].getText());
  94. break;
  95. }
  96. }
  97.  
  98. if (e.getSource() == capslock) {
  99. if (capslock.isSelected()) {
  100. for (int i = 10; i < 36; i++) {
  101. keys[i].setFont(new Font("Arial", Font.PLAIN, 12));
  102. keys[i].setText(keys[i].getText().toUpperCase());
  103.  
  104. }
  105. } else if (!capslock.isSelected()) {
  106. for (int i = 10; i < 36; i++) {
  107. keys[i].setFont(new Font("Arial", Font.PLAIN, 13));
  108. keys[i].setText(keys[i].getText().toLowerCase());
  109.  
  110. }
  111. }
  112. }
  113.  
  114. setMessage(getMessage());
  115.  
  116. //JOptionPane.showMessageDialog(null, getMessage());
  117.  
  118. }
  119.  
  120. }
  121.  
  122. public class LoginScreen implements ActionListener, FocusListener {
  123.  
  124. JFrame frame;
  125. Container content;
  126. FlowLayout fl;
  127.  
  128. JTextField txtusername, txtpassword;
  129. JLabel lblusername, lblpassword;
  130.  
  131. JPanel panel1, panel2;
  132.  
  133. JButton keyboard, signup, signin;
  134. OnScreenKeyboard kyb;
  135. Dimension text;
  136.  
  137. private void init() {
  138. text =new Dimension(100, 30);
  139.  
  140. fl = new FlowLayout(FlowLayout.CENTER);
  141. lblusername = new JLabel("enter username");
  142. lblpassword = new JLabel("enter password");
  143. txtusername = new JTextField();
  144. txtpassword = new JPasswordField();
  145.  
  146. keyboard = new JButton("keyboard");
  147. signup = new JButton("signup");
  148. signin = new JButton("sign in");
  149.  
  150. panel1 = new JPanel(fl);
  151. panel2 = new JPanel(fl);
  152.  
  153. keyboard = new JButton("keyboard");
  154.  
  155. txtusername.setPreferredSize(text);
  156. txtpassword.setPreferredSize(text);
  157.  
  158. kyb = new OnScreenKeyboard();
  159.  
  160. }
  161.  
  162. public LoginScreen() {
  163.  
  164. init();
  165. frame = new JFrame("BorderLayoutDemo");
  166. frame.setTitle("Registration Form");
  167. frame.setSize(300, 300);
  168. frame.setDefaultCloseOperation(3);
  169.  
  170. frame.setLocationRelativeTo(null);
  171. frame.setVisible(true);
  172.  
  173. content = frame.getContentPane();
  174.  
  175. content.setLayout(new GridLayout(2, 1));
  176.  
  177. panel1.add(lblusername);
  178. panel1.add(txtusername);
  179. panel1.add(lblpassword);
  180. panel1.add(txtpassword);
  181.  
  182. panel2.add(signup);
  183. panel2.add(signin);
  184. panel2.add(keyboard);
  185.  
  186. content.add(panel1);
  187. content.add(panel2);
  188. keyboard.addActionListener(this);
  189.  
  190. txtusername.addFocusListener(this);
  191. txtpassword.addFocusListener(this);
  192.  
  193. }
  194.  
  195. public static void main(String[] args) {
  196. new LoginScreen();
  197. }
  198.  
  199. @Override
  200. public void actionPerformed(ActionEvent e) {
  201.  
  202. if (!kyb.keyboard.isVisible()) {
  203. if (e.getSource() == keyboard) {
  204. kyb = new OnScreenKeyboard();
  205. }
  206. }
  207.  
  208. }
  209.  
  210.  
  211. @Override
  212. public void focusGained(FocusEvent e) {
  213. if(txtusername == e.getSource()){
  214. txtusername.setText(kyb.getMessage());
  215.  
  216. }else if(txtpassword == e.getSource()){
  217.  
  218. kyb.reset();
  219. txtpassword.setText(kyb.getMessage());
  220. }
  221. }
  222.  
  223. @Override
  224. public void focusLost(FocusEvent e) {
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement