文本字段不响应组合框选择

时间:2022-05-04 11:44:34

Am kind of a beginner. I have an application in which I have a combo box. The combo box has two strings/items 1 being "Add" and 2nd being "Minus". If I select "Add" from the combo box my jText field should display the word "Addition" and if I select "Minus" from the combo box the jText field should display the word "Subtraction". My issue is it only displays "Addition" if I select add, but if I selects "Minus" it doesn't display "Subtraction".

我是一个初学者。我有一个应用程序,我有一个组合框。组合框有两个字符串/项1为“Add”,第二个为“Minus”。如果我从组合框中选择“添加”,我的jText字段应显示单词“Addition”,如果我从组合框中选择“Minus”,则jText字段应显示单词“Subtraction”。我的问题是,如果我选择添加,它只显示“添加”,但如果我选择“减号”则不显示“减法”。

Is there something wrong with my if statement??

我的if语句有问题吗?

    String display ="";

   if (comboBxOperator.getSelectedItem().equals("ADD"))
    {
        display = "Addition";
    }
  else  if (comboBxOperator.getSelectedItem().equals("Minus"))
    {
        display = "Subtraction";
    }
    txtDisplay.setText(display);

Update: After I implemented the acionListener for my combo Box I get"UnsupportedOperationException":

更新:我为我的组合框实现了acionListener后,我得到“UnsupportedOperationException”:

comboBxOperator.addActionListener(this);

1 个解决方案

#1


3  

You are not using an EventListener, there is no way your program can detect the changes if you don't tell it the selection has changed. Check out this tutorial.

您没有使用EventListener,如果您没有告诉它选择已更改,则程序无法检测到更改。看看这个教程。

Here is a simple generic example:

这是一个简单的通用示例:

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
});

#1


3  

You are not using an EventListener, there is no way your program can detect the changes if you don't tell it the selection has changed. Check out this tutorial.

您没有使用EventListener,如果您没有告诉它选择已更改,则程序无法检测到更改。看看这个教程。

Here is a simple generic example:

这是一个简单的通用示例:

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
});