Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import java.text.DecimalFormat;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import java.awt.BorderLayout;
- import java.awt.Font;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.border.BevelBorder;
- import javax.swing.border.MatteBorder;
- import java.awt.Color;
- import javax.swing.border.LineBorder;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Window {
- double first=0;
- double sec=0;
- int n=1;
- int art=0;
- boolean c=false;
- boolean onlysec=false;
- String zeichen=null;
- private JFrame frame;
- private JLabel ausgabefeld;
- String pattern = "###,###.##########";
- DecimalFormat format = new DecimalFormat(pattern);
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Window window = new Window();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Window() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- public static int dec_length(double input) {
- String param=(""+input);
- String vergleich =(".0");
- String sub= param.substring((param.indexOf('.')));
- if (sub.equals(vergleich)) {
- return 1;
- }else
- return sub.length();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 279, 506);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JLabel lblNewLabel;
- ausgabefeld = new JLabel("");
- ausgabefeld.setBorder(new LineBorder(new Color(0, 0, 0), 3));
- ausgabefeld.setBounds(10, 35, 243, 77);
- frame.getContentPane().add(ausgabefeld);
- JButton button_1 = new JButton("1");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+1;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+1;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- System.out.println(first+"");
- System.out.println(dec_length(first));
- first=((first*Math.pow(10, dec_length(first)))+1)/Math.pow(10, dec_length(first));
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+1)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_1.setBounds(3, 159, 50, 50);
- frame.getContentPane().add(button_1);
- JButton button_2 = new JButton("2");
- button_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+2;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+2;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+2)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+2)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_2.setBounds(63, 159, 50, 50);
- frame.getContentPane().add(button_2);
- JButton button_3 = new JButton("3");
- button_3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+3;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+3;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+3)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+3)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_3.setBounds(123, 159, 50, 50);
- frame.getContentPane().add(button_3);
- JButton button_4 = new JButton("4");
- button_4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+4;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+4;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+4)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+4)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_4.setBounds(3, 220, 50, 50);
- frame.getContentPane().add(button_4);
- JButton button_5 = new JButton("5");
- button_5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+5;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+5;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+5)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+5)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_5.setBounds(63, 220, 50, 50);
- frame.getContentPane().add(button_5);
- JButton button_6 = new JButton("6");
- button_6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+6;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+6;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+6)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+6)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_6.setBounds(123, 220, 50, 50);
- frame.getContentPane().add(button_6);
- JButton button_7 = new JButton("7");
- button_7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+7;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+7;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+7)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+7)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_7.setBounds(3, 281, 50, 50);
- frame.getContentPane().add(button_7);
- JButton button_8 = new JButton("8");
- button_8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+8;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+8;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+8)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+8)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_8.setBounds(63, 281, 50, 50);
- frame.getContentPane().add(button_8);
- JButton button_9 = new JButton("9");
- button_9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10)+9;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10)+9;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10)+9)/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10)+9)/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_9.setBounds(123, 281, 50, 50);
- frame.getContentPane().add(button_9);
- JButton button_0 = new JButton("0");
- button_0.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(c==false) {
- if(n==1) {
- first=(first*10);
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=(sec*10);
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }else if(c==true) {
- if(n==1) {
- first=((first*10))/10;
- ausgabefeld.setText(""+format.format(first));
- }
- else if(n==2) {
- sec=((sec*10))/10;
- ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
- }
- else {
- }
- }
- }
- });
- button_0.setBounds(63, 342, 50, 50);
- frame.getContentPane().add(button_0);
- JButton button_add = new JButton("+");
- button_add.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- art=1;
- c=false;
- zeichen=("+");
- System.out.println(""+n);
- if(n==1) {
- n=2;
- onlysec=false;
- ausgabefeld.setText(format.format(first)+"+");
- }else {
- }
- }
- });
- button_add.setBounds(208, 159, 50, 50);
- frame.getContentPane().add(button_add);
- JButton button_sub = new JButton("-");
- button_sub.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- art=2;
- c=false;
- zeichen=("-");
- if(n==1) {
- n=2;
- onlysec=false;
- ausgabefeld.setText(format.format(first)+"-");
- }else {
- }
- }
- });
- button_sub.setBounds(208, 220, 50, 50);
- frame.getContentPane().add(button_sub);
- JButton button_div = new JButton("/");
- button_div.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- art=3;
- c=false;
- zeichen=("/");
- if(n==1) {
- n=2;
- onlysec=false;
- ausgabefeld.setText(format.format(first)+"/");
- }else {
- }
- }
- });
- button_div.setBounds(208, 281, 50, 50);
- frame.getContentPane().add(button_div);
- JButton button_mult = new JButton("*");
- button_mult.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- art=4;
- c=false;
- zeichen=("*");
- if(n==1) {
- n=2;
- onlysec=false;
- ausgabefeld.setText(format.format(first)+"*");
- }else {
- }
- }
- });
- button_mult.setBounds(208, 342, 50, 50);
- frame.getContentPane().add(button_mult);
- JButton button_ergebnis = new JButton("=");
- button_ergebnis.setFont(new Font("Rockwell", Font.PLAIN, 32));
- button_ergebnis.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- n=3;
- c=false;
- double erg=0;
- switch(art) {
- case 1:
- erg=(first+sec);
- ausgabefeld.setText(""+format.format(erg));
- break;
- case 2:
- erg=(first-sec);
- ausgabefeld.setText(""+format.format(erg));
- break;
- case 3:
- erg=(first/sec);
- ausgabefeld.setText(""+format.format(erg));
- break;
- case 4:
- erg=(first*sec);
- ausgabefeld.setText(""+format.format(erg));
- break;
- }
- first=erg;
- sec=0;
- n=1;
- art=0;
- onlysec=true;
- }
- });
- button_ergebnis.setBounds(3, 403, 255, 50);
- frame.getContentPane().add(button_ergebnis);
- JButton button_delete = new JButton("C");
- button_delete.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- first=0;
- sec=0;
- c=false;
- n=1;
- art=0;
- ausgabefeld.setText("");
- }
- });
- button_delete.setBounds(3, 342, 50, 50);
- frame.getContentPane().add(button_delete);
- JButton button_comma = new JButton(",");
- button_comma.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- c=true;
- if(n==1) {
- ausgabefeld.setText(format.format(first)+",");
- }else if(n==2) {
- ausgabefeld.setText(format.format(first)+zeichen+format.format(sec)+",");
- }else {
- }
- }
- });
- button_comma.setBounds(123, 342, 50, 50);
- frame.getContentPane().add(button_comma);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement