Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Closing of JFrame once button called

RibbletonRibbleton Posts: 152Member
When i press a button i want to close a screen down and open the new screen. I can open the new screen but can't close a screen.

most of the code is just setting the screen up. I have commented the problem area.

Code:

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

public class Menu extends JFrame {

private JButton BExit, BQuestions, BTutorial;
private JPanel PTop, PExit, PCenter;
private Color color = Color.blue;
private JLabel label1;

public Menu()
{
super( "Menu Screen" );

Container Canvas = getContentPane();

PTop = new JPanel();
PExit = new JPanel();
PCenter = new JPanel();

BExit = new JButton(" Exit Application ");
BExit.setToolTipText( " Press To Exit Application ");
Canvas.add(BExit);

BQuestions = new JButton(" Add Questions ");
BQuestions.setToolTipText( " Add a new set of questions to the system ");
Canvas.add(BQuestions);

BTutorial = new JButton(" Take Tutorial ");
BTutorial.setToolTipText( " Take Java Tutorial");
Canvas.add(BTutorial);

Icon IPic = new ImageIcon( "images/Test.gif" );
label1 = new JLabel ( IPic );

PTop.add ( BQuestions );
PTop.add ( BTutorial );
PExit.add ( BExit );
PCenter.add( label1 );

Canvas.add( PTop, BorderLayout.NORTH );
Canvas.add( PCenter, BorderLayout.CENTER );
Canvas.add( PExit, BorderLayout.SOUTH );

PTop.setBackground ( color );
PExit.setBackground ( color );
PCenter.setBackground ( color );
Canvas.setBackground ( color );

ButtonHandler handler = new ButtonHandler();
BTutorial.addActionListener(handler);
BQuestions.addActionListener(handler);
BExit.addActionListener(handler);

setSize (300, 225);
show();

}

public static void main ( String args[] ) {

Menu app = new Menu();

app.addWindowListener (
new WindowAdapter() {
public void windowClosing (WindowEvent e)
{
System.exit( 0 );
}
}
);

}
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
if( e.getSource() == BTutorial) {

/* PROBLEM AREA
If a user presses the tutorial button i want the tutroial screen to open and the menu screen the close. I've tired this.setVisible(false); but it doesn't work.

*/

Tutorial Tut = new Tutorial();
}
if( e.getSource() == BQuestions) {
Questions Ques = new Questions();
}
if( e.getSource() == BExit) {
System.exit( 0 );
}
}
}
}

Thanks for your time
*Ribbleton_
Sign In or Register to comment.