我无法实现JMenuItem。我想要我的JMenuItem,“毕达哥拉斯”在这种情况下制作一棵树,这意味着如果用户点击毕达哥拉斯,我想让它显示一棵树。我有一个名为PythagorasTree的课,它吸引了树。我无法将PythagorasTree类与JMenuItem连接起来。我是GUI的新手。任何帮助将不胜感激。
import java.awt.event.*;
import javax.swing.*;
public class MenuExp extends PythagorasTree{
public static void main(String[] args) {
JFrame frame = new JFrame("Computer Graphics HW");
frame.setVisible(true);
frame.setSize(400, 400);
frame.pack();
// Creating MenuBar
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
// Creating Menu
JMenu file = new JMenu("File");
menubar.add(file);
//creating item inside File
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenuItem pythagoras = new JMenuItem("Pythagoras");
file.add(pythagoras);
class exitaction implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class pythagorasAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
/*
* Do something here
*
*/
}
}
exit.addActionListener(new exitaction());
}
}