Ich bin ein Anfänger Java-Student arbeitet derzeit eine Gui-Aufgabe, die Berechnung der Einkäufe in einem Auto-Werk Garage umfasst, habe ich die Mehrheit abgeschlossen, aber ich bin fest, wenn eine Zusammenfassung Panel-Klasse machen.Die erste Code-Block ist meine Handler-Klasse ohne Probleme und die nächste ist die Zusammenfassung beide enthalten die erforderlichen importierten Pakete, ich kann nicht herausfinden, wie die Variablenwerte aus dem Handler auf die Übersichtsseite übergeben und sie den TextFields zuweisen . Ich entschuldige mich für das Format dieses Post es ist in der Tat meine erste. Bei Bedarf kann ich den Code aus meinen anderen Platten bietenGui Getter und Setter-Methode
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
/** * Enthält den Ereigniscode Handhabung der Berechnung und Exit-Button Funktionen
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 retireve 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;
}
laufen
}