我正在为一个学校项目制作一个图书馆数据库,但我的菜单有点问题。所以主要问题是在我写的Action Listener方法中
(e.getSource()==m1Frame1)
我的程序没有检测到菜单项并给我一个错误。我已经在网上查看了多个教程等,但似乎找不到任何方法来修复它并使其能够在单击特定项目时发生特定操作。任何有关此问题的帮助/解决方案将不胜感激。
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.Icon;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Color;
public class m1 extends JFrame {
JPanel pane = new JPanel();
JFrame a = new JFrame("Main Frame");
JFrame b = new JFrame("Sub Frame");
JButton checkOutButton = new JButton("check");
JButton returnButton = new JButton("return");
JMenu mb2 = new JMenu("Books");
// mb2.setForeground(Color.white);
JMenu open = new JMenu("Students");
// open.setForeground(Color.white);
public m1() {
JMenuBar mb;
mb = new JMenuBar() {
public void paintComponent(Graphics g) {
g.drawImage(Toolkit.getDefaultToolkit().getImage("G:"), 0, 0, this);
}
};
setSize(400, 400);
setBackground(Color.BLACK);
setTitle("Screen 2");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mb.add(open);
JMenuItem m1Frame1 = new JMenuItem("Create");
JMenuItem m1Frame2 = new JMenuItem("Delete");
JMenu m1Frame3 = new JMenu("Look-Up");
JMenuItem m1Frame4 = new JMenuItem("Check Fine");
JMenuItem m1Frame5 = new JMenuItem("Check Borrowed Books");
JMenuItem subM1 = new JMenuItem("Name");
JMenuItem subM2 = new JMenuItem("Student #");
open.add(m1Frame1);
open.add(m1Frame2);
open.add(m1Frame3);
open.add(m1Frame4);
open.add(m1Frame5);
m1Frame3.add(subM1);
m1Frame3.add(subM2);
mb.add(mb2);
JMenuItem m2Frame1 = new JMenuItem("Create");
JMenuItem m2Frame2 = new JMenuItem("Delete");
JMenu m2Frame3 = new JMenu("Look-Up");
JMenuItem subB1 = new JMenuItem("Title");
JMenuItem subB2 = new JMenuItem("Author");
JMenuItem subB3 = new JMenuItem("Category");
JMenuItem subB4 = new JMenuItem("ISBN");
JMenuItem m2Frame4 = new JMenuItem("Compare Star Rating");
JMenuItem m2Frame5 = new JMenuItem("Check If Checked Out");
JMenuItem m2Frame6 = new JMenuItem("Lost Book");
mb2.add(m2Frame1);
mb2.add(m2Frame2);
mb2.add(m2Frame3);
mb2.add(m2Frame4);
mb2.add(m2Frame5);
mb2.add(m2Frame6);
m2Frame3.add(subB1);
m2Frame3.add(subB2);
m2Frame3.add(subB3);
m2Frame3.add(subB4);
a.setJMenuBar(mb);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(1280, 720);
a.setVisible(true);
b.setSize(600, 400);
m handler = new m();
pane.add(checkOutButton);
pane.add(returnButton);
add(pane);
checkOutButton.setVisible(false);
returnButton.setVisible(false);
checkOutButton.setBounds(60, 440, 220, 30);
returnButton.setBounds(60, 404, 100, 50);
checkOutButton.addActionListener(handler);
returnButton.addActionListener(handler);
}
public class m implements ActionListener, ItemListener {
public void actionPerformed(ActionEvent e) {
(e.getSource() == m1Frame1) {
a.setVisible(false);
setVisible(true);
checkOutButton.setVisible(true);
returnButton.setVisible(true);
}
}
public void itemStateChanged(ItemEvent e) {
}
}
public static void main(String[] args) {
m1 aa = new m1();
}
}