Java:当从 JMenuItem 多次调用方法时,JTextField 会创建无限循环

标签 java swing jtextfield

我正在为大学编写一个文本分析程序,需要您将单词添加到字典中以及执行其他操作。我对这部分代码有疑问,因为如果用户在 JTextField 为空时点击“确定”,我需要显示一条错误消息。我可以让它继续运行,但以某种方式创建了一个带有错误消息的无限循环。请帮忙,我查遍了互联网,但找不到这么简单的错误的答案。这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.Character;

public class TAPtest implements ActionListener
{
 private JFrame tap = new JFrame("Text Analysis"); 
 private JMenuBar tapMenu = new JMenuBar();
 private JMenu dictionary = new JMenu("Dictionary");
 private JMenuItem addWord = new JMenuItem("Add Word");
 private JFrame frameAdd = new JFrame("Add Word");
 private JLabel add = new JLabel("Enter word to add:");
 private JButton okNewWord = new JButton("Ok");
 private JButton cancelNewWord = new JButton("Cancel");
 private JTextField inputNewWord = new JTextField(28);
 private JPanel westAdd = new JPanel();
 private JPanel eastAdd = new JPanel();
 private JPanel southAdd = new JPanel();
 private Vector <String> dictionaryVector = new Vector<String>();

 public TAPtest() //Constructor. Contains TAP GUI.
 {  
   tap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   tap.setSize(350, 100);
   tap.setLocation(700, 500);
   tap.setResizable(false); 
   tap.setJMenuBar(tapMenu);
   tapMenu.add(dictionary);
   dictionary.add(addWord);

   addWord.addActionListener(this);

   tap.setVisible(true);
 }

 public void actionPerformed(ActionEvent event) //All actions performed.
 {
   if (event.getActionCommand() == "Add Word") addNewDicWordGUI();

   if (event.getSource() == okNewWord) addNewDicWord();
   if (event.getSource() == cancelNewWord) frameAdd.dispose();
 }

 public void addNewDicWordGUI()
 {
   frameAdd.setSize(350, 150);
   frameAdd.setLocation(700, 500);
   frameAdd.setResizable(false); 

   okNewWord.setSize(5,20);
   cancelNewWord.setSize(5,20);

   westAdd.add(add);
   eastAdd.setLayout(new GridLayout(2,1));
   eastAdd.add(okNewWord);
   eastAdd.add(cancelNewWord);
   southAdd.add(inputNewWord);

   frameAdd.add(westAdd, BorderLayout.WEST);
   frameAdd.add(eastAdd, BorderLayout.EAST);
   frameAdd.add(southAdd, BorderLayout.SOUTH);

   okNewWord.addActionListener(this);
   cancelNewWord.addActionListener(this);
   inputNewWord.addActionListener(this);

   frameAdd.setVisible(true);
 }

 public void addNewDicWord() 
 {
   String inputNew = inputNewWord.getText(); //Get JTextField value.
   inputNew = inputNew.toLowerCase();
   frameAdd.dispose();
   if (inputNew.equals("")) 
    JOptionPane.showMessageDialog(null, "You must enter a word", "Notice", 1);
   else {
    boolean addItemFound = dictionaryVector.contains(inputNew); //True if contains.
   if(addItemFound) //If true.
JOptionPane.showMessageDialog(null, inputNew + " already exists in the dictionary.", "Word Already Exists", 1);
   else { //If not true.
    dictionaryVector.addElement(inputNew); //Add the word to the dictionary Vector.
Collections.sort(dictionaryVector); //Sort the vector in ascending order.
JOptionPane.showMessageDialog(null, inputNew + " was added to the dictionary.", "Word Added", 1);
   }
   inputNewWord.setText(null);
 }
}

public static void main(String [] args)
{
  TAPtest program = new TAPtest();
}
}

最佳答案

private Vector <String> dictionaryVector = new Vector<String>();
inputNewWord.setText(null);
JOptionPane.showMessageDialog(null, "You must enter a word", "Notice", 1);
public void actionPerformed(ActionEvent event) //All actions performed.
{
    ...
}

应该更好

private SortedSet<String> dictionaryVector = new TreeSet<String>();
inputNewWord.setText("");
JOptionPane.showMessageDialog(null, "You must enter a word", "Notice",
         JOptionPane.ERROR_MESSAGE);
public void actionPerformed(ActionEvent event) //All actions performed.
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() {
            ...
        }
    }
}

最后一个invokeLater可能导致了这些麻烦。事件处理 actionPerformed 在事件线程上完成,并且显示消息应该在之后完成 该事件已得到处理(与 GUI 保持响应的任何较长任务一样)。

关于Java:当从 JMenuItem 多次调用方法时,JTextField 会创建无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13181283/

相关文章:

java - 如何放置.getScaledInstance

java - 如何将字符串从 JTextField 转换为 double 组

java - 如何限制hibernate criteria API获得的结果?

java - JTextArea缩进

Java:计时器或 ScheduleExecutorService 用于在繁忙的窗口中计算点击次数?

java - 使用 NetBeans IDE 将图像添加到 JPanel

java - 为什么我会收到 NullPointerException

java - 启用光标选择后不可编辑的 JTextField

java - Java-Scanner .nextLine()读取未分配的字符串

java - JDatePicker Java中的日期差异