无法跨 类 更改 JLabel
Can't change a JLabel across classes
我有一个涉及一些基本 Java GUI 的多class 项目。我要使用 10 个 classes(其中 6 个是 JPanel subclasses,一个计算 class,一个 CombinedPanels class,一个 LoanCalculatorGUI [=28] 创建一个贷款计算器=] 创建 CombinedPanels class 和计算 class 的实例,以及一个驱动程序)。我必须在 JPanel subclasses(ActionButtons)之一中创建一个重置按钮,更改另一个 JPanel subclass(PaymentInformation)中的私有 JLabel。这是 ActionButtons class:
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
}
}
}
}
以及支付信息class:
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 class(获取所有 JLabel subclasses 并将它们放入 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 的图像:。
即使“5.0”JLabel 应更改为“0.0”,它在重置时保持不变。但是,退出按钮可以使用。
对不起,文字墙,但这个问题让我抓狂。非常感谢任何帮助或解释。提前致谢。
试试这个
public void changeValues(String val){
loanVal=new JLabel(val, JLabel.RIGHT);
}
您有 3 个独立的 PaymentInformation
实例。 CombinedPanels
class 中的第一个(显示的那个),ActionButtons
class 中的第一个和 ButtonListener
class 中的一个。您只需更改最后一个(不可见)的值。
所以一种解决方案是将 CombinedPanels
class 的(可见)pi
传递给 ActionButtons
class 并调用 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 GUI 的多class 项目。我要使用 10 个 classes(其中 6 个是 JPanel subclasses,一个计算 class,一个 CombinedPanels class,一个 LoanCalculatorGUI [=28] 创建一个贷款计算器=] 创建 CombinedPanels class 和计算 class 的实例,以及一个驱动程序)。我必须在 JPanel subclasses(ActionButtons)之一中创建一个重置按钮,更改另一个 JPanel subclass(PaymentInformation)中的私有 JLabel。这是 ActionButtons class:
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
}
}
}
}
以及支付信息class:
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 class(获取所有 JLabel subclasses 并将它们放入 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 的图像:
即使“5.0”JLabel 应更改为“0.0”,它在重置时保持不变。但是,退出按钮可以使用。
对不起,文字墙,但这个问题让我抓狂。非常感谢任何帮助或解释。提前致谢。
试试这个
public void changeValues(String val){
loanVal=new JLabel(val, JLabel.RIGHT);
}
您有 3 个独立的 PaymentInformation
实例。 CombinedPanels
class 中的第一个(显示的那个),ActionButtons
class 中的第一个和 ButtonListener
class 中的一个。您只需更改最后一个(不可见)的值。
所以一种解决方案是将 CombinedPanels
class 的(可见)pi
传递给 ActionButtons
class 并调用 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
}
}
}
}