java - 无法跨类更改 JLabel

标签 java swing user-interface jframe jlabel

我有一个涉及一些基本 Java GUI 的多类项目。我要使用 10 个类(其中 6 个是 JPanel 子类、一个计算类、一个 CombinedPanels 类、一个创建 CombinedPanels 类和计算类的实例的 LoanCalculatorGUI 类以及一个驱动程序)创建一个贷款计算器。我必须在 JPanel 子类(ActionButtons)之一中创建一个重置按钮,以更改不同 JPanel 子类(PaymentInformation)中的私有(private) JLabel。这是 ActionButtons 类:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class ActionButtons extends JPanel{
private JButton calc, reset, exit;
private JPanel actionButtons;
public ActionButtons(){
    PaymentInformation pi = new PaymentInformation();
    actionButtons = new JPanel(new GridLayout(1, 3, 20, 20));
    calc = new JButton("Calculate");
    reset = new JButton("Reset");
    exit = new JButton("Exit");
    actionButtons.add(calc);
    actionButtons.add(reset);
    actionButtons.add(exit);
    actionButtons.setBorder(BorderFactory.createTitledBorder("Action Buttons"));

    //Add ActionListeners
    calc.addActionListener(new ButtonListener());
    reset.addActionListener(new ButtonListener());
    exit.addActionListener(new ButtonListener());

}

public JPanel getGUI(){
    return actionButtons;
}



private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        PaymentInformation pi = new PaymentInformation();
        if(e.getActionCommand().equals("Exit")){
            System.exit(0);
        }
        if(e.getActionCommand().equals("Reset")){
            pi.changeValues("0.0");
        }
        if(e.getActionCommand().equals("Calculate")){
            //TODO DO CALCULATIONS 
        }

    }

  }
}

以及 PaymentInformation 类:

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;


 @SuppressWarnings("serial")
 public class PaymentInformation extends JPanel{
//Declare variables
private JPanel payInfo;
private JLabel loanAmt, monthPay, totalPay, loanVal, monthVal, totalVal;

public PaymentInformation(){
    //Give panel layout
    payInfo = new JPanel(new GridLayout(3, 2));
    //Give titles, set alignment
    loanAmt = new JLabel("Total Loan Amount:  $", JLabel.LEFT);
    monthPay = new JLabel("Monthly Payment:  $", JLabel.LEFT);
    totalPay = new JLabel("Total Payment:  $", JLabel.LEFT);
    loanVal = new JLabel("5.0", JLabel.RIGHT);
    monthVal = new JLabel("0.0", JLabel.RIGHT);
    totalVal = new JLabel("0.0", JLabel.RIGHT);
    //Add stuff to JPanel
    payInfo.add(loanAmt);
    payInfo.add(loanVal);
    payInfo.add(monthPay);
    payInfo.add(monthVal);
    payInfo.add(totalPay);
    payInfo.add(totalVal);
    //Set border
    payInfo.setBorder(BorderFactory.createTitledBorder("Payment Information"));
}
//Method to get the JPanel
public JPanel getGUI(){
    return payInfo;
}

public void changeValues(String val){
    loanVal.setText(val);
 }
}

我尝试使用 PaymentInformation 中的 setValue 方法来更改 JLabel 的文本,但单击重置按钮时它保持不变(为“5.0”)。我不确定是否需要这样做,但是 CombinedPanels 类(获取所有 JLabel 子类并将它们放入 JFrame 中)在这里:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class CombinedPanels extends JFrame{
public CombinedPanels(){
    setTitle("Auto Loan Calculator");
    setSize(700,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    JPanel center = new JPanel(new GridLayout(2, 2, 20, 20));

    //Add other classes to this layout
    TitleBar tb = new TitleBar();
    add(tb.getGUI(), BorderLayout.NORTH);
    ActionButtons ab = new ActionButtons();
    add(ab.getGUI(), BorderLayout.SOUTH);
    //Add center JPanel to the center of BorderLayout
    add(center, BorderLayout.CENTER);
    //Continue with adding rest of classes to center JPanel
    PaymentInformation pi = new PaymentInformation();
    center.add(pi.getGUI());
    LoanTerm lt = new LoanTerm();
    center.add(lt.getGUI());
    FinancingInformation fi = new FinancingInformation();
    center.add(fi.getGUI());
    PriceWithOptions pwo = new PriceWithOptions();
    center.add(pwo.getGUI());
 }
}

最后,这是 GUI 的图像:image .

即使“5.0”JLabel 应更改为“0.0”,重置后它仍保持不变。不过,退出按钮是有效的。

抱歉,文字墙很长,但这个问题让我发疯。非常感谢任何帮助或解释。提前致谢。

最佳答案

您有 3 个独立的 PaymentInformation 实例。第一个在 CombinedPanels 类中(显示的那个),一个在 ActionButtons 类中,一个在 ButtonListener 类中。您只需更改最后一个值(不可见)。

因此,一种解决方案是将 CombinedPanels 类的(可见)pi 传递给 ActionButtons 类并调用 changeValues () 实例,而不是其他实例。

相关代码(已更改):

public CombinedPanels() {
    setTitle("Auto Loan Calculator");
    setSize(700, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    JPanel center = new JPanel(new GridLayout(2, 2, 20, 20));

    // Add other classes to this layout
    PaymentInformation pi = new PaymentInformation();
    ActionButtons ab = new ActionButtons(pi);
    add(ab.getGUI(), BorderLayout.SOUTH);
    // Add center JPanel to the center of BorderLayout
    add(center, BorderLayout.CENTER);
    // Continue with adding rest of classes to center JPanel
    center.add(pi.getGUI());
    setVisible(true);
}


public class ActionButtons extends JPanel {
    private JButton calc, reset, exit;
    private JPanel actionButtons;
    PaymentInformation pi;

    public ActionButtons(PaymentInformation pi) {
        this.pi = pi;
        actionButtons = new JPanel(new GridLayout(1, 3, 20, 20));
        calc = new JButton("Calculate");
        reset = new JButton("Reset");
        exit = new JButton("Exit");
        actionButtons.add(calc);
        actionButtons.add(reset);
        actionButtons.add(exit);
        actionButtons.setBorder(BorderFactory.createTitledBorder("Action Buttons"));

        // Add ActionListeners
        calc.addActionListener(new ButtonListener());
        reset.addActionListener(new ButtonListener());
        exit.addActionListener(new ButtonListener());

    }

    public JPanel getGUI() {
        return actionButtons;
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Exit")) {
                System.exit(0);
            }
            if (e.getActionCommand().equals("Reset")) {
                pi.changeValues("0.0");
            }
            if (e.getActionCommand().equals("Calculate")) {
                // TODO DO CALCULATIONS
            }

        }

    }
}

关于java - 无法跨类更改 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809114/

相关文章:

c++ - 在 WM6+ 上覆盖系统绘图

java - Java 可以重用未处置的系统 GUI 资源吗?

java - 适用于 Android 的 Adob​​e AIR 打包

java - 即使我将编码设置为 UTF-8,文件也不会以 UTF-8 编码保存

java - 在 NTFS 分区上时,Powermock 会降低 Eclipse/Fedora 10 上的测试启动速度

java - 使用 GridBagLayout 需要帮助

java - 如何在Java Native Interface中获取Java "object"地址

java - Java 对话框弹出框中的随机图像

Java- 将文本写入图像,然后写入输出文件

java - 使用 JFIleChooser 在 Java 中创建 Open 函数