Advertisement
madhawaseeeee

jframe_problem

Dec 17th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JRootPane;
  16. import javax.swing.JTextField;
  17. import javax.swing.SwingConstants;
  18.  
  19.  
  20. public class Interface extends JFrame implements ActionListener {
  21.  
  22. private Container contentPane;
  23.  
  24. private JPanel buttonPanel, userPanel;
  25.  
  26. private JButton loginButton, createUserButton, logoutButton, withdrawButton, depositButton, switchToRegisterButton, switchToLoginButton;
  27.  
  28. private JLabel headerLabel, inputTopJTFLabel, inputPW1JPFLabel, toastLabel, inputPW2JPFLabel;
  29.  
  30. public JTextField inputTopJTF;
  31. public JPasswordField inputPW1JPF, inputPW2JPF;
  32.  
  33. JRootPane rootPane;
  34.  
  35. public Interface(int width, int height, String title) {
  36.  
  37. //Setting up Interface
  38. setTitle(title);
  39. setSize(width, height);
  40. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. setLocation(100,100);
  42.  
  43.  
  44. Font header = new Font("TimesRoman", Font.PLAIN, 50);
  45.  
  46. ///////////////////////BUTTON PANEL///////////////////////
  47.  
  48. //Button Panel
  49. buttonPanel = new JPanel();
  50.  
  51. //Buttons
  52. //loginButton
  53. loginButton = new JButton("Login");
  54. loginButton.addActionListener(this);
  55. loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  56.  
  57. //switchToRegisterButton
  58. switchToRegisterButton = new JButton("New User?");
  59. switchToRegisterButton.addActionListener(this);
  60. switchToRegisterButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  61.  
  62. //switchToLoginButton
  63. switchToLoginButton = new JButton("Switch to Login");
  64. switchToLoginButton.addActionListener(this);
  65. switchToLoginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  66. switchToLoginButton.setVisible(false);
  67.  
  68. //createUserButton
  69. createUserButton = new JButton("Register");
  70. createUserButton.addActionListener(this);
  71. createUserButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  72. createUserButton.setVisible(false);
  73.  
  74. //logoutButton
  75. logoutButton = new JButton("Logout");
  76. logoutButton.addActionListener(this);
  77. logoutButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  78. logoutButton.setVisible(false);
  79.  
  80. //withdrawButton
  81. withdrawButton = new JButton("Withdraw");
  82. withdrawButton.addActionListener(this);
  83. withdrawButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  84. withdrawButton.setVisible(false);
  85.  
  86. //depositButton
  87. depositButton = new JButton("Deposit");
  88. depositButton.addActionListener(this);
  89. depositButton.setAlignmentX(Component.CENTER_ALIGNMENT);
  90. depositButton.setVisible(false);
  91.  
  92. //Adding items to buttonPanel
  93. buttonPanel.add(loginButton);
  94. buttonPanel.add(switchToRegisterButton);
  95. buttonPanel.add(switchToLoginButton);
  96. buttonPanel.add(createUserButton);
  97. buttonPanel.add(logoutButton);
  98. buttonPanel.add(withdrawButton);
  99. buttonPanel.add(depositButton);
  100.  
  101. ///////////////BODY PANEL//////////////////////
  102.  
  103. //Body Panel
  104. userPanel = new JPanel();
  105. userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.PAGE_AXIS));
  106.  
  107. //JTextFields
  108. //inputTopJTF
  109. Dimension inputTopJTFDimension = new Dimension(100, 20);
  110. inputTopJTF = new JTextField();
  111. inputTopJTF.setMaximumSize(inputTopJTFDimension);
  112.  
  113. //inputPW1JPF
  114. Dimension inputPW1JPFDimension = new Dimension(100, 20);
  115. inputPW1JPF = new JPasswordField();
  116. inputPW1JPF.setMaximumSize(inputPW1JPFDimension);
  117.  
  118. //inputPW2JPF
  119. Dimension inputPW2JPFDimension = new Dimension(100, 20);
  120. inputPW2JPF = new JPasswordField();
  121. inputPW2JPF.setMaximumSize(inputPW2JPFDimension);
  122. inputPW2JPF.setVisible(false);
  123.  
  124. //JLabels
  125. //toastLabel
  126. toastLabel = new JLabel("Please sign in or create new account.");
  127. toastLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
  128.  
  129. //inputTopJTFLabel
  130. inputTopJTFLabel = new JLabel("Username:");
  131. inputTopJTFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
  132.  
  133. //inputPW1JPFLabel
  134. inputPW1JPFLabel = new JLabel("Password:");
  135. inputPW1JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
  136.  
  137. //inputPW2JPFLabel
  138. inputPW2JPFLabel = new JLabel("Confirm Password:");
  139. inputPW2JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
  140. inputPW2JPFLabel.setVisible(false);
  141.  
  142. //Adding items to userPanel
  143. userPanel.add(toastLabel);
  144. userPanel.add(inputTopJTFLabel);
  145. userPanel.add(inputTopJTF);
  146. userPanel.add(inputPW1JPFLabel);
  147. userPanel.add(inputPW1JPF);
  148. userPanel.add(inputPW2JPFLabel);
  149. userPanel.add(inputPW2JPF);
  150.  
  151. ///////////////CONTENT PANE/////////////////////
  152.  
  153. //Content Pane
  154. contentPane = getContentPane();
  155.  
  156. //JLabels
  157. //headerLabel
  158. headerLabel = new JLabel("Bank", SwingConstants.CENTER);
  159. headerLabel.setFont(header);
  160.  
  161. //PAGE_START
  162. contentPane.add(headerLabel, BorderLayout.PAGE_START);
  163.  
  164. //LINE_START
  165.  
  166. //CENTER
  167. contentPane.add(userPanel, BorderLayout.CENTER);
  168.  
  169. //LINE_END
  170.  
  171. //PAGE_END
  172. contentPane.add(buttonPanel, BorderLayout.PAGE_END);
  173.  
  174. userPanel.setFocusable(true);
  175. userPanel.requestFocus();
  176.  
  177. //Default Button
  178. rootPane = getRootPane();
  179. rootPane.setDefaultButton(loginButton);
  180. setVisible(true);
  181.  
  182. }
  183.  
  184. @Override
  185. public void actionPerformed(ActionEvent e) {
  186. System.out.println("hi");
  187. }
  188.  
  189. public static void main(String[] args) {
  190. new Interface(500, 400, "hii");
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement