有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不能跨类更改JLabel

我有一个涉及一些基本JavaGUI的多类项目。我将使用10个类创建贷款计算器(其中6个是JPanel子类、一个计算类、一个CombinedPanels类、一个LoanCalculatorGUI类,该类创建CombinedPanels类和calculation类的实例,以及一个驱动程序)。我必须在一个JPanel子类(ActionButtons)中创建一个重置按钮,然后在另一个JPanel子类(PaymentInformation)中更改一个私有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的文本,但单击reset按钮时它保持不变(在“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”,当点击重置时,它仍保持不变。但是,“退出”按钮可以正常工作

很抱歉出现了文字墙,但这个问题快把我逼疯了。任何帮助或解释都将不胜感激。提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    您有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
                }
    
            }
    
        }
    }
    
  2. # 2 楼答案

    试试这个

    public void changeValues(String val){
        loanVal=new JLabel(val, JLabel.RIGHT);
     }