简体   繁体   English

选中时JCheckBox不更新

[英]JCheckBox not updating when selected

I'm using JCheckBox to find what a costumer wants to buy, if the JCheckBox(hat) is selected then total += hatprice, but it's not updating the total. 我正在使用JCheckBox查找客户想要购买的商品,如果选择了JCheckBox(hat),则总计+ = hatprice,但它不会更新总计。 My code : 我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NgaFillimi extends JFrame implements ActionListener{

    private JCheckBox hat;
    private JLabel thetotal;
    private int total = 0;

    public NgaFillimi() {

        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        hat = new JCheckBox("hat");
        thetotal = new JLabel("The total is " + total);

        hat.addActionListener(this);

        c.add(thetotal);
        c.add(hat);
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        if (hat.isSelected()) {
            total += 50;
        }
    }

    public static void main(String[] args) {
        NgaFillimi gui = new NgaFillimi();
        gui.setSize(300,200);
        gui.setVisible(true);
        gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
        gui.setTitle("GUI");
    }

}

You're suffering from "magical thinking". 您正在遭受“魔术思维”的困扰。 Yes, the total variable is being changed, but the JLabel's text is not going to magically change on its own just because total changes. 是的, total变量改变,但JLabel的文本是不会在自己只是因为总的变化幻化。 You the coder have to change it yourself by re-calling thetotal.setText(...) within your actionPerformed method once total has been changed. 的编码器必须通过重新调用自己去改变它thetotal.setText(...)你的actionPerformed方法中,一旦人数已经改变。

Why does your code not work? 为什么您的代码不起作用? Because all the JLabel knows is that its text is set once to a String and that's it. 因为JLabel所知道的只是它的文本一次被设置为一个字符串,仅此而已。 The String object that it displays never changes (nor can it since Strings are immutable). 它显示的String对象永远不会更改(因为String是不可变的,所以它也不会更改)。 Again, the only way for the JLabel to change its display is to explicitly call its setText method. 同样,JLabel更改其显示的唯一方法是显式调用其setText方法。

In addition, if you fixed your code so that the JLabel appropriately updates every time the JCheckBox is selected, it won't behave in a good way, since every time the JCheckBox is un-selected and then re-selected, total increments again, which doesn't seem right. 此外,如果您修改了代码,以使JLabel每次选择JCheckBox时都可以适当更新,则它的行为就不会很好,因为每次取消选择JCheckBox并重新选择JCheckBox时,总增量都会再次增加,这似乎不正确。 Better to remove 50 when the JCheckBox is unselected and then add 50 back when it's selected. 最好在未选中JCheckBox的情况下删除50,然后在选择JCheckBox的情况下再添加50。


I'm trying to what you said now that I fixed it, but it's getting negative, since I have a bunch of other JCheckBoxes 我已经解决了,现在我正在尝试您所说的,但是由于我还有很多其他JCheckBoxes,它正在变得负面

Then don't add/subtract but rather consider giving your gui a method, say called sumAllCheckBoxes() and have all the JCheckBox listeners call this method whether they are checked or unchecked. 然后,不要添加/减去,而是考虑给您的gui一个方法,例如称为sumAllCheckBoxes()并让所有JCheckBox侦听器都调用此方法,无论它们是选中还是未选中。 The method will zero out total -- total = 0; 该方法将total归零total = 0; , and then go through all the JCheckBoxes, adding cost if the JCheckBox is checked, eg: ,然后遍历所有JCheckBoxes,如果选中了JCheckBox,则增加成本,例如:

public void sumAllCheckBoxes() {
    total = 0;
    if (someCheckBox.isSelected()) {
        total += someValue;
    }
    if (someOtherCheckBox.isSelected()) {
        total += someOtherValue;
    }

    // .... etc...

    // then set the text for theTotal here

}

At the end it sets the JLabel. 最后,它设置了JLabel。 The key is to think through what you want your code doing at each step of your program. 关键是仔细考虑您希望代码在程序的每个步骤中做什么。

You haven't updated the actual JLabel . 您尚未更新实际的JLabel Rather, you updated the total variable. 相反,您更新了total变量。 To update the JLabel , you would need code that says thetotal.setText("The total is " + total) . 要更新JLabel ,您需要代码thetotal.setText("The total is " + total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM