Hellow everyone!
Min ellers fantastiske, ultimative, guddommelige lommeregner, virker fantastisk, men får fejl når jeg tilføjer min KeyboardListener til min JLabel..
-1000 Kroner til den der svarer indenfor et et hemmeligt tidsrum!
Main class
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JButton;
- import javax.swing.JLabel;
-
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
-
- public class Main extends JFrame
- {
- Tastatur tastatur = new Tastatur();
-
- double first, second, result;
- String number = "";
- boolean bplus, bminus, btimes, bdivided;
-
- private JPanel panel;
- private JLabel label;
- private JButton b1;
- private JButton b2;
- private JButton b3;
- private JButton b4;
- private JButton b5;
- private JButton b6;
- private JButton b7;
- private JButton b8;
- private JButton b9;
- private JButton b0;
- private JButton equals;
- private JButton plus;
- private JButton minus;
- private JButton times;
- private JButton divided;
- private JButton squareRoot;
- private JButton c;
- private JButton delete;
- private JButton period;
-
-
- public Main()
- {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(432, 880);
- this.setVisible(true);
-
- panel = new JPanel();
- panel.setLayout(null);
-
- label = new JLabel(getNumber(), null, JLabel.CENTER);
- label.setBounds(0, 0, 160, 40);
- label.setForeground(Color.BLUE);
- panel.add(label);
-
- b1 = new JButton("1");
- b1.setBounds(0, 40, 50, 40);
- panel.add(b1);
-
- b1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "1";
- label.setText(getNumber());
- }
- });
-
- b2 = new JButton("2");
- b2.setBounds(50, 40, 50, 40);
- panel.add(b2);
-
- b2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "2";
- label.setText(getNumber());
- }
- });
-
- b3 = new JButton("3");
- b3.setBounds(100, 40, 50, 40);
- panel.add(b3);
-
- b3.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "3";
- label.setText(getNumber());
- }
- });
-
- b4 = new JButton("4");
- b4.setBounds(0, 80, 50, 40);
- panel.add(b4);
-
- b4.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "4";
- label.setText(getNumber());
- }
- });
-
- b5 = new JButton("5");
- b5.setBounds(50, 80, 50, 40);
- panel.add(b5);
-
- b5.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "5";
- label.setText(getNumber());
- }
- });
-
- b6 = new JButton("6");
- b6.setBounds(100, 80, 50, 40);
- panel.add(b6);
-
- b6.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "6";
- label.setText(getNumber());
- }
- });
-
- b7 = new JButton("7");
- b7.setBounds(0, 120, 50, 40);
- panel.add(b7);
-
- b7.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "7";
- label.setText(getNumber());
- }
- });
-
- b8 = new JButton("8");
- b8.setBounds(50, 120, 50, 40);
- panel.add(b8);
-
- b8.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "8";
- label.setText(getNumber());
- }
- });
-
- b9 = new JButton("9");
- b9.setBounds(100, 120, 50, 40);
- panel.add(b9);
-
- b9.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "9";
- label.setText(getNumber());
- }
- });
-
- b0 = new JButton("0");
- b0.setBounds(50, 160, 50, 40);
- panel.add(b0);
-
- b0.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "0";
- label.setText(getNumber());
- }
- });
-
- plus = new JButton("+");
- plus.setBounds(150, 160, 50, 40);
- panel.add(plus);
-
- plus.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("+");
- first = Double.parseDouble(number);
- number = "";
- bplus = true;
- bminus = false;
- bdivided = false;
- btimes = false;
- }
- });
-
- minus = new JButton("-");
- minus.setBounds(150, 120, 50, 40);
- panel.add(minus);
-
- minus.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("-");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = true;
- bdivided = false;
- btimes = false;
- }
- });
-
- times = new JButton("*");
- times.setBounds(150, 80, 50, 40);
- panel.add(times);
-
- times.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("*");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = false;
- bdivided = false;
- btimes = true;
- }
- });
-
- divided = new JButton("/");
- divided.setBounds(150, 40, 50, 40);
- panel.add(divided);
-
- divided.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("/");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = false;
- bdivided = true;
- btimes = false;
- }
- });
-
- equals = new JButton("=");
- equals.setBounds(100, 160, 50, 40);
- panel.add(equals);
-
- equals.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- second = Double.parseDouble(number);
-
- if(bplus)
- result = first + second;
-
- else if(bminus)
- result = first - second;
-
- else if(bdivided)
- result = first / second;
-
- else if(btimes)
- result = first * second;
-
- result = cutDecimals(result);
- number = Double.toString(result);
- label.setText(number);
- }
- });
-
- squareRoot = new JButton("v");
- squareRoot.setBounds(200, 160, 50, 40);
- panel.add(squareRoot);
-
- squareRoot.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
-
- double temp = Double.parseDouble(number);
- temp = Math.sqrt(temp);
- temp = cutDecimals(temp);
-
- number = Double.toString(temp);
- label.setText(number);
- }
-
- });
-
- c = new JButton("C");
- c.setBounds(200, 40, 60, 40);
- panel.add(c);
-
- c.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- setDefault();
- label.setText(number);
- }
-
- });
-
- delete = new JButton("<-");
- delete.setBounds(180, 0, 80, 40);
- panel.add(delete);
-
- delete.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- number = number.substring(0, number.length() -1);
- label.setText(number);
- }
-
- });
-
- period = new JButton(",");
- period.setBounds(0, 160, 50, 40);
- panel.add(period);
-
- period.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- number += ".";
- label.setText(getNumber());
- }
-
- });
-
- label.addKeyListener(tastatur);
- this.add(panel);
- }
-
- public static void main(String args[])
- {
- Main main = new Main();
- }
-
- public String getNumber()
- {
- return number;
- }
-
- public void refreshLabel()
- {
- label.setText(getNumber());
- }
-
- public void setDefault()
- {
- first = 0; second = 0; result = 0;
- number = "";
- bplus = false; bminus = false; btimes = false; bdivided = false;
- }
-
- public double cutDecimals(double a)
- {
- a = (double)Math.round(a * 100) / 100;
- return a;
- }
- }
Tastatur class
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
-
- public class Tastatur implements KeyListener
- {
- Main Main = new Main();
-
- public void keyPressed(KeyEvent e)
- {
- int keyTyped = e.getKeyCode();
-
- if(keyTyped == KeyEvent.VK_1)
- Main.number += "1";
- else if(keyTyped == KeyEvent.VK_2)
- Main.number += "2";
- else if(keyTyped == KeyEvent.VK_3)
- Main.number += "3";
- else if(keyTyped == KeyEvent.VK_4)
- Main.number += "4";
- else if(keyTyped == KeyEvent.VK_5)
- Main.number += "5";
- else if(keyTyped == KeyEvent.VK_6)
- Main.number += "6";
- else if(keyTyped == KeyEvent.VK_7)
- Main.number += "7";
- else if(keyTyped == KeyEvent.VK_8)
- Main.number += "8";
- else if(keyTyped == KeyEvent.VK_9)
- Main.number += "9";
- else if(keyTyped == KeyEvent.VK_0)
- Main.number += "0";
- else if(keyTyped == KeyEvent.VK_COMMA)
- Main.number += ",";
-
- Main.refreshLabel();
- }
-
- public void keyReleased(KeyEvent e)
- {
-
- }
-
- public void keyTyped(KeyEvent e)
- {
-
- }
-
- }
Indlæg senest redigeret d. 26.09.2012 19:54 af Bruger #16945