import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
public class CalcPanel
{
static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public static void main( String[] args )
{
JFrame f = new JFrame();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout( gbl );
JTextField field;
field = new JTextField();
JButton[] numbers = new JButton[10];
for(int i = 0; i < numbers.length; i++){
numbers[i] = new JButton(Integer.toString(i));
}
JButton sum, sub, div, mult;
sum = new JButton("+");
sub = new JButton("-");
div = new JButton("*");
mult = new JButton("/");
JButton C;
C=new JButton("C");
JButton erg;
erg = new JButton("=");
JButton point;
point =new JButton(".");
MeineKlasse meinObjekt = new MeineKlasse();
ActionListener al = new MeinActionListener(meinObjekt){
public void actionPerformed(ActionEvent e){
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
String text = ((JButton)e.getSource()).getText();
field.setText(field.getText()+text);
try {
// Evaluate the expression
Object result = engine.eval(field.getText());
this.meinObjekt.setBerechnung(String.valueOf(result));
System.out.println(result);
}
catch (ScriptException h) {
System.out.println("Fehler");
h.printStackTrace();
}
if(e.getSource() == sum){
field.setText(field.getText());
}
else if(e.getSource() == sub){
field.setText(field.getText());
}
else if (e.getSource() == mult){
field.setText(field.getText());
}
else if (e.getSource() == div){
field.setText(field.getText());
}
}
};
ActionListener al2 = new MeinActionListener(meinObjekt){
public void actionPerformed(ActionEvent f){
field.setText("");
}
};
ActionListener al3 = new MeinActionListener(meinObjekt){
public void actionPerformed(ActionEvent g){
/* hier kann man dann auf this.meinObjekt aus MeinActionListener zugreifen z.b. also this.meinObjekt.setBerechnung(berechnung); oder this.meinObjekt.getBerechnung();
*/
if (g.getSource()==erg){
field.setText();
};
}
numbers[0].addActionListener(al);
numbers[1].addActionListener(al);
numbers[2].addActionListener(al);
numbers[3].addActionListener(al);
numbers[4].addActionListener(al);
numbers[5].addActionListener(al);
numbers[6].addActionListener(al);
numbers[7].addActionListener(al);
numbers[8].addActionListener(al);
numbers[9].addActionListener(al);
point.addActionListener(al);
C.addActionListener(al2);
sum.addActionListener(al);
sub.addActionListener(al);
mult.addActionListener(al);
div.addActionListener(al);
erg.addActionListener(al);
// x y w h wx wy
addComponent( c, gbl, field, 0, 0, 4, 1, 1.0, 1.0);
addComponent( c, gbl, C, 0, 1, 2, 1, 0 , 0 );
addComponent( c, gbl, new JButton("←"), 2, 1, 1, 1, 0 , 0 );
addComponent( c, gbl, div , 3, 1, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[7], 0, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[4], 0, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[1], 0, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("+/-"), 0, 6, 1, 1, 0, 0 );
addComponent( c, gbl, numbers[8], 1, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[5], 1, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[2], 1, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[0], 1, 6, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[9], 2, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[6], 2, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, numbers[3], 2, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, point, 2, 6, 1, 1, 0 , 0 );
addComponent( c, gbl, mult, 3, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, sub, 3, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, sum, 3, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, erg, 3, 6, 1, 1, 0 , 0 );
f.setSize( 250, 250);
f.setVisible( true );
}
Add a code snippet to your website: www.paste.org