Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从JMenuItem打开JPanel?_Java_Swing - Fatal编程技术网

Java 如何从JMenuItem打开JPanel?

Java 如何从JMenuItem打开JPanel?,java,swing,Java,Swing,我刚开始用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. */

我刚开始用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初始化创建菜单,例如在方法initializeAbout中创建About Frame,但不显示它,请尝试添加About.setVisible(true)它必须对您有所帮助


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

您会错过这两行

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

然后你得到了它应该做什么?我看到一些JMenuItems正在创建并添加到窗格中。未将侦听器或
操作添加到JMenuItems。你想让他们弹出一些框架吗?学习并遵循标准Java命名约定。你在一本书或教程中找到的任何例子都将使用指南,所以不要自己编。变量名不应以大写字符开头。此外,对于“关于”窗口,您应该使用
模态JDialog
,而不是JFrame。