我有一个涉及一些基本Java GUI的多类项目。我将使用10个类创建一个贷款计算器(其中6个是JPanel子类,一个计算类,一个CombinedPanels类,一个LoanCalculatorGUI类,它创建CombinedPanels类和计算类的实例,以及一个驱动程序)。我必须在其中一个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的文本,但是当单击重置按钮时它保持不变(在“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());
}
}
当重置命中时,它保持不变,即使“5.0”JLabel应该更改为“0.0”。但是,退出按钮功能正常。
抱歉文字的墙,但这个问题让我发疯。任何帮助或解释都非常感谢。提前谢谢。
答案 0 :(得分:1)
您有3个单独的PaymentInformation
个实例。 CombinedPanels
类中的第一个(显示的那个),ActionButtons
类中的一个和ButtonListener
类中的一个。您只能更改最后一个的值(不可见)。
因此,一种解决方案是将pi
类的(可见)CombinedPanels
传递给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
}
}
}
}
答案 1 :(得分:0)
试试这个
public void changeValues(String val){
loanVal=new JLabel(val, JLabel.RIGHT);
}