KururuMan
KururuMan

Reputation: 13

how do you open a JPanel from a JMenuItem?

I have just started programming with Swing, and it isn't too easy... I started out with an encryption program, and I have hit a wall. When I click my JMenuItem, it does not open the JPanel specified. Here is my code. There were no errors that were picked up by Eclipse.

/**
 * Copyright 2013
 * 
 * You may edit this code and redistribute it to friends,
 * but you MAY NOT say that this is yours, or sell it for
 * money.
 */

package lakecoding.RAMBIT7;

import java.awt.EventQueue;
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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class RAMBIT7 implements ActionListener {

private JFrame frame;
private JFrame About;
private JFrame License;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RAMBIT7 window = new RAMBIT7();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public RAMBIT7() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setSize(200, 300); //1024x768, 800x600
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("RAMBIT7 Encryption Software 1.0.0");
    frame.setResizable(false);
    frame.getContentPane().setLayout(new FlowLayout(0, 13, 0)); //GridLayout, FlowLayout, GridBagLayout, BorderLayout

    /**
     * 'Encrypt' and 'Decrypt' buttons
     */

    JButton encrypt = new JButton("Encrypt");
    JButton decrypt = new JButton("Decrypt");
    encrypt.addActionListener(this);
    decrypt.addActionListener(this);
    frame.getContentPane().add(encrypt);
    frame.getContentPane().add(decrypt);

    /**
     * JMenuBar
     */

    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu help = new JMenu("Help");
    JMenuItem about = new JMenuItem("About");
    JMenuItem license = new JMenuItem("License");
    JMenuItem close = new JMenuItem("Exit");
    file.add(close);
    help.add(about);
    help.add(license);
    bar.add(file);
    bar.add(help);
    about.addActionListener(this);
    license.addActionListener(this);
    close.addActionListener(this);
    frame.setJMenuBar(bar);

}

public void intializeAbout() {
    About = new JFrame();
    About.setSize(200, 200);
    About.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    About.setTitle("About");
    About.setResizable(false);
    About.getContentPane().setLayout(new FlowLayout());
}

public void intializeLicense() {
    License = new JFrame();
    License.setSize(200, 200);
    License.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    License.setTitle("License");
    License.setResizable(false);
    License.getContentPane().setLayout(new FlowLayout());
}

@Override
public void actionPerformed(ActionEvent e) {
    String a = e.getActionCommand();
    if(a.equalsIgnoreCase("encrypt")) {
        System.out.println("Begin RAMBIT7 encryption.");
        //encryptRAMBIT7(input);
    } else if(a.equalsIgnoreCase("decrypt")) {
        System.out.println("Begin RAMBIT7 decryption.");
        //decryptRAMBIT7(input);
    } else if(a.equalsIgnoreCase("about")) {
        intializeAbout();
    } else if(a.equalsIgnoreCase("license")) {
        intializeLicense();
    } else if(a.equalsIgnoreCase("exit")) {
        System.out.println("Terminating...");
        System.exit(0);
    }

}

}

Upvotes: 0

Views: 1254

Answers (2)

subash
subash

Reputation: 3140

you miss these two lines..

About.setVisible(true);
License.setVisible(true);

then you got

Upvotes: 0

alex2410
alex2410

Reputation: 10994

In your code you create menu with new JFrame initialization, for example in method initializeAbout you create About Frame , but you don' t show it, try to add About.setVisible(true) it must help you.

If you want to add this to existing frame use JPanel class instead of frames, and add it to your parent container.

Upvotes: 1

Related Questions