[虫?] 鼠标左右按键事件

楼主: WingedDragon (黄金会死鸟-死后无法复活)   2016-08-02 13:08:25
设定左右按键事件接收后, 每次按下左键会多出一次事件
也就是我原先预定左键按一次, 跳一次输入框
当我要加入多组文字时, 后面按一次左键输入文字, 之后还会持续跳出输入框
可能是按键监听事件写坏
请各位高手看看应该怎么改
谢谢
原始码相关部分
private List<JButton> item = new ArrayList<JButton>();
private Object clickButton = null;
public void paintTable() {
frameTable.setLayout(new GridLayout(3,0));
buttonPop popup = new buttonPop();
for (int i = 0; i < item.size(); i++) {
frameTable.add(item.get(i));
item.get(i).setVisible(true);
item.get(i).addMouseListener(popup);
}
}
private class buttonPop extends MouseAdapter {
public void mousePressed(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {
popupMenu = new JPopupMenu();
LinkedList<JMenuItem> popupItem = new LinkedList<JMenuItem>();
clickButton = me.getSource();
PopupSelect select = new PopupSelect();
for (int i = 0; i < item.size(); i++) {
JMenuItem tmpItem = new JMenuItem(item.get(i).getText());
tmpItem.addActionListener(select);
popupItem.addLast(tmpItem);
}
while (!popupItem.isEmpty()) {
popupMenu.add(popupItem.removeFirst());
if (!popupItem.isEmpty())
popupMenu.addSeparator();
}
popupMenu.show(me.getComponent(), me.getX(), me.getY());
}
else if (SwingUtilities.isLeftMouseButton(me)) {
String newLeaveItem = JOptionPane
.showInputDialog(mainFrame, "Enter a new item, ex :",
"输入其他字");
if (newLeaveItem == null) {
return;
}
for (int i = 0; i < item.size(); i++) {
if (item.get(i).getText().equals(newLeaveItem)) {
JOptionPane.showMessageDialog(null,
"Duplicated items", "Warning",
JOptionPane.WARNING_MESSAGE);
return;
}
}
item.add(new JButton(newLeaveItem));
paintTable();
mainFrame.revalidate();
}
}
}
private class PopupSelect implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String selectItem = ae.getActionCommand();
if (item.contains((JButton) clickButton)) {
((JButton) clickButton).setText(selectItem);
}
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com