Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';不要跨类更改JLabel_Java_Swing_User Interface_Jframe_Jlabel - Fatal编程技术网

Java Can';不要跨类更改JLabel

Java Can';不要跨类更改JLabel,java,swing,user-interface,jframe,jlabel,Java,Swing,User Interface,Jframe,Jlabel,我有一个涉及一些基本JavaGUI的多类项目。我将使用10个类创建贷款计算器(其中6个是JPanel子类、一个计算类、一个CombinedPanels类、一个LoanCalculatorGUI类,该类创建CombinedPanels类和calculation类的实例,以及一个驱动程序)。我必须在一个JPanel子类(ActionButtons)中创建一个重置按钮,然后在另一个JPanel子类(PaymentInformation)中更改一个私有JLabel。下面是ActionButtons类:

我有一个涉及一些基本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中)如下所示:

最后,这里是GUI的一个图像:

即使“5.0”JLabel应更改为“0.0”,当点击重置时,它仍保持不变。但是,“退出”按钮可以正常工作

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

试试这个

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

您有3个不同的
PaymentInformation
实例。第一个在
组合面板
类中(显示的一个),一个在
操作按钮
类中,一个在
按钮列表器
类中。只更改最后一个(不可见)的值

因此,一种解决方案是将
组合面板
类的(可见的)
pi
传递给
ActionButtons
类,并在该实例上调用
changeValues()

相关代码(已更改):



不适合我。请注意,您可以在更改一点发布的代码后,先自己尝试一下。这很有效!我明白你的意思。谢谢你的帮助。
public void changeValues(String val){
    loanVal=new JLabel(val, JLabel.RIGHT);
 }
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
            }

        }

    }
}