Guest User

Untitled

a guest
Jul 30th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. Override a frames next/prev buttons using cardLayout
  2. public class MainFrame {
  3.  
  4. private static void createAndShowGUI() {
  5. JFrame frame = new JFrame("Dimensions helper");
  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. frame.setLocationRelativeTo(null);
  8.  
  9. final JPanel contentPane = new JPanel();
  10. contentPane.setLayout(new CardLayout(200, 200));
  11.  
  12. Window1 win1 = new Window1();
  13. contentPane.add(win1);
  14. Window2 win2 = new Window2();
  15. contentPane.add(win2);
  16. Window3 win3 = new Window3();
  17. contentPane.add(win3);
  18.  
  19. JPanel buttonPanel = new JPanel();
  20. final JButton previousButton = new JButton("< PREVIOUS");
  21. final JButton nextButton = new JButton("NEXT >");
  22. final JButton cancelButton = new JButton("CANCEL");
  23. buttonPanel.add(cancelButton);
  24. buttonPanel.add(previousButton);
  25. buttonPanel.add(nextButton);
  26.  
  27. previousButton.addActionListener(new ActionListener() {
  28. public void actionPerformed(ActionEvent ae) {
  29. Verifiable verifiable = null;
  30. Component[] contents = contentPane.getComponents();
  31. for(Component component : contents) {
  32. if(component.isVisible() && component instanceof Verifiable) {
  33. verifiable = (Verifiable)component;
  34. }
  35. }
  36. if(verifiable != null && verifiable.isDataValid()) {
  37. CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  38. cardLayout.previous(contentPane);
  39. }
  40. }
  41. });
  42.  
  43. nextButton.addActionListener(new ActionListener() {
  44. public void actionPerformed(ActionEvent ae) {
  45. Verifiable verifiable = null;
  46. Component[] contents = contentPane.getComponents();
  47. for(Component component : contents) {
  48. if(component.isVisible() && component instanceof Verifiable) {
  49. verifiable = (Verifiable)component;
  50. }
  51. }
  52. if(verifiable != null && verifiable.isDataValid()) {
  53. CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  54. cardLayout.next(contentPane);
  55. }
  56. }
  57. });
  58.  
  59. cancelButton.addActionListener(new ActionListener() {
  60. public void actionPerformed(ActionEvent ae) {
  61. System.exit(0);
  62. }
  63.  
  64. });
  65.  
  66. frame.add(contentPane);
  67. frame.add(buttonPanel, BorderLayout.PAGE_END);
  68. frame.pack();
  69. frame.setVisible(true);
  70. }
  71.  
  72. public static void main(String[] args) {
  73. SwingUtilities.invokeLater(new Runnable() {
  74. public void run() {
  75. createAndShowGUI();
  76. }
  77. });
  78. } }
  79.  
  80. import javax.swing.*;
  81. import java.awt.*;
  82. import java.awt.event.*;
  83. import javax.swing.JPanel;
  84.  
  85. public class Window1 extends JPanel implements Verifiable {
  86.  
  87. JTextField txtUsername = new JTextField();
  88. JPasswordField txtPassword = new JPasswordField();
  89.  
  90. public Window1() {
  91. init();
  92. }
  93.  
  94. private void init() {
  95. setLayout(new GridLayout(2, 2));
  96. JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
  97. txtUsername = new JTextField();
  98.  
  99. JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
  100. txtPassword = new JPasswordField();
  101.  
  102. add(lblUsername);
  103. add(txtUsername);
  104. add(lblPassword);
  105. add(txtPassword);
  106. String title = "Log in";
  107. setBorder(BorderFactory.createTitledBorder(title));
  108. }
  109.  
  110. @Override
  111. public boolean isDataValid() {
  112. if(txtUsername.getText().equals("foo") &&
  113. java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray())) {
  114. return true;
  115. } else {
  116. JOptionPane.showMessageDialog(this, "Fel användarnamn och/eller lösenord",
  117. "Error", JOptionPane.ERROR_MESSAGE);
  118. return false;
  119. }
  120. }
  121. }
  122.  
  123. interface Verifiable {
  124. boolean isDataValid();
  125. }
  126.  
  127. class Window1 extends JPanel implements Verifiable {
  128.  
  129. JTextField txtUsername = new JTextField();
  130. JPasswordField txtPassword = new JPasswordField();
  131.  
  132. public Window1() {
  133. init();
  134. }
  135.  
  136. private void init() {
  137.  
  138. setLayout(new GridLayout(2, 2));
  139. JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
  140.  
  141. JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
  142.  
  143. add(lblUsername);
  144. add(txtUsername);
  145. add(lblPassword);
  146. add(txtPassword);
  147. String title = "Use "foo" and "bar"";
  148. setBorder(BorderFactory.createTitledBorder(title ));
  149. }
  150.  
  151. @Override
  152. public boolean isDataValid() {
  153. return txtUsername.getText().equals("foo") &&
  154. java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray());
  155. }
  156. }
  157.  
  158. nextButton.addActionListener(new ActionListener() {
  159. public void actionPerformed(ActionEvent ae) {
  160. Verifiable verifiable = null;
  161. Component[] contents = contentPane.getComponents();
  162. for (Component component : contents) {
  163. if (component.isVisible() && component instanceof Verifiable) {
  164. verifiable = (Verifiable) component;
  165. }
  166. }
  167. if (verifiable != null && verifiable.isDataValid()) {
  168. CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  169. cardLayout.next(contentPane);
  170. }
  171.  
  172. }
  173. });
Add Comment
Please, Sign In to add comment