JCombo框的空文本字段的错误消息

时间:2016-04-08 20:42:37

标签: java eclipse jcombobox

我创建了一个JComboBox,以便在我创建游戏时将玩家添加到列表中。我试图使用JOptionPane显示一条错误消息,如果文本字段留空我使用下面的方法做了这个。

    btnAddPlayer = new JButton("Add Player");
    btnAddPlayer.addActionListener(new ActionListener() { //This is the layout for the list of points that are possible to achieve
        public void actionPerformed(ActionEvent arg0) {     
            if (txtAddPlayer.equals("")){
                        JOptionPane.showMessageDialog(btnAddPlayer, this, "Please Enter Full Details", NumofAnswers);//THIS IS THE METHOD I TRIED 
                } else {
                    comboBox.addItem(txtAddPlayer.getText());
                }
        }
    });
    btnAddPlayer.setBounds(469, 243, 89, 23);
    panel.add(btnAddPlayer);
    txtAddPlayer = new JTextField();

    txtAddPlayer.setBounds(373, 244, 86, 20);
    panel.add(txtAddPlayer);
    txtAddPlayer.setColumns(10);

我不确定为什么这不起作用。请使用我的代码提供答案。 的问候,

1 个答案:

答案 0 :(得分:0)

应该是     如果(textField.gettext()。equals(“”))