0

我刚开始用 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);
    }

}

}
4

2 回答 2

1

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

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

于 2013-11-09T03:46:48.207 回答
0

你错过了这两行..

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

那么你得到了

于 2013-11-09T04:28:29.553 回答