How to open a frame from a button

Hi, I have 2 frames, frame1 and frame2. in frame1 I have a jButton1 when I click jButton1 the idear is to open frame2, how do I do that?

best regards

kurg

here is my code from Jbuilder

package button;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {

JPanel contentPane;

XYLayout xYLayout1 = new XYLayout();

JButton jButton1 = new JButton();

/**Construct the frame*/

public Frame1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

/**Component initialization*/

private void jbInit() throws Exception {

jButton1.setText("jButton1");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(xYLayout1);

this.setSize(new Dimension(455, 370));

this.setTitle("Frame Title");

contentPane.add(jButton1, new XYConstraints(60, 99, 110, 81));

}

/**Overridden so we can exit when window is closed*/

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

void jButton1_actionPerformed(ActionEvent e) {

}

}

package button;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

public class Frame2 extends JFrame {

JPanel contentPane;

XYLayout xYLayout1 = new XYLayout();

JLabel jLabel1 = new JLabel();

/**Construct the frame*/

public Frame2() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

/**Component initialization*/

private void jbInit() throws Exception {

jLabel1.setText("jLabel1");

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame2.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(xYLayout1);

this.setSize(new Dimension(400, 300));

this.setTitle("Frame Title");

contentPane.add(jLabel1, new XYConstraints(126, 53, 58, 43));

}

/**Overridden so we can exit when window is closed*/

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

}

[2926 byte] By [kurgan008] at [2007-9-27 18:23:23]
# 1

Frame f1;

Frame f2;

Button b = new Button ("hi");

f1.add(b);

b.addActionListener(new ActionListener() {

public void actionPerformed (ActionEvent ae) {

f2.setBounds(...)

f2.setVisible(true);

}));

b.setBounds(...);

f1.setBounds(...);

f1.setVisible(true)

tjacobs01 at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Check out the java swing tutorials(trails). They explain all this stuff
tjacobs01 at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
just go to my site www.geocities.com\yahiasylhetyand click on brain check you can get a complete code from here bye.
mdyahiakhan at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
just go to my site www.geocities.com\yahiasylhetyand click on brain check you can get a complete code from here bye.
mdyahiakhan at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Frame1 extends JFrame

{

JPanel contentPane;

// XYLayout xYLayout1 = new XYLayout();

JButton jButton1 = new JButton();

public Frame1()

{

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try

{

jbInit();

}

catch(Exception e)

{

e.printStackTrace();

}

}

private void jbInit() throws Exception

{

jButton1.setText("jButton1");

jButton1.setBounds(60, 99, 110, 81);

jButton1.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(ActionEvent e)

{

jButton1_actionPerformed(e);

}

});

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

this.setSize(new Dimension(455, 370));

this.setTitle("Frame Title");

contentPane.add(jButton1);

setVisible(true);

}

/**Overridden so we can exit when window is closed*/

protected void processWindowEvent(WindowEvent e)

{

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING)

{

System.exit(0);

}

}

void jButton1_actionPerformed(ActionEvent e)

{

new Frame2();

}

}

............................................................

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Frame2 extends JFrame

{

JPanel contentPane;

//XYLayout xYLayout1 = new XYLayout();

JLabel jLabel1 = new JLabel();

public Frame2()

{

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try

{

jbInit();

}

catch(Exception e)

{

e.printStackTrace();

}

}

private void jbInit() throws Exception

{

jLabel1.setText("jLabel1");

jLabel1.setBounds(126, 53, 58, 43);

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame2.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

this.setSize(new Dimension(400, 300));

this.setTitle("Frame Title");

contentPane.add(jLabel1);

setVisible(true);

}

/**Overridden so we can exit when window is closed*/

protected void processWindowEvent(WindowEvent e)

{

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING)

{

System.exit(0);

}

}

}

noah.w at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
thx for the help krug
kurgan008 at 2007-7-6 18:56:40 > top of Java-index,Archived Forums,New To Java Technology Archive...