User interface Gui getter和setter方法
我是一名java初学者,目前正在从事一项gui任务,该任务涉及计算在auto motive车库购买的物品,我完成了大部分,但在创建摘要面板类时遇到了问题。第一块代码是我的handler类,没有问题,下一块是摘要,其中包括所需的导入包,我似乎不知道如何将变量值从处理程序传递到摘要页面,并将它们分配到文本字段。我为这篇文章的格式道歉,事实上这是我的第一篇。如果需要,我可以从其他面板提供代码User interface Gui getter和setter方法,user-interface,panel,getter-setter,class-variables,User Interface,Panel,Getter Setter,Class Variables,我是一名java初学者,目前正在从事一项gui任务,该任务涉及计算在auto motive车库购买的物品,我完成了大部分,但在创建摘要面板类时遇到了问题。第一块代码是我的handler类,没有问题,下一块是摘要,其中包括所需的导入包,我似乎不知道如何将变量值从处理程序传递到摘要页面,并将它们分配到文本字段。我为这篇文章的格式道歉,事实上这是我的第一篇。如果需要,我可以从其他面板提供代码 import java.awt.*; import java.awt.event.*; import java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
/**
* Contains the event handling code to run the calculation and exit button functions
public class JoesAutoHandler implements ActionListener
{
// instance variables of the JoesAutoGUI and Tax rate
private final double TAX_RATE = 0.07;
protected JoesAutoGUI gui;
/**
* Constructor for objects of class EventHandler
*/
public JoesAutoHandler(JoesAutoGUI gui)
{
// initialize instance of gui referencing the JoesAutoGUI
this.gui = gui;
}
/**
* method called when the buttons are clicked
* performs the required total calculations when the calcButton is clicked
* closes program when exit button is clicked
*
*/
public void actionPerformed(ActionEvent e)
{
//variables used within this method
double subTotal,
totalTax = 0,
routineCharge = 0,
nonRoutineCharge = 0,
totalCharges = 0;
int totalCustomers = 0,
response;
//sets the values decimal formats
DecimalFormat dollar = new DecimalFormat("$#,##0.00");
// method runs when the calcButton is clicked
if(e.getSource() == gui.calcButton)
{
//call method to retrieve the routine charge amount
routineCharge = gui.routinePanel.getCharges();
//call the getCharges() method to retrieve the non routine charge amount
nonRoutineCharge = gui.nonRoutinePanel.getCharges();
//calculations for subtotal
subTotal = routineCharge + nonRoutineCharge;
//calculates the total tax
totalTax = subTotal * TAX_RATE;
//calculates the total charges
totalCharges = subTotal + totalTax;
//Displays the variable values after the calculations in the details window in dollar format
response = JOptionPane.showConfirmDialog(null, "Total Routine Service Charge:" + dollar.format(routineCharge) + "\n"
+ "Non Routine Service Charge:" + dollar.format(nonRoutineCharge) + "\n"
+ "Sub-Total:" + dollar.format(subTotal) + "\n"
+ "Total tax:" + dollar.format(totalTax) + "\n"
+ "Total Charges:" + dollar.format(totalCharges),
"Comfirm Customer Reciept",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
//increase the totalCustomer value when the calcButton is clicked
totalCustomers++;
}
//if the exitButton is clicked the program will close
if(e.getSource() == gui.exitButton) System.exit(0);
}
}
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class SummaryPanel extends JPanel
{
// instance variables for the labels, text fields and values
protected JLabel totalCustomersLabel,
totalRoutineChargesLabel,
totalNonRoutineChargesLabel,
totalTaxLabel,
totalChargesLabel;
protected JoesAutoHandler gui;
protected JTextField totalCustomersTextField,
totalRoutineChargesTextField,
totalNonRoutineChargesTextField,
totalTaxTextField,
totalChargesTextField;
//instance of the summary panel variables
private int totalCustomers;
private double totalRoutineServiceCharges;
private double totalNonRoutineServiceCharges;
private double totalTax;
private double totalCharges;
/**
* Constructor for objects of class SummaryPanel
*/
public SummaryPanel()
{
// creates decimal format objects fot the totals variables
DecimalFormat totalCustomerDf = new DecimalFormat("#,##0.00");
DecimalFormat totalRoutineChargesDf = new DecimalFormat("#,##0.00");
DecimalFormat totalNonRoutineChargesDf = new DecimalFormat("#,##0.00");
DecimalFormat totalTaxDf = new DecimalFormat("#,##0.00");
DecimalFormat totalChargesDf = new DecimalFormat("#,##0.00");
//creates the gridLayout with 5 row and 2 columns
setLayout(new GridLayout(5,2));
//creates the label objects and sets the string text
totalCustomersLabel = new JLabel("Total Customers:");
totalRoutineChargesLabel = new JLabel("Total Routine Charges:");
totalNonRoutineChargesLabel = new JLabel("Total NonRoutine Charges:");
totalTaxLabel = new JLabel("Total Tax:");
totalChargesLabel = new JLabel("Total Charges:");
//creates the text field components and initalizes them to 0
totalCustomersTextField = new JTextField("0");
totalRoutineChargesTextField = new JTextField("0");
totalNonRoutineChargesTextField = new JTextField("0");
totalTaxTextField = new JTextField("0");
totalChargesTextField = new JTextField("0");
totalCustomersTextField.setText("" + totalCustomers);
//sets the labels to right-justification
totalCustomersLabel.setHorizontalAlignment(JLabel.RIGHT);
totalRoutineChargesLabel.setHorizontalAlignment(JLabel.RIGHT);
totalNonRoutineChargesLabel.setHorizontalAlignment(JLabel.RIGHT);
totalTaxLabel.setHorizontalAlignment(JLabel.RIGHT);
totalChargesLabel.setHorizontalAlignment(JLabel.RIGHT);
//sets the textboxes to right-justification
totalCustomersTextField.setHorizontalAlignment(JTextField.RIGHT);
totalRoutineChargesTextField.setHorizontalAlignment(JTextField.RIGHT);
totalNonRoutineChargesTextField.setHorizontalAlignment(JTextField.RIGHT);
totalTaxTextField.setHorizontalAlignment(JTextField.RIGHT);
totalChargesTextField.setHorizontalAlignment(JTextField.RIGHT);
//makes the textboxes read only
totalCustomersTextField.setEditable(false);
totalRoutineChargesTextField.setEditable(false);
totalNonRoutineChargesTextField.setEditable(false);
totalTaxTextField.setEditable(false);
totalChargesTextField.setEditable(false);
//adds components to the panel
add(totalCustomersLabel);
add(totalCustomersTextField);
add(totalRoutineChargesLabel);
add(totalRoutineChargesTextField);
add(totalNonRoutineChargesLabel);
add(totalNonRoutineChargesTextField);
add(totalTaxLabel);
add(totalTaxTextField);
add(totalChargesLabel);
add(totalChargesTextField);
//converts the variable data types to String
String totalCustomersText = Integer.toString(totalCustomers);
String totalRoutineServiceChargesText = Double.toString(totalRoutineServiceCharges);
String totalNonRoutineServiceChargesText = Double.toString(totalNonRoutineServiceCharges);
String totalTaxText = Double.toString(totalTax);
String totalChargesText = Double.toString(totalCharges);
//sets the component text fields with the variable values
totalCustomersTextField.setText(totalCustomersText);
totalRoutineChargesTextField.setText(totalRoutineServiceChargesText);
totalNonRoutineChargesTextField.setText(totalNonRoutineServiceChargesText);
totalTaxTextField.setText(totalTaxText);
totalChargesTextField.setText(totalChargesText);
}
/**
*get set methods for all variables
*/
public int getTotalCustomers()
{
// returns total customers
return totalCustomers;
}
public void setTotalCustomers(int totalCustomers)
{
//set total customers
this.totalCustomers = totalCustomers;
}
public double getTotalRoutineServiceChargess()
{
// gets totalRoutineServiceCharges
return totalRoutineServiceCharges;
}
public void setTotalRoutineServiceCharges(double totalRoutineServiceCharges)
{
//sets totalRoutineServiceCharges
this.totalRoutineServiceCharges = totalRoutineServiceCharges;
}
public double getTotalNonRoutineCharges()
{
//gets totalNonRoutineServiceCharges
return totalNonRoutineServiceCharges;
}
public void setTotalNonRoutineServiceCharges(double totalNonRoutineServiceCharges)
{
//sets totalNonRoutineServiceCharges
this.totalNonRoutineServiceCharges = totalNonRoutineServiceCharges;
}
public double getTotalTax()
{
// gets total Tax
return totalTax;
}
public void setTotalTax(double totalTax)
{
//set total tax
this.totalTax = totalTax;
}
public double getTotalCharges()
{
// gets total charges
return totalCharges;
}
public void setTotalCharges(double totalCharges)
{
//sets total charges
this.totalCharges = totalCharges;
}
}
啊,我明白问题所在了,我需要编写代码来设置处理程序类中的摘要文本字段,并为摘要类创建一个引用对象