Java - 无法更新JLabel的文本

时间:2012-10-24 06:39:50

标签: java swing debugging constructor jlabel

我有我的Store类,其中我可以将商品添加到购物车,然后它将增加购物车项目编号和总金额。

我也有一个像购物车按钮一样的东西,如果点击它会显示购物车中的商品显示的另一个框架。当我从这个购物车框架中点击删除按钮时,我计划减少前一帧中的计数和总金额,但我在总价格所在的jLabel中使用的setText方法不起作用。

我从购物车框架调用此方法,然后在我点击删除按钮

时传递要删除的价格
public void updateTotalAmount(double deduct){
    System.out.println("updateTotalAmount - "+deduct);
    tAPriceL.setText(String.valueOf(deduct)); //Total amount price label
    cICountL.setText(String.valueOf(--cICount)); //cart item count label
}

system.out行是唯一有效的语句,其余的则没有。

当我尝试交换这样的代码时。

public void updateTotalAmount(double deduct){
    tAPriceL.setText(String.valueOf(deduct)); //Total amount price label
    cICountL.setText(String.valueOf(--cICount)); //cart item count label
    System.out.println("updateTotalAmount - "+deduct);
}

system.out现在没有用,所以我猜setText部分有问题。

我无法弄清问题在哪里。 有人可以帮我这个吗?

这是这个的摘要.. 对于主要商店类..例如我有5000件物品

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class NewClass extends JFrame implements ActionListener {

    JLabel tAPrice = new JLabel("5000");
    JButton viewcart = new JButton("view cart");

    public NewClass() {
        this.setLayout(new FlowLayout());
        add(tAPrice);
        add(viewcart);
        viewcart.addActionListener(this);
    }

    public static void main(String[] args) {
        NewClass n = new NewClass();
        n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        n.setSize(1150, 730);
        n.setVisible(true);
    }

    public void update(double deduct) {
        System.out.println("updated");
        tAPrice.setText(String.valueOf(Double.parseDouble(tAPrice.getText())
            - deduct));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == viewcart) {
            Cart2 c = new Cart2();
            c.setVisible(true);
            c.setSize(250, 230);
        }
    }
}

和购物车类...例如我想从总金额中删除1000

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Cart2 extends JFrame implements ActionListener {

    JButton remove = new JButton("remove");

    public Cart2() {
        add(remove);
        remove.addActionListener(this);
    }

    public static void main(String[] args) {
        Cart2 r = new Cart2();
        r.setVisible(true);
        r.setSize(250, 230);
        r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == remove) {
            NewClass nc = new NewClass();
            nc.update(1000);
        }
    }
}

4 个答案:

答案 0 :(得分:4)

Cart2.actionPerformed()中,您正在分配新的NewClass(),而不是使用NewClass的调用实例。尝试将NewClass的实例传递给Cart2构造函数。

例如:

public class Cart2 extends JFrame implements ActionListener {
    JButton remove = new JButton("remove");
    NewClass newClass;

    public Cart2(NewClass newClass) {
        this.newClass = newClass;
        add(remove);

        remove.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == remove) {
            // NewClass nc = new NewClass();
            newClass.update(1000);
        }
    }

}

然后在NewClass

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == viewcart) {
        Cart2 c = new Cart2(this);
        c.setVisible(true);
        c.setSize(250, 230);
    }
}

答案 1 :(得分:3)

  • 不要使用两个或更多JFrames,而不是在运行时,这是麻烦之路

  • 这是CardLayout

  • 的工作
  • 将每个JComponents放入JPanel,将JPanels放入Cards

  • (也许)然后只有一个JButton用于整个GUI

答案 2 :(得分:2)

虽然可能不是最好的,但这可能会让人们失意,并且是调试的早期测试:

tAPriceL.paintImmediately(tAPriceL.getVisibleRect());
cICountL.paintImmediately(cICountL.getVisibleRect());

编辑:留下这个,但在这种情况下不是修复。

答案 3 :(得分:2)

这只是因为你没有更新正确的NewClass实例...在Cart2 actionPerformed中,你创建一个新的NewClass实例并更新它。

您必须保留对NewClass框架实例的引用才能更新它。