0

Actually I am New to java I want to know how to link two frames. I made two different java files, which looks simple. But I am unable to solve it till now. It should open the welcome frame by clicking Next Button in Enter.java. Can anybody help me?

Here is my code:

Enter.java

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Enter extends JFrame {

public Enter() {
// TODO Auto-generated constructor stub

setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

add(new JLabel("Name"));
add(new JTextField(8));
add(new JLabel("Address"));
add(new JTextField(15));
add(new JLabel("Surname"));
add(new JTextField(8));
add(new JLabel("Phone"));
add(new JTextField(8));
add(new JButton("Next"));
}


/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
    Enter myProg = new Enter();
myProg.setTitle("Ceng 344 - Lab 7");
myProg.setSize(450, 400);
myProg.setLocationRelativeTo(null);
myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProg.setVisible(true);
}

}

Welcome.java
 import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class welcome extends JFrame {

public welcome() {
// TODO Auto-generated constructor stub

setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

add(new JLabel("Welcome"));
}


/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
welcome myProg = new welcome();
myProg.setTitle("Ceng 344 - Lab 7");
myProg.setSize(450, 400);
myProg.setLocationRelativeTo(null);
myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProg.setVisible(true);
}

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

0

If you need to call the Welcome Frame from the Next Button, so that you can add the action to this button.

Try this:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JFrame {

    public Main() {
// TODO Auto-generated constructor stub

        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

        add(new JLabel("Name"));
        add(new JTextField(8));
        add(new JLabel("Address"));
        add(new JTextField(15));
        add(new JLabel("Surname"));
        add(new JTextField(8));
        add(new JLabel("Phone"));
        add(new JTextField(8));
        JButton next = new JButton("Next");
        add(next);
        next.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }

            private void NextActionPerformed(ActionEvent evt) {
                welcome myProg = new welcome();
                myProg.setTitle("Ceng 344 - Lab 7");
                myProg.setSize(450, 400);
                myProg.setLocationRelativeTo(null);
                myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                myProg.setVisible(true);
            }
        });



    }

    /**
     * @param args
     */
    public static void main(String[] args) {
// TODO Auto-generated method stub
        Main myProg = new Main();
        myProg.setTitle("Ceng 344 - Lab 7");
        myProg.setSize(450, 400);
        myProg.setLocationRelativeTo(null);
        myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myProg.setVisible(true);
    }
}

class welcome extends JFrame {

    public welcome() {
// TODO Auto-generated constructor stub

        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
        add(new JLabel("Welcome"));
    }
    /**
     * @param args
     */
}

i remove the Welcome's Main class , and add it's content to the NextActionPerformed method

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Alya'a Gamal
  • 5,624
  • 19
  • 34
  • Thank it worked. But can you explain me this code. What does it do? private void NextActionPerformed(ActionEvent evt) { welcome myProg = new welcome(); myProg.setTitle("Ceng 344 - Lab 7"); myProg.setSize(450, 400); myProg.setLocationRelativeTo(null); myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myProg.setVisible(true); } – Nachinne_Manche Mar 06 '13 at 08:17
  • Ok, you need to add action to next button , when you click on it , it call another frame , so i create Next button **JButton next = new JButton("Next");** , then i add an action to it , this action called **NextActionPerformed** , this method called the **Welcome** Frame – Alya'a Gamal Mar 06 '13 at 08:20
  • This part which you call it in **Welcome** Main method , so i don't need it in the main method , this is the **Next** button action – Alya'a Gamal Mar 06 '13 at 08:22
  • i think that you need to read in Java GUI , so try this : http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html – Alya'a Gamal Mar 06 '13 at 08:27
0

First of all

  1. You need to add ActionListener to the JButton
  2. You can set the properties of JFrame inside constructor.
  3. You don't need main() in welcome.java, only `constructor will do it.

Enter.java

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Enter extends JFrame implements ActionListener {
public Enter(){
    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
    add(new JLabel("Name"));
    add(new JTextField(8));
    add(new JLabel("Address"));
    add(new JTextField(15));
    add(new JLabel("Surname"));
    add(new JTextField(8));
    add(new JLabel("Phone"));
    add(new JTextField(8));
    JButton next = new JButton("Next");
    next.addActionListener(this);
    add(next);
}

public void actionPerformed(ActionEvent ae){
    //if your frame contains more than one button then you can check which button is clicked using ae.getSource() or using ae.getActionCommand()
    new Welcome();
}
public static void main(String[] args) {
    Enter myProg = new Enter();
    myProg.setTitle("Ceng 344 - Lab 7");
    myProg.setSize(450, 400);
    myProg.setLocationRelativeTo(null);
    myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myProg.setVisible(true);
}
}

//Welcome

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Welcome extends JFrame {
    public Welcome() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
        add(new JLabel("Welcome"));
        setTitle("Ceng 344 - Lab 7");
        setSize(450, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}
Bhushan
  • 6,151
  • 13
  • 58
  • 91