| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class BorderLayoutExample implements ActionListener{ JPanel textPanel, panelForTextFields, completionPanel; JLabel titleLabel, usernameLabel, passwordLabel, userLabel, passLabel; JTextField usernameField, loginField; JButton loginButton; public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. //This sets the Border Layout to have a horizontal gap of 10 //and a vertical gap of 10 between each widget. JPanel totalGUI = new JPanel(); totalGUI.setLayout(new BorderLayout(10, 10)); // Setting the preferredSize is basically making sure the widget // is not re-sized to be smaller or bigger than this unless it needs to be. // Also, when adding, we have the area of the JPanel as a second argument. titleLabel = new JLabel("Login Screen"); titleLabel.setPreferredSize(new Dimension(290, 30)); titleLabel.setHorizontalAlignment(0); totalGUI.add(titleLabel, BorderLayout.PAGE_START); // Creation of a Panel to contain the JLabels textPanel = new JPanel(); textPanel.setPreferredSize(new Dimension(70, 80)); totalGUI.add(textPanel, BorderLayout.LINE_START); // Username Label usernameLabel = new JLabel("Username"); usernameLabel.setPreferredSize(new Dimension(70, 30)); usernameLabel.setHorizontalAlignment(4); textPanel.add(usernameLabel); // Login Label passwordLabel = new JLabel("Password"); passwordLabel.setPreferredSize(new Dimension(70, 30)); passwordLabel.setHorizontalAlignment(4); textPanel.add(passwordLabel); // TextFields Panel Container panelForTextFields = new JPanel(); panelForTextFields.setPreferredSize(new Dimension(100, 70)); totalGUI.add(panelForTextFields, BorderLayout.CENTER); // Username Textfield usernameField = new JTextField(8); usernameField.setPreferredSize(new Dimension(100, 30)); panelForTextFields.add(usernameField); // Login Textfield loginField = new JTextField(8); loginField.setPreferredSize(new Dimension(100, 30)); panelForTextFields.add(loginField); // Creation of a Panel to contain the completion JLabels completionPanel = new JPanel(); completionPanel.setPreferredSize(new Dimension(70, 80)); totalGUI.add(completionPanel, BorderLayout.LINE_END); // Username Label userLabel = new JLabel("Wrong"); userLabel.setForeground(Color.red); userLabel.setPreferredSize(new Dimension(70, 30)); completionPanel.add(userLabel); // Login Label passLabel = new JLabel("Wrong"); passLabel.setForeground(Color.red); passLabel.setPreferredSize(new Dimension(70, 30)); completionPanel.add(passLabel); // Button for Logging in loginButton = new JButton("Login"); loginButton.addActionListener(this); totalGUI.add(loginButton, BorderLayout.PAGE_END); totalGUI.setOpaque(true); return totalGUI; } // With this action performed, we simply check to see if the username and // password match "Bob" as the username and "Robert" as the password. // If they do, we set the labels ajacent to them to "Correct!" and color // them green. // At the end, we check if both labels are green. If they are, we set the // screen to be 'Logging In'. public void actionPerformed(ActionEvent e) { if(e.getSource() == loginButton) { if(usernameField.getText().trim().compareTo("Bob") == 0) { userLabel.setForeground(Color.green); userLabel.setText("Correct!"); } else { userLabel.setForeground(Color.red); userLabel.setText("Wrong!"); } if(loginField.getText().trim().compareTo("Robert") == 0) { passLabel.setForeground(Color.green); passLabel.setText("Correct!"); } else { passLabel.setForeground(Color.red); passLabel.setText("Wrong!"); } if((userLabel.getForeground() == Color.green) && (passLabel.getForeground() == Color.green)) { titleLabel.setText("Logging in...."); loginButton.setEnabled(false); } } } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] JTextField of Dreams [=]"); BorderLayoutExample demo = new BorderLayoutExample(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // We no longer manually re-size, we use pack to automatically size the frame. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |
| 22 | totalGUI.setLayout(new BorderLayout(10, 10)); |
| Absolute Positioning | Border Layout | |||||
|
|
| 83 84 85 | loginButton = new JButton("Login"); loginButton.addActionListener(this); totalGUI.add(loginButton, BorderLayout.PAGE_END); |
