0

I am a newbie in Java, and I want to create the first step in a big system. I was trying to use Eclipse WindowsBuilder for this task, but I got a lot of code I don't understand, and furthermore, I don't know how to proceed...

What I want to do is simple. When I run my program, I want a small window (frame?) to come up with a label "user name" and a text field next to it, below I want another label "password" with a password field next to it. Below that, I want a button "submit", that when pressed, makes this window/frame close, and opens a new window/frame, bigger, in which I will put all sorts of things. This is my code, it creates the first window, I don't know how to make it do what I want when I press the button. Additionally, I am not sure how to create a new class for the new frame, without a main method, will it work ?

Thank you for any sort of help and guidance you can provide (if there is a nicer way or writing it without windowsbuilder - I am up for it).

package HR;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SignIn extends JFrame {

    private JPanel contentPane;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SignIn frame = new SignIn();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SignIn() {
        setTitle("SYSTEM");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 308, 179);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new FormLayout(new ColumnSpec[] {
                FormFactory.RELATED_GAP_COLSPEC,
                FormFactory.DEFAULT_COLSPEC,
                FormFactory.RELATED_GAP_COLSPEC,
                FormFactory.DEFAULT_COLSPEC,
                FormFactory.RELATED_GAP_COLSPEC,
                FormFactory.DEFAULT_COLSPEC,
                FormFactory.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow"),},
            new RowSpec[] {
                FormFactory.RELATED_GAP_ROWSPEC,
                FormFactory.DEFAULT_ROWSPEC,
                FormFactory.RELATED_GAP_ROWSPEC,
                FormFactory.DEFAULT_ROWSPEC,
                FormFactory.RELATED_GAP_ROWSPEC,
                FormFactory.DEFAULT_ROWSPEC,
                FormFactory.RELATED_GAP_ROWSPEC,
                FormFactory.DEFAULT_ROWSPEC,}));

        JLabel lblNewLabel = new JLabel("User Name");
        contentPane.add(lblNewLabel, "4, 4, right, default");

        final JFormattedTextField username = new JFormattedTextField();
        contentPane.add(username, "6, 4, 3, 1, fill, default");

        JLabel lblNewLabel_1 = new JLabel("Password");
        contentPane.add(lblNewLabel_1, "4, 6, right, default");

        passwordField = new JPasswordField();
        contentPane.add(passwordField, "6, 6, 3, 1, fill, default");

        JButton btnSignIn = new JButton("Sign In");
        btnSignIn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                username.setText("Me");
            }
        });
        contentPane.add(btnSignIn, "6, 8");
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3275222
  • 225
  • 3
  • 12
  • Here's an example of simple [Login](http://stackoverflow.com/a/20286447/2587435) using a `JDilog` instead of a `JFrame` – Paul Samsotha Feb 11 '14 at 12:19

2 Answers2

0

The right place to creat the new window is here

        public void actionPerformed(ActionEvent arg0) 
        {

        }

You can do it like this

        public void actionPerformed(ActionEvent arg0) 
        {
            dispose();                //close old frame
            JFrame f = new JFrame("test");    //creat new frame
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new JPanel());      //add new panel
            f.setVisible(true);       //make it visible
            f.setSize(768,1024);      //set the size
        }

you only have to replace the new JPanel() with your class extends jpanel. and you are right, you need no main method in your class. all events will be triggered by listeners on buttons etc.

kai
  • 6,702
  • 22
  • 38
0
   public void actionPerformed(ActionEvent arg0) 
    {
        dispose();              
        JFrame newFrame = new JFrame("newJFrame");    
        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        newFrame.add(new JPanel());     
        newFrame.setSize(200,200);     //set this to any size you wish             
        newFrame.setVisible(true);             
    }

Just replace the new JPanel with the class that extends JPanel rest the listeners on the buttons will take care. All the Best!

D3X
  • 547
  • 3
  • 20