我正在尝试为该程序创建一个下拉菜单,该菜单显示一堆疾病,因此我想要它,因此当我单击JMenuBar中的一个项目时,它会通过调用paintComponent绘制一张图片,但是我的动作监听者首先不要工作,甚至不能查看是否单击了JMenuItem。我的错误在哪里?我已经尽力了
我曾尝试在声明jmenuitem对象成功之后立即放置动作侦听器,但尝试干预我的actionPerformed方法却没有成功,但解决了我的问题。
public void makeDDM()
{
ddmFRAME = new JFrame ("you.");
ddmPANEL = new JPanel();
ddmFRAME.setSize(300, 700);
ddmFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ddmFRAME.setLocation(0,0);
ddmFRAME.setResizable(true);
DDM dmeOne = new DDM();
ddmIMAGE dmeTwo = new ddmIMAGE();
ddmTITLE dmeThree = new ddmTITLE();
ddmPANEL.setLayout(new GridLayout(3,1));
ddmPANEL.add(dmeThree); //yellow
ddmPANEL.add(dmeOne); //blue
ddmPANEL.add(dmeTwo); //red
ddmFRAME.add(ddmPANEL);
ddmFRAME.setVisible(true);
}
class DDM extends JPanel implements ActionListener
{
public DDM()
{
setBackground(Color.ORANGE);
jmb = new JMenuBar();
ddmFRAME.setJMenuBar(jmb);
JMenu file = new JMenu("Examples Of Diseases / Conditions");
jmb.add(file);
file.setOpaque(true);
file.setBackground(Color.GREEN.darker());
JMenuItem itemOne = new JMenuItem("Anorexia");
file.add(itemOne);
itemOne.addActionListener(this);
JMenuItem itemTwo = new JMenuItem("Cancer");
file.add(itemTwo);
itemTwo.addActionListener(this);
JMenuItem itemThree = new JMenuItem("High Blood Cholesetrol");
file.add(itemThree);
itemThree.addActionListener(this);
JMenuItem itemFour = new JMenuItem("Heart Disease");
file.add(itemFour);
itemFour.addActionListener(this);
JMenuItem itemFive = new JMenuItem("Obesity");
file.add(itemFive);
itemFive.addActionListener(this);
JMenuItem itemSix = new JMenuItem("Stroke");
file.add(itemSix);
itemSix.addActionListener(this);
itemOne.setOpaque(true);
itemOne.setBackground(Color.GREEN.darker());
itemTwo.setOpaque(true);
itemTwo.setBackground(Color.GREEN.darker());
itemThree.setOpaque(true);
itemThree.setBackground(Color.GREEN.darker());
itemFour.setOpaque(true);
itemFour.setBackground(Color.GREEN.darker());
itemFive.setOpaque(true);
itemFive.setBackground(Color.GREEN.darker());
itemSix.setOpaque(true);
itemSix.setBackground(Color.GREEN.darker());
add(jmb, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == itemOne)
{
anorexia = true;
System.out.println(anorexia + " hi nigd");
dmeTwo.repaint();
}
else if(evt.getSource() == itemTwo)
{
cancer = true;
dmeTwo.repaint();
}
else if(evt.getSource() == itemThree)
{
heart1 = true;
dmeTwo.repaint();
}
else if(evt.getSource() == itemFour)
{
heart2 = true;
dmeTwo.repaint();
}
else if(evt.getSource() == itemFive)
{
obesity = true;
dmeTwo.repaint();
}
else if(evt.getSource() == itemSix)
{
stroke = true;
dmeTwo.repaint();
}
}
}
// i declared image file before this paintComponent method
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println(anorexia);
if(anorexia)
{
System.out.println("hi");
g.drawImage(ddmPIC1, 0, 0, this);
}
else if(cancer)
{
g.drawImage(ddmPIC2, 0, 0, this);
}
else if(heart1)
{
g.drawImage(ddmPIC3, 0, 0, this);
}
else if(heart2)
{
g.drawImage(ddmPIC4, 0, 0, this);
}
else if(obesity)
{
g.drawImage(ddmPIC5, 0, 0, this);
}
else if(stroke)
{
g.drawImage(ddmPIC6, 0, 0, this);
}
}
}
我希望当我单击JMenuItems之一时声明的布尔值变为true,但是经过测试,这似乎根本不起作用。任何帮助表示赞赏。谢谢
答案 0 :(得分:1)
您将在创建它们的方法中重新声明菜单项,使它们在该方法本地,并隐藏相同名称的实例变量。因此,在动作侦听器中,您正在将源与null值进行比较。