JCombobox 的空文本字段的错误消息
Error message for empty textfield for a JCombo box
我创建了一个 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);
我不确定为什么这不起作用。请使用我的代码提供答案。
此致,
应该是
如果 (textField.gettext().equals(""))
我创建了一个 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);
我不确定为什么这不起作用。请使用我的代码提供答案。 此致,
应该是 如果 (textField.gettext().equals(""))