0

I wrote a program, it's a login window with two text fields and two button, the actionPerformed method at this stage is for testing purpose.

My problem is, I put all the component in the constructor, I don't know how to write my main() method in order to get the program running, I tried something like to grab a reference of the class, set visibility, but none of them works.

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JPasswordField;
 import javax.swing.JTextField;


 public class LoginUI extends JFrame implements ActionListener
 {


JTextField Tusername;
JTextField Tpassword;
JButton Login = new JButton("Login");
JButton register = new JButton("Register");
JLabel passwordLabel = new JLabel("Password");
JLabel userLabel = new JLabel("User");

public String username;
public String password;

public LoginUI()
{

    JFrame frame = new JFrame("Login or register");
    JPanel Panel = new JPanel();
    frame.add(Panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Login.setBounds(10, 80, 80, 25);
    Panel.add(Login);
    Login.addActionListener(this);

    register.setBounds(180, 80, 80, 25);
    Panel.add(register);
    register.addActionListener(this);

    Panel.setLayout(null);

    JLabel userLabel = new JLabel("User");
    userLabel.setBounds(10, 10, 80, 25);
    Panel.add(userLabel);

    Tusername = new JTextField(20);
    Tusername.setBounds(100, 10, 160, 25);
    Panel.add(Tusername);

    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(10, 40, 80, 25);
    Panel.add(passwordLabel);

    Tpassword = new JPasswordField(20);
    Tpassword.setBounds(100, 40, 160, 25);
    Panel.add(Tpassword);

}


public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == Login) 
    {
        String username = Tusername.getText(); 
        System.out.println(username);
    }
    else if (e.getSource() == register)
    {
        String password = Tpassword.getText(); 
        System.out.println(password);
    }
}


public static void main(String args[])
{




 }}
halfer
  • 19,824
  • 17
  • 99
  • 186
A.Y
  • 137
  • 1
  • 9
  • 2
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Dec 02 '16 at 20:35

1 Answers1

3

Okay, there's a few things wrong here. First off is this part of the constructor:

JFrame frame = new JFrame("Login or register");

LoginUI is already a JFrame. You don't need to create another one. Replace that line with this:

super("Login or register");

and then change these lines:

frame.add(Panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

to this:

add(Panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

That will call the methods on the LoginUI instance itself, rather than a new JFrame that it creates.

Then, in your main method, you need to create a LoginUI, and set it to be visible. That will look something like this:

JFrame loginUI = new LoginUI();
loginUI.setVisible(true);

That code will bring up the fully populated frame. It will not, however, set the correct size for it. Ordinarily you could automatically size it with loginUI.pack(), but you use a null layout in your panel (which is pretty much universally considered a very bad idea). If you don't want to correct that, the only option is to determine what size it needs to be, and call loginUI.setSize() with those values. I strongly recommend not using a null layout though.

If you make all those changes, it should bring up the UI like you expect.

Community
  • 1
  • 1
resueman
  • 10,572
  • 10
  • 31
  • 45
  • Thank you very much, i set the size in main() method, i'm a new leaner of Java, your advice truly helped me alot – A.Y Dec 02 '16 at 20:43