我是一個java的新手,並從書中學習過去兩個月。我已經嘗試了本書中的JApplet菜單程序。JApplet菜單不可見
MenuDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MenuDemo implements ActionListener {
JLabel jlab;
MenuDemo() {
JFrame jfrm = new JFrame("Menu Demo");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 200);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlab = new JLabel();
JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiClose = new JMenuItem("Close");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
JMenu jmOptions = new JMenu("Options");
JMenu jmColors = new JMenu("Colors");
JMenuItem jmiRed = new JMenuItem("Red");
JMenuItem jmiGreen = new JMenuItem("Green");
JMenuItem jmiBlue = new JMenuItem("Blue");
jmColors.add(jmiRed);
jmColors.add(jmiGreen);
jmColors.add(jmiBlue);
jmOptions.add(jmColors);
JMenu jmPriority = new JMenu("Priority");
JMenuItem jmiHigh = new JMenuItem("High");
JMenuItem jmiLow = new JMenuItem("Low");
jmPriority.add(jmiHigh);
jmPriority.add(jmiLow);
jmOptions.add(jmPriority);
JMenuItem jmiReset = new JMenuItem("Reset");
jmOptions.addSeparator();
jmOptions.add(jmiReset);
jmb.add(jmOptions);
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiRed.addActionListener(this);
jmiGreen.addActionListener(this);
jmiBlue.addActionListener(this);
jmiHigh.addActionListener(this);
jmiLow.addActionListener(this);
jmiReset.addActionListener(this);
jmiAbout.addActionListener(this);
jfrm.add(jlab);
jfrm.setJMenuBar(jmb);
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String comStr = ae.getActionCommand();
if (comStr.equals("Exit"))
System.exit(0);
jlab.setText(comStr + " Selected");
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MenuDemo();
}
});
}
}
但它給下面的例外。
load: MenuDemo is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a
member of class MenuDemo with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)
因此,我改變前三行從類如下:
public class MenuDemo extends JApplet implements ActionListener {
JLabel jlab;
public void MenuDemo() {
現在的小程序窗口是可見的,但沒有菜單。作爲新手,我該如何解決它。
謝謝。
縮進你的代碼更好的生活。 – Maroun
您需要至少有一個'public'類,其中'main'將被jvm調用。在你的情況下,將'public'修飾符添加到'class MenuDemo' – Abubakkar
看起來你似乎已經解決了你自己原來的問題,因此你應該改變你的問題的標題,爲什麼菜單不可見 – Keiwan