Hi Leute,
ich habe nun einige Zeit dran gesessen und finde einfach keine Lösung dazu, ich hoffe ihr könnt mir da weiterhelfen.
Ich habe ein Tabpanel, jeder Tab hat eine eigene Gui und auch eine Klasse.
Ich habe eine StartGui, die mir alles initialisiert und erstellt.
Ich erstelle nun in der StartGui ein Variable total, die mir den totalen Preis anzeigt von allen Gui´s, die übergebe ich als Parameter für alle Gui´s und referenziere es in jeder Gui.
Jede Gui fängt aber immer wieder von 0€ an zu rechnen, anstatt das total von der vorherigen Klasse zu benutzen .. Ich habe es auch schon mit der getter Methode probiert, was den gleichen Effekt hat, weil das Objekt da neu erstellt werden muss ...
Hier der Code von der Startgui
Das ist ist eine Gui´s die in der StartGui erstellt wurden
Hier die zweite :
In beiden Gui´s wird total benutzt von der StartGui, aber total ist bei beiden Gui´s unabhängig und wird auch unabhängig voneinandern berechnet ...
Ich bedanke mich im voraus
ich habe nun einige Zeit dran gesessen und finde einfach keine Lösung dazu, ich hoffe ihr könnt mir da weiterhelfen.
Ich habe ein Tabpanel, jeder Tab hat eine eigene Gui und auch eine Klasse.
Ich habe eine StartGui, die mir alles initialisiert und erstellt.
Ich erstelle nun in der StartGui ein Variable total, die mir den totalen Preis anzeigt von allen Gui´s, die übergebe ich als Parameter für alle Gui´s und referenziere es in jeder Gui.
Jede Gui fängt aber immer wieder von 0€ an zu rechnen, anstatt das total von der vorherigen Klasse zu benutzen .. Ich habe es auch schon mit der getter Methode probiert, was den gleichen Effekt hat, weil das Objekt da neu erstellt werden muss ...
Hier der Code von der Startgui
Java:
public class StartGui extends JFrame {
private final Object[] columnNames = {"Auswahl", "Preis"};
private final Object[][] rowDataInit = {{"Auswahl", "Preis in Euro"}};
private DefaultTableModel model;
int total = 0;
public StartGui() {
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel panel = new JPanel();
JTabbedPane tabLeiste = new JTabbedPane();
JLabel price = new JLabel("Total Preis:" + total + "€");
JTable table = new JTable();
model = new DefaultTableModel(rowDataInit, columnNames);
table.setModel(model);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table);
panel.add(price);
SizeGui size = new SizeGui(table, tabLeiste, total, price);
int totalFlavour = size.getTotal();
tabLeiste.addTab("Tab1", size);
FlavourGui flavour = new FlavourGui(table, tabLeiste, totalFlavour, price);
int totalTopping = flavour.getTotal();
tabLeiste.addTab("Tab2", flavour);
ToppingGui topping = new ToppingGui(table, tabLeiste, totalTopping, price);
tabLeiste.addTab("Tab3", topping);
SummaryGui summary = new SummaryGui(tabLeiste);
tabLeiste.addTab("Tab4", summary);
PaymentGui payment = new PaymentGui(tabLeiste);
tabLeiste.addTab("Tab5", payment);
this.add(tabLeiste, BorderLayout.CENTER);
this.add(panel,BorderLayout.EAST);
}
Das ist ist eine Gui´s die in der StartGui erstellt wurden
Java:
public class SizeGui extends JPanel implements ActionListener {
private static final Object[][] rowData = {{"small", "1"}, {"middle", "2"}, {"big", "3"}};
private JTable table;
private DefaultTableModel model;
private JTabbedPane tabLeiste;
public int total;
private JLabel price;
//private JPanel panel = new JPanel();
//set size of panel and add all elements on the right place in layout
public SizeGui(JTable table, JTabbedPane tabLeiste, int total, JLabel price) {
//north panel with back button
setSize(800, 600);
this.table = table;
this.model = (DefaultTableModel) table.getModel();
this.tabLeiste = tabLeiste;
this.total = total;
this.price = price;
...
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals(Commandos.SMALL)) {
model.addRow(rowData[0]);
total = total + Integer.valueOf((String) rowData[0][1]);
price.setText("Total Preis:" + total + "€");
} else if (ae.getActionCommand().equals(Commandos.MIDDLE)) {
model.addRow(rowData[1]);
total = total + Integer.valueOf((String) rowData[1][1]);
price.setText("Total Preis:" + total + "€");
} else if (ae.getActionCommand().equals(Commandos.BIG)) {
model.addRow(rowData[2]);
total = total + Integer.valueOf((String) rowData[2][1]);
price.setText("Total Preis:" + total + "€");
} else if (ae.getActionCommand().equals(Commandos.CONTINUE)) {
tabLeiste.setSelectedIndex(1);
}
}
public int getTotal() {
return this.total;
}
}
Hier die zweite :
Java:
public class FlavourGui extends JPanel implements ActionListener {
private static final Object[][] rowData = {{"chocolate", "1"}, {"banana", "1"}, {"blueberry", "1"}};
private JTable table;
private DefaultTableModel model;
private JTabbedPane tabLeiste;
private int totalFlavour;
private JLabel price;
public FlavourGui(JTable table, JTabbedPane tabLeiste, int totalFlavour, JLabel price) {
setSize(800, 600);
this.table = table;
this.model = (DefaultTableModel) table.getModel();
this.tabLeiste = tabLeiste;
this.totalFlavour = totalFlavour;
this.price = price;
....
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals(Commandos.CHOCOLATE)) {
model.addRow(rowData[0]);
totalFlavour =+ Integer.valueOf((String) rowData[0][1]);
//System.out.println(new SizeGui(table,tabLeiste,total,price).getTotal());
price.setText("Total Preis:" + totalFlavour + "€");
} else if (ae.getActionCommand().equals(Commandos.BANANA)) {
model.addRow(rowData[1]);
totalFlavour =+ Integer.valueOf((String) rowData[1][1]);
price.setText("Total Preis:" + totalFlavour + "€");
} else if (ae.getActionCommand().equals(Commandos.BLUEBERRY)) {
model.addRow(rowData[2]);
totalFlavour =+ Integer.valueOf((String) rowData[2][1]);
price.setText("Total Preis:" + totalFlavour + "€");
} else if (ae.getActionCommand().equals(Commandos.CONTINUE)) {
tabLeiste.setSelectedIndex(2);
} else if (ae.getActionCommand().equals(Commandos.BACK)) {
tabLeiste.setSelectedIndex(0);
}
}
public int getTotal() {
return this.totalFlavour;
}
}
In beiden Gui´s wird total benutzt von der StartGui, aber total ist bei beiden Gui´s unabhängig und wird auch unabhängig voneinandern berechnet ...
Ich bedanke mich im voraus
Zuletzt bearbeitet von einem Moderator: