帮助在 JComboBox 中的 ItemChange 时自动更改值

发布于 2024-11-28 10:20:44 字数 902 浏览 2 评论 0原文

我有一个程序,其中使用了 3 个东西:一个复选框、一个组合框和一个文本字段。如果启用了复选框,则逻辑将如下所示,否则将启用组合框和文本字段,除非未启用。

然后通过将文本字段与组合框中的项目相乘来设置文本字段中的一些值。

我正在处理的 JFrame

从框架中 - 最终价格的值为价格 * 数量。

现在,当我点击购买时,问题一切顺利。但是,当我更改 Jcombobox 中的值时,它不会自动更改最终价格的值,并且仍为第一种情况下的 1200。对于要更改的值,我取消选中然后选中复选框。

可能是什么问题。我已将 ItemListner 用于复选框和组合框。

@Override
public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();

    int multiplier;
    int ftotal;


    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==chkbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);
             multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));


             ftotal = Integer.parseInt(price1.getText()) * multiplier;
             fprice1.setText(String.valueOf(ftotal));}

I have a program in which I am using 3 things, a checkbox, a combobox and a textfield. The logic works like this if checkbox is enable then combobox and textfield are enable unless not.

Then set some value in the textfield by mulitplying it with the item in combobox.

The JFrame that I am working on

From the frame - The value of Final Price is Price * Quantity.

Now the issue when I click purchase everything went fine. But when I change the value from Jcombobox it doesn't automatically change the value in final price and remains to be 1200 as in first case. For the value to be changed I have uncheck and then check the Checkbox.

What could be the problem. I have used ItemListner for both checkbox and combobox.

@Override
public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();

    int multiplier;
    int ftotal;


    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==chkbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);
             multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));


             ftotal = Integer.parseInt(price1.getText()) * multiplier;
             fprice1.setText(String.valueOf(ftotal));}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-12-05 10:20:44

您必须为您的 JComboBox 实现 ActionListener

private static final String command_cbo1 = "ComboBox1";
// ...

public class YourClass implements ItemListener, ActionListener
{
    // ...

    public YourClass()
    {
        // ...
        qntbox1.addActionListener(this);
        qntbox1.setActionCommand(command_cbo1);
        // ...
    }

    // ...

    public void itemStateChanged(ItemEvent e)        
    {
        // ...
    }

    // ...

    public void actionPerformed(ActionEvent e)
    {
        JComboBox cb = (JComboBox) e.getSource();
        String s = (String) cb.getSelectedItem();

        if(e.getActionCommand().equals(command_cbo1))
        {
            fprice1.setText("" + (Integer.parseInt(price1.getText()) * Integer.parseInt(s)));
        }
        // ...
    }

    // ...
}

You have to implement ActionListener for your JComboBox:

private static final String command_cbo1 = "ComboBox1";
// ...

public class YourClass implements ItemListener, ActionListener
{
    // ...

    public YourClass()
    {
        // ...
        qntbox1.addActionListener(this);
        qntbox1.setActionCommand(command_cbo1);
        // ...
    }

    // ...

    public void itemStateChanged(ItemEvent e)        
    {
        // ...
    }

    // ...

    public void actionPerformed(ActionEvent e)
    {
        JComboBox cb = (JComboBox) e.getSource();
        String s = (String) cb.getSelectedItem();

        if(e.getActionCommand().equals(command_cbo1))
        {
            fprice1.setText("" + (Integer.parseInt(price1.getText()) * Integer.parseInt(s)));
        }
        // ...
    }

    // ...
}
薔薇婲 2024-12-05 10:20:44

不直接回答你的问题

1/ JCheckBox 完全是无用,最终计算确实需要它

2/考虑 JComponentsPriceFinal Price 将仅为 JFormattedTextField,那么你很可能会忘记 Parse#Whatever

3/ 考虑一下QuantityJComponents 将仅为 JSpinner,但是 Number 实例的解决方法会比 JFormattedTextField 示例稍微复杂此处

4/为了获得良好的输出,请将所有内容放入 JTable

5/对于 JComboBox 我更喜欢 ItemListener 不是 ActionListener,因为您的问题不在于正确的 Listener 但以正确的方式解析Numbers

not directly to your question

1/ JCheckBox is totally useless, that will be really needed for final calculation(s)

2/ consider that JComponents for Price and Final Price would be only JFormattedTextField, then you can pretty to forgot for Parse#Whatever

3/ consider that JComponents for Quantity would be only JSpinner, but workaround for Number Instance would be litte bit complicated as for JFormattedTextField example here

4/ for nice output put everything to the JTable

5/ for JComboBox I preferred ItemListener not ActionListener, because your problems isn't with proper Listener but with parsing Numbers correct way

無處可尋 2024-12-05 10:20:44

好的,它工作了。 ActionListner 使其工作(JComboBox)。我猜想对太多组件使用 ItemListner 会使解析有点混乱,此外我在 ItemListner 范围中使用了太多子句。非常感谢大家的帮助。

@mKorbel:我将尽快使用您的建议:)并将检查 JTable 和所述组件。因为我还没有使用过它们,所以必须经过它们。

@Eng.Fouad:谢谢大家的帮助。

只有一个问题。当我将 getSelectedItem() 类型转换为整数时,它会给出 NumberFormatException 错误(运行时)。所以我必须先将对象更改为字符串,然后将其解析为整数。任何线索为什么直接转换会抛出错误?

这是该项目的工作代码。

public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();



    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==chkbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);   
             fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()))));
        }
  @Override
       public void actionPerformed (ActionEvent ae)
       {

          Object toggel = ae.getSource();      
          String check;

          if (toggel == qntbox1)
          {
            check = (String) qntbox1.getSelectedItem();
            fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(check)));

          }

Ok got it working. The ActionListner made it work (JComboBox). I guess using ItemListner for too many components made parsing a little confusing, add to that I used too many clauses in the ItemListner scope. Thanks a lot everyone for helping.

@mKorbel : I'll be using your suggestion asap :) and will check JTable and said components. have to go through them since I haven't used it.

@Eng.Fouad : Thanks man for the help.

Just one issue. When I typecast getSelectedItem() to integer it gives NumberFormatException error (runtime). So I have to first change the object to String and then parseit into integer. Any clue why direct conversion is throwing error ?

Here is the working code for the project.

public void itemStateChanged(ItemEvent e){

    Object get = e.getSource();



    if (e.getStateChange()==ItemEvent.SELECTED){
        if(get==chkbox1){
             qntbox1.setEnabled(true);            
             size1.setEnabled(true);   
             fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()))));
        }
  @Override
       public void actionPerformed (ActionEvent ae)
       {

          Object toggel = ae.getSource();      
          String check;

          if (toggel == qntbox1)
          {
            check = (String) qntbox1.getSelectedItem();
            fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(check)));

          }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文