3

I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window. I know I can do it with setVisible(false);. But I can't make it work yet.

This is one example of code I have:

    _startBtn.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("_startBtn pressed");
            // Code to hide this JFrame and initialize another
        }

My question is, how can I do it using a anonymous class like this one?

Thanks in advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ángel Araya
  • 225
  • 1
  • 8
  • 1
    *"I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Redesign the GUI rather than make such a mess (multiple frames) work reliably - it will be quicker. – Andrew Thompson Aug 12 '12 at 04:56
  • 1
    *"..I can't make it work yet."* We could guess why, but you could save us the trouble and just tell us. – Andrew Thompson Aug 12 '12 at 05:04
  • 1
    Though the advice given by @AndrewThompson, is really valid in this context. But if you still wanted to stick to your **not so good approach**, here is one [example](http://stackoverflow.com/a/9443609/1057230) for the same. – nIcE cOw Aug 12 '12 at 05:59
  • 1
    Given your advice I'm changing the design to a single JFrame with a CardLayout management. Which I hope will fulfill my needs. Now I have some other questions (since I never used it before) but I don't think I should post that here. – Ángel Araya Aug 12 '12 at 06:35
  • Good choice (single frame)! Also a good choice to ask it on a separate question. I'll keep an eye out for the follow up question, but don't be surprised if someone jumps in and provides an excellent answer before I ever see it. :) – Andrew Thompson Aug 12 '12 at 21:06
  • Still you are facing any problem... or I mistaken to understand your question.. – Bharat Sharma Aug 13 '12 at 08:15

1 Answers1

2

I am posting an example for you i hope it will help you.

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

import javax.swing.JButton;
import javax.swing.JFrame;


public class windows_test {
    JFrame login = null;
    JFrame inner_frame = null;

    public windows_test() {
        login = new JFrame();
        login.setBounds(10, 10, 300, 300);
        login.setLayout(new BorderLayout());

        JButton button = new JButton("Login");
        login.add(button, BorderLayout.CENTER);

        login.setVisible(true);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (inner_frame == null) {
                    inner_frame = new JFrame();
                }
                inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
                inner_frame.add(new JButton("inner frame"));
                inner_frame.setVisible(true);
                login.setVisible(false);
                inner_frame.setBounds(10, 10, 300, 300);
            }
        });
    }
}

I will recommend you to use jpanel instead of jframes but you have asked for frames so i have created it with them. Hope it will help you ask if i am wrong somewhere or you are not able to understand.

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29