帮忙看一下:JPopupMenu.add(action a)问题

skytears 2003-01-22 11:48:27
编译时不出错,运行时如下错误:
D:\temp>java ToolDemo
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JPopupMenu.createActionComponent(Unknown Source)
at javax.swing.JPopupMenu.add(Unknown Source)
at ToolDemo.createPopupMenu(ToolDemo.java:52)
at ToolDemo.<init>(ToolDemo.java:90)
at ToolDemo.main(ToolDemo.java:103)

附码:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ToolDemo extends JFrame{
protected JPopupMenu popupMenu;
protected JToolBar toolbar;

Action openAction;
Action saveAction;
Action closeAction;
Action exitAction;

protected void createActionObjects(){
ImageIcon icon;
icon = new ImageIcon("open.gif");
openAction = new AbstractAction("Open",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: Open");
}
};

icon = new ImageIcon("save.gif");
openAction = new AbstractAction("Save",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Save");
}
};

icon = new ImageIcon("close.gif");
closeAction = new AbstractAction("Close",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Close");
}
};

icon = new ImageIcon("exit.gif");
exitAction = new AbstractAction("Exit",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Exit");
System.exit(0);
}
};

}


protected void createPopupMenu(){
popupMenu = new JPopupMenu();
popupMenu.add(openAction);
popupMenu.add(saveAction);
popupMenu.add(closeAction);
popupMenu.addSeparator();
popupMenu.add(exitAction);
}

class PopupHandler extends MouseAdapter{
public void mousePressed(MouseEvent e){
if (e.isPopupTrigger()) popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e){
if (e.isPopupTrigger()) popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}


protected void createToolbar(){
toolbar = new JToolBar();
toolbar.add(openAction);
toolbar.add(saveAction);
toolbar.add(closeAction);
toolbar.addSeparator();
toolbar.add(exitAction);
}

public ToolDemo(){
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

createActionObjects();
createPopupMenu();
createToolbar();

addMouseListener(new PopupHandler());

Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(toolbar,BorderLayout.NORTH);
toolbar.setFloatable(true);
content.add(new JLabel("Click inside the window"));
}

public static void main(String args[]){
ToolDemo app = new ToolDemo();
app.setTitle("ToolDemo");
app.setSize(320,240);
app.show();
}
}

...全文
35 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qxjavajava 2003-01-22
  • 打赏
  • 举报
回复
protected void createActionObjects(){
ImageIcon icon;
icon = new ImageIcon("open.gif");
openAction = new AbstractAction("Open",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: Open");
}
};

icon = new ImageIcon("save.gif");
openAction = new AbstractAction("Save",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Save");
}
};

icon = new ImageIcon("close.gif");
closeAction = new AbstractAction("Close",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Close");
}
};

