0
私はこのコードをテキストフィールドでenterを押すのではなく、追加ボタンとして機能させようとしています。 ボタンとして機能するようにaddメソッドを変更しようとしています
ActionListener cmdLis = new CmdTextListener();
cmdTextField.addActionListener(cmdLis);
public void actionPerformed(ActionEvent evt)
{
String cmdStr = cmdTextField.getText();
Scanner sc = new Scanner(cmdStr);
String cmd = sc.next();
if (cmd.equals("add"))
{
int value = sc.nextInt();
binTree.add(value);
if(view != null)
remove(view);
view = binTree.getView();
add(view);
pack();
validate();
cmdResultTextField.setText(" ");
}
は、だから私はこのようにそれを実行しようとしましたが、その私はそのも
if (e.getSource() == addButton)
{
//int value = Integer.parseInt(cmd);
int value = Integer.parseInt(cmdStr);
binTree.add(value);
if(view != null)
remove(view);
view = binTree.getView();
add(view);
pack();
validate();
cmdResultTextField.setText("Added "+ value);
}
あなたは 'addButton.addActionListener(cmdLis);を呼び出しましたか? – guleryuz
質問とコードを明確にしてください。私は他の人に話すことはできませんが、あなたがしようとしていることやそのコードが何をしているのかははっきりしていません。より詳細で明確な説明とより良いコード、好ましくは[mcve]または[sscce](http://sscce.org)を投稿することを検討してください(リンクをお読みください)。 –
ありがとうGuleryuz god私はアクションリスナーを追加するために少なくとも10の方法を試してみた – user6860301