Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple window closing problem 1

Status
Not open for further replies.

andypara

Programmer
Apr 25, 2002
32
GB
All I want to do is create a JFrame with one JButton. This I can do. When the button is pressed I want another JFrame to open. This I can also do. However, when the new window opens I want the old window to dissapear. I'm having real trouble with this and I would appreciate it if some one could give me two example classes to do this.

Please send to andrew.smith@vosperthornycroft.com

and copy in ajs@bliss27.freeserve.co.uk

I know this is a stupid problem. So far I have been using the "public static void main method" of the first class to pack and setVisble the first window. This works fine. I'm then trying to use a nested buttonhandler class to insantiate the next class, pack the frame, setVisible, but when I use this nested class to setVisible(false) the first window it doesn't compile, saying :-

M:\java\JAVASW~1\LunarPhases.java:146: cannot resolve symbol
symbol : variable lunarPhasesFrame
location: class LunarPhases.ButtonHandler
lunarPhasesFrame.isVisible(false);

I'd be happy to send both classes to anyone who wants them.

Many thanks.
 
Here are two simple frames, each with a button that loads the other and then disposes of itself. Frame1 has the main method...

<======== Frame 1 ===========>

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Frame1 extends JFrame
{
private JButton jButton1 = new JButton();

public Frame1()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}

}

private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle(&quot;Frame 1&quot;);
jButton1.setText(&quot;To Frame2&quot;);
jButton1.setBounds(new Rectangle(105, 130, 130, 50));
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jButton1, null);
}

private void jButton1_actionPerformed(ActionEvent e)
{
Frame2 f2 = new Frame2();
f2.show();
dispose();
}

public static void main(String[] args)
{
Frame1 f1 = new Frame1();
f1.show();
}
}

<======== Frame 2 ===========>

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Frame2 extends JFrame
{
private JButton jButton1 = new JButton();

public Frame2()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}

}

private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle(&quot;Frame 2&quot;);
jButton1.setText(&quot;To Frame1&quot;);
jButton1.setBounds(new Rectangle(130, 120, 110, 45));
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jButton1, null);
}

private void jButton1_actionPerformed(ActionEvent e)
{
Frame1 f1 = new Frame1();
f1.show();
dispose();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top