0

I am trying to make a simple GUI that has the username and password with the textfields under each one. Any help would be greatly appreciated!

Here is my code. I cannot figure out how to get everything in it's rightful place.

enter code here
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
/**
*/
public class PassWordFrame extends JFrame
{
   private static final int FIELD_WIDTH = 10;
   private static final int FRAME_WIDTH = 500;
   private static final int FRAME_HEIGHT = 400;
   private JLabel instruct;
   private JLabel username;
   private JLabel password;
   private JTextField usertext;
   private JTextField passtext;
   private JButton login;
   private ActionListener listener;
   String text = "";

   public PassWordFrame()
   {
      createComponents();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
      listener = new ClickListener();
   }
   class ClickListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         System.out.println("Hello");
      }
   }
   public void createComponents()
   {
      Color blue = new Color(0,128,155);
      Font font = new Font("Times New Roman", Font.BOLD, 14);
      instruct = new JLabel("Please enter your username and password.");
      instruct.setFont(font);
      username = new JLabel("Username: ");
      username.setFont(font);
      password = new JLabel("Password: ");
      password.setFont(font);
      usertext = new JTextField(FIELD_WIDTH);
      passtext = new JTextField(FIELD_WIDTH);
      login = new JButton("Login");
      login.setFont(font);
      instruct.setForeground(Color.BLACK);
      login.setForeground(Color.BLACK);
      username.setForeground(Color.BLACK);
      password.setForeground(Color.BLACK);
      login.addActionListener(listener);
      JPanel panel = new JPanel();
      panel.setBackground(blue);
      panel.add(instruct);
      panel.add(username);
      panel.add(usertext);
      panel.add(password);
      panel.add(passtext);
      panel.add(login);
      add(panel);
   }
}      
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dustin
  • 185
  • 3
  • 12
  • 1
    Provide ASCII art or a simple drawing of how the GUI should appear at default size and (if resizable) with extra width/height. – Andrew Thompson Sep 08 '14 at 16:42
  • 1
    `public class PassWordFrame extends JFrame` should probably be `public class PassWordDialog extends JDialog`.. either that, or just use a `JOptionPane`. – Andrew Thompson Sep 08 '14 at 16:45
  • 1
    BTW If you mean labels in one column, fields in another, you might consider `GroupLayout` for this, as seen in [this answer](http://stackoverflow.com/a/21659516/418556). The factory method may seem overkill for 4 components, but works well for many components. – Andrew Thompson Sep 08 '14 at 16:53

1 Answers1

0

Change the line where you create the panel to

JPanel panel = new JPanel(new GridLayout(2,2));
user1207177
  • 577
  • 3
  • 16