我有我的 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 部分有问题。
我不知道问题出在哪里。 谁能帮我解决这个问题吗?
这里是这个的摘要.. 对于主要商店类别..例如我有值(value) 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);
}
}
}
最佳答案
在 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);
}
}
关于Java - 无法更新 JLabel 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044065/