Also im pretty sure ill run into troubles on how i can make the calculator understand Operator Precedence.
I know i could have made an num[i] array to create the number buttons and assign a listener etc, but i didnt at that time, so ..
English is not my native language but im doing my best
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Reiknari extends JPanel
{
JButton t1, t2, t3, t4, t5, t6, t7,t8,t9,t0;
JButton plus, minus, margf, deiling, plusminus, komma, hreinsa, samasem;
JTextField slattur;
JLabel empty;
public Reiknari()
{
ButtonListener listener = new ButtonListener();
t1 = new JButton ("1");
t1.addActionListener(listener);
t2 = new JButton ("2");
t2.addActionListener(listener);
t3 = new JButton ("3");
t3.addActionListener(listener);
t4 = new JButton ("4");
t4.addActionListener(listener);
t5 = new JButton ("5");
t5.addActionListener(listener);
t6 = new JButton ("6");
t6.addActionListener(listener);
t7 = new JButton ("7");
t7.addActionListener(listener);
t8 = new JButton ("8");
t8.addActionListener(listener);
t9 = new JButton ("9");
t9.addActionListener(listener);
t0 = new JButton ("0");
t0.addActionListener(listener);
plus = new JButton ("+");
plus.addActionListener(listener);
minus = new JButton ("-");
minus.addActionListener(listener);
margf = new JButton ("*");
margf.addActionListener(listener);
deiling = new JButton ("/");
deiling.addActionListener(listener);
hreinsa = new JButton ("C");
hreinsa.addActionListener(listener);
samasem = new JButton ("=");
samasem.addActionListener(listener);
slattur = new JTextField ("");
empty = new JLabel ("");
plusminus = new JButton ("+/-");
plusminus.addActionListener(listener);
komma = new JButton (",");
komma.addActionListener(listener);
// Plus, minus, devide, multiply
JPanel skipanir = new JPanel();
skipanir.setLayout (new GridLayout(5, 1));
skipanir.add(plus);
skipanir.add(minus);
skipanir.add(margf);
skipanir.add(deiling);
skipanir.add(samasem);
// numbers, comma, clear and an empty space.
JPanel tolur = new JPanel();
tolur.setLayout (new GridLayout(5, 3));
tolur.add(t7);
tolur.add(t8);
tolur.add(t9);
tolur.add(t4);
tolur.add(t5);
tolur.add(t6);
tolur.add(t1);
tolur.add(t2);
tolur.add(t3);
tolur.add(t0);
tolur.add(plusminus);
tolur.add(komma);
tolur.add(empty);
tolur.add(hreinsa);
JPanel look = new JPanel();
look.setLayout (new BorderLayout());
look.add(tolur, BorderLayout.CENTER);
look.add(skipanir, BorderLayout.EAST);
look.add(slattur, BorderLayout.NORTH);
setPreferredSize (new Dimension(400, 400));
setBackground(Color.green);
add (look);
}
public class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
// Here's mostly my problem ... was trying to use switch sentence, didnt work.
// I know this line down here is nowhere close i guess.
slattur.setText(event.getString());
}
}
}
Thanks in Advance! =)
------------
EDIT:
I know this is possible with tons of if statements, but im also sure there is a much better way. A smarter solution
This post has been edited by hallizh: 10 March 2008 - 08:27 AM

New Topic/Question
Reply




MultiQuote



|