import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
public class calc extends JFrame implements ActionListener {
private static final long serialVersionUID = 1054322144645546640L;
String gegeben = null;
double myGegeben = 0.0;
double myToPay = 0.0;
JButton[] numbers;
JButton storno = new JButton("Eingabe Verwerfen");
JButton payed = new JButton ("oK");
public JLabel Betrag = new JLabel("Zu zahlender Betrag: ");
JLabel gezahlt = new JLabel("Gezahlt:");
JLabel rueck = new JLabel ("Rückgeld: ");
JTextArea output = new JTextArea(1,10);
JTextArea input = new JTextArea(1,10);
JTextArea toPay = new JTextArea(1,10);
String[] buttonnames = {"1", "2", "3", "4", "5",
"6", "7", "8", "9","0", ".", ".00"};
JPanel mPan = new JPanel();
JPanel butPan = new JPanel(new GridLayout(4,3));
JPanel panl = new JPanel(new GridLayout(3,1));
JDBCAdapter summe = new JDBCAdapter(
"jdbc:mysql://localhost/restaurant",
"com.mysql.jdbc.Driver",
"root",
"");
public calc(){
mPan.add(panl);
mPan.add(butPan);
panl.add(Betrag);
panl.add(toPay);
panl.add(gezahlt);
panl.add(input);
input.setEditable(false);
toPay.setEditable(false);
numbers = new JButton[buttonnames.length];
for(int i = 0; i<buttonnames.length; i++){
numbers[i] = new JButton(buttonnames[i]);
numbers[i].setVisible(true);
numbers[i].addActionListener(this);
butPan.add(numbers[i]);
}
panl.add(rueck);
panl.add(output);
panl.add(payed);
payed.addActionListener(this);
panl.add(storno);
storno.addActionListener(this);
output.setEditable(false);
panl.setVisible(true);
panl.setPreferredSize(new Dimension(200,50));
butPan.setPreferredSize(new Dimension(200, 200));
butPan.setVisible(true);
this.setSize(300, 300);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.add(mPan);
this.setVisible(true);
}
public void actionPerformed(ActionEvent calcAE) {
try {
for (int i = 0; i<buttonnames.length; i++){
if (calcAE.getSource() == numbers[i]){
input.setText(input.getText()+""+numbers[i].getText());
gegeben = input.getText();
myGegeben = Double.parseDouble(gegeben);
System.out.println(myGegeben);
}
}
}
catch(NumberFormatException nfe) {
}
if (calcAE.getSource() == payed){
System.out.println(myGegeben);
String StrToPay = toPay.getText();
myToPay = Double.parseDouble(StrToPay);
double rech = (myToPay-myGegeben);
output.setText(""+rech);
try {
String insertme = output.getText();
summe.statement.executeUpdate("INSERT INTO Bon " +
"VALUES (null, '"+insertme+"', '0000-00-00 00:00:00', 0)");
} catch (SQLException e) {
e.printStackTrace();
}
this.dispose();
}
if (calcAE.getSource() == storno){
output.setText("");
input.setText("");
gegeben = null;
myGegeben = 0.0;
myToPay = 0.0;
}
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class produkts extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JDBCAdapter prods = new JDBCAdapter(
"jdbc:mysql://localhost/restaurant",
"com.mysql.jdbc.Driver",
"root",
"");
calc rechner;
int[] a;
double[] zwsum;
double sum;
JLabel design = new JLabel("");
JLabel[] zwischensumme = new JLabel[18];
JButton[] buttons;
JButton pay = new JButton("Bezahlen");
String[] buttonnames;
double[] price;
JPanel besteller = new JPanel(new GridLayout(10, 0));
JButton storno = new JButton("STORNO!");
public produkts(){
rechner = new calc();
// mainPan.setLayout(null);
rechner.setVisible(false);
prods.executeQuery("SELECT Artikel_Name FROM artikel");
buttonnames = new String[prods.getRowCount()];
buttons = new JButton[prods.getRowCount()];
for(int i=0; i<prods.getRowCount(); i++){
buttonnames[i] = (""+prods.getValueAt(i, 0));
buttons[i] = new JButton(buttonnames[i]);
buttons[i].addActionListener(this);
besteller.add(buttons[i]);
}
for(int j=0; j<18; j++){
zwischensumme[j] = new JLabel("");
besteller.add(zwischensumme[j]);
}
zwsum = new double[20];
a = new int[20];
besteller.add(pay);
pay.addActionListener(this);
besteller.add(storno);
storno.addActionListener(this);
besteller.setSize(800,600);
this.add(besteller);
this.setSize(800,600);
this.setVisible(false);
}
public void actionPerformed(ActionEvent peach) {
for(int i=0; i<(buttons.length); i++) {
String ProdID = null;
String Price = null;
if(peach.getSource()==storno){
sum = 0.0;
zwischensumme[i].setText("");
a[i] = 0;
rechner.toPay.setText("");
}
if(peach.getSource() == buttons[i]) {
String name = buttons[i].getText();
//System.out.println(buttons[i].getText());
prods.executeQuery("SELECT preisID FROM artikel WHERE artikel_name ='"+name+"'");
ProdID = ""+prods.getValueAt(0, 0);
prods.executeQuery("SELECT Preis FROM Preise WHERE Preis_ID = '"+ProdID+"'");
Price = ""+prods.getValueAt(0, 0);
//System.out.println(Price);
a[i]++;
double myPrice = Double.parseDouble(Price);
//System.out.println(Price);
zwsum[i] = myPrice * a[i];
zwsum[i] = Math.round(zwsum[i] * 100.)/100.;
zwischensumme[i].setText(""+a[i]+"x"+name+": "+zwsum[i]+"€\n");
}
}
if (peach.getSource() == pay) {
//Zahlfenster
sum = Math.round(sum * 100.)/100.;
sum = zwsum[0]+ zwsum[1]+ zwsum[2]+ zwsum[3]+ zwsum[4]+ zwsum[5]+
zwsum[6]+ zwsum[7]+ zwsum[8]+ zwsum[9]+ zwsum[10]+ zwsum[11]+
zwsum[12]+ zwsum[13]+ zwsum[14]+ zwsum[15]+ zwsum[16]+ zwsum[17];
rechner.toPay.setText(sum+"");
rechner.setVisible(true);
}
}
}
Hallo Livo,Frame Schließen und neustarten
package myapplication;
import java.io.*;
import java.util.logging.*;
public class Starter {
private static String JARNAME = "MyApplication.jar";
private static String MAINCLASS = "myapplication.MainGui";
private Starter() {
}
public static void main(String[] args) {
start();
}
public static void start() {
String userdir = System.getProperty("user.dir");
String classpath = "-cp \""
//the non-jar path is used in case you start
//the application from your IDE project:
+ userdir + "\\build\\classes;"
+ userdir + "\\" + JARNAME + "\" ";
String cmd = "javaw " + classpath + MAINCLASS;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException ex) {
Logger.getLogger(Starter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package myapplication;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public final class MainGui {
public static final int RESTART = 1;
public MainGui() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
JButton btRestart = new JButton("Frame Schließen und neustarten");
btRestart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exit(RESTART);
}
});
frame.add(btRestart, BorderLayout.PAGE_START);
frame.setVisible(true);
}
public void exit(final int opt) {
if (opt == RESTART) {
Starter.start();
}
System.exit(0);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
@Override
public void run() {
MainGui mainGui = new MainGui();
}
};
//GUI must start on EventDispatchThread:
EventQueue.invokeLater(gui);
}
}