icon = new ImageIcon("exit.gif");
exitAction = new AbstractAction("Exit",icon){
public void actionPerformed(ActionEvent e){
//JOptionPane.showMessageDialog(this,"Command: "+"Exit");
System.exit(0);
}
};

}
------------------
n你的这个地方有问题!
你定义了两次openAction 而没有定义saveAction
手工写的旅店管理系统界面。 import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JTextField; public class MyButton extends JButton implements MouseListener, ActionListener { private static final long serialVersionUID = 6261021704484837560L; private JPopupMenu popMenu; private ButtonGroup bg; private JRadioButton SetDark;// 置为脏房,待打扫 private JRadioButton SetClean;// 置为应该打扫的房间 private JRadioButton SetUsing;// 置为正在使用的房间 private JRadioButton SetHouseNum;// 设置房号 private JRadioButton SetEmpty;//置为空房 private JRadioButton Delete;//删除 private JRadioButton Bujiaoyajin;//补交押金 private String HouseAr, HouseNu; private Color Housecolor; private int ID; private double SingelSal; public double getSingelSal() { return SingelSal; } public void setSingelSal(double singelSal) { SingelSal = singelSal; } private HashMap tempMap; private Customer customer; public MyButton(int ID,HashMap tempMap) { super.setIcon(new ImageIcon("Blue Victorian.JPG")); this.ID = ID; this.tempMap = tempMap; init(); } public MyButton() { super.setIcon(new ImageIcon("Blue Victorian.JPG")); init(); } public void init() { Housecolor = Color.GREEN; super.setBackground(Housecolor); popMenu = new JPopupMenu(); bg = new ButtonGroup(); SetDark = new JRadioButton("置为脏房"); SetClean = new JRadioButton("置为闲置房"); SetUsing = new JRadioButton("添加留客"); SetHouseNum = new JRadioButton("初始房态"); SetEmpty = new JRadioButton("置为空房"); Delete = new JRadioButton("删除本房"); Bujiaoyajin = new JRadioButton("补交押金"); // 添加action监听 SetDark.addActionListener(this); SetClean.addActionListener(this); SetUsing.addActionListener(this); SetHouseNum.addActionListener(this); SetEmpty.addActionListener(this); Delete.addActionListener(this); Bujiaoyajin.addActionListener(this); // 加入buttongroup,实现单选 bg.add(SetClean); bg.add(SetDark); bg.add(SetUsing); bg.add(SetHouseNum); bg.add(SetEmpty); bg.add(Delete); bg.add(Bujiaoyajin); //bg.add(Delete); // 设置相应房态颜色 popMenu.add(SetDark); popMenu.add(SetClean); popMenu.add(SetUsing); popMenu.add(SetHouseNum); popMenu.add(SetEmpty); popMenu.add(Bujiaoyajin); //popMenu.add(Delete); // 设置jbutton的固定大小 setMaximumSize(new Dimension(80, 60)); setMinimumSize(new Dimension(80, 60)); setPreferredSize(new Dimension(80, 60)); this.addMouseListener(this); } public void mouseClicked(MouseEvent arg0) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} // 为jbuuton添加popmenu public void mousePressed(MouseEvent arg0) { if (arg0.getButton() == MouseEvent.BUTTON3) { popMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY()); } } //定位该button在map中的位置 public MyButton indexof() { MyButton b = null; Set set = this.tempMap.keySet(); Iterator iter = set.iterator(); while(iter.hasNext()) { String id = iter.next(); if(this.ID == Integer.parseInt(id.trim())) b = this.tempMap.get(id); } return b; } //getcustomer的信息 public Customer getCustomer() { return this.customer; } // 为选项添加监听 public void actionPerformed(ActionEvent e) { if(e.getSource() == Bujiaoyajin) { System.out.println("fgfhjhk"); } if (e.getSource() == SetDark) { Housecolor = Color.yellow; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetClean) { Housecolor = Color.gray; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == Delete) { tempMap.remove(ID); Write(); } if (e.getSource() == SetEmpty) { Housecolor = Color.green; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetUsing) { new GetCustomerInfo(); this.setBackground(Housecolor); } if (e.getSource() == SetHouseNum) { new GetHouseInfo(); } } public int getID() { return ID; } public void setID(int id) { ID = id; } //保存到本地 public void Write() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/ButtonsInfo.shf")); oos.writeObject(this.tempMap); oos.flush(); oos.close(); } catch (Exception e) { e.printStackTrace(); } } //为外层程序提供 public void setHouseAr(String str) { this.HouseAr = str; } public String getHouseAr() { return this.HouseAr; } public void setHouseNu(String str) { this.HouseNu = str; } public String getHouseNu() { return this.HouseNu; } public void setHouseColor(Color cl) { this.Housecolor = cl; } public Color getHouseColor() { return this.Housecolor; } // 设置房号的窗口 class GetHouseInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4; private JLabel HouseArea, HouseNum,SingleSal; private JTextField HouseA, HouseN,SinglS; private JButton Entrue, Cansal; private String A,N; public GetHouseInfo() { super.setTitle("初始房态"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); HouseArea = new JLabel("设定房区:"); HouseNum = new JLabel("设定房号:"); SingleSal = new JLabel("设定单价:"); HouseA = new JTextField(10); HouseN = new JTextField(10); SinglS = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); Entrue.addActionListener(this); Cansal.addActionListener(this); p.setLayout(new GridLayout(4, 1)); p1.add(HouseArea); p1.add(HouseA); p2.add(HouseNum); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SingleSal); p4.add(SinglS); p.add(p1); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setAlwaysOnTop(true); this.setVisible(true); this.pack(); } public String getHouseA() { return this.A; } public String getHouseN() { return this.N; } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == Entrue) { A = HouseAr = HouseA.getText(); N = HouseNu = HouseN.getText(); SingelSal = new Double(SinglS.getText().trim()).doubleValue(); MyButton.this.setText(HouseAr + "\n" + HouseNu); indexof().setHouseNu(A+N); Write(); this.hide(); } if (arg0.getSource() == Cansal) { this.hide(); } } } //留客信息录入 class GetCustomerInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4,p5,p6; private JLabel Name, SumCash,SumDays,Sex,TelePhone; private JTextField HouseA, HouseN,SumD,telePhone; private JButton Entrue, Cansal; private JLabel sex; private ButtonGroup bg; private JRadioButton man,waman; private String sexStr; public GetCustomerInfo() { super.setTitle("录入顾客信息"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); p6 = new JPanel(); Name = new JLabel("旅客姓名:"); sex = new JLabel("性别: "); TelePhone = new JLabel("联系方式:"); SumCash = new JLabel("旅客押金:"); SumDays = new JLabel("留住天数:"); HouseA = new JTextField(10); telePhone = new JTextField(10); HouseN = new JTextField(10); SumD = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); bg = new ButtonGroup(); man = new JRadioButton("男"); waman = new JRadioButton("女"); bg.add(man); bg.add(waman); p6.add(sex); p6.add(man); p6.add(waman); Entrue.addActionListener(this); Cansal.addActionListener(this); man.addActionListener(this); waman.addActionListener(this); p.setLayout(new GridLayout(6, 1)); p1.add(Name); p1.add(HouseA); p2.add(SumCash); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SumDays); p4.add(SumD); p5.add(TelePhone); p5.add(telePhone); p.add(p1); p.add(p5); p.add(p6); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setVisible(true); this.pack(); } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if(arg0.getSource() == man) { sexStr = "男"; } if(arg0.getSource() == waman) { sexStr = "女"; } if (arg0.getSource() == Entrue) { customer = new Customer(HouseA.getText(),sexStr,telePhone.getText(), new Integer(SumD.getText().trim()).intValue(), new Integer(HouseN.getText().trim()).intValue()); Housecolor = Color.blue; MyButton.this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); if(new Double(HouseN.getText().trim()).doubleValue() < SingelSal) { JOptionPane.showMessageDialog(null, "押金不足!"); } else { Write(); this.hide(); } } if (arg0.getSource() == Cansal) { this.hide(); } } } public static void main(String[] s) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); MyButton mb = new MyButton(); panel.add(mb); frame.add(panel); frame.setSize(600, 600); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