2013-11-09 2 views
0

Я только что начал программировать с Swing, и это не так просто ... Я начал с программы шифрования, и я ударил стену. Когда я нажимаю мой JMenuItem, он не открывает указанный JPanel. Вот мой код. Не было ошибок, которые были подобраны Eclipse.Как вы открываете JPanel из JMenuItem?

/** 
* 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); 
    } 

} 

} 
+0

Является ли это для школы? – Tdorno

+0

Что нужно делать _supposed_? Я вижу несколько JMenuItems, которые создаются и добавляются в панель. В JMenuItems не добавлен прослушиватель или 'Action'. Вы хотите, чтобы они отображали какой-то фрейм? – initramfs

+0

Узнайте и следуйте стандартным соглашениям об именах Java. Любой пример, который вы найдете в книге или учебнике, будет использовать эти рекомендации, поэтому не составляйте свои собственные. Имена переменных не должны начинаться с символа верхнего регистра. Также для окна «О программе» вы должны использовать «модальный JDialog», а не JFrame. – camickr

ответ

1

В своем коде вы создаете меню с новой инициализации JFrame, например, в методе initializeAbout вы создаете О Рама, но вы не»т показать, попробуйте добавить About.setVisible (правда) он должен вам помочь.

Если вы хотите добавить это в существующий фрейм, используйте класс JPanel вместо фреймов и добавьте его в родительский контейнер.

0

вы пропустите эти две строки ..

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

тогда вы получили

Смежные вопросы