設定左右按鍵事件接收後, 每次按下左鍵會多出一次事件
也就是我原先預定左鍵按一次, 跳一次輸入框
當我要加入多組文字時, 後面按一次左鍵輸入文字, 之後還會持續跳出輸入框
可能是按鍵監聽事件寫壞
請各位高手看看應該怎麼改
謝謝
原始碼相關部分
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);
}
}
}