繁体   English   中英

如何从JMenuItem打开JPanel?

[英]how do you open a JPanel from a JMenuItem?

我刚刚开始使用Swing进行编程,但这并不是一件容易的事...我从一个加密程序开始,但是遇到了麻烦。 当我单击JMenuItem时,它不会打开指定的JPanel。 这是我的代码。 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);
    }

}

}

在您的代码中,使用新的JFrame初始化创建菜单,例如在initializeIn方法中创建About Frame,但是您没有显示它,请尝试添加About.setVisible(true),它必须对您有所帮助。

如果要将其添加到现有框架中,请使用JPanel类而不是框架,然后将其添加到父容器中。

您会错过这两行。

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

然后你有

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM