Witam,
Chciałbym prosić o pomoc w dodaniu działań, komend do mojego kalkulatora czyli dodawanie, odejmowanie, dzielenie, mnożenie i równa się. Bardzo prosiłbym o pomoc. Poniżej umieszczam swój dotychczasowy kod, który udało mi się stworzyć:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulator;
/**
*
* @author Łukasz
*/
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Kalkulator {
/**
* @param args the command line arguments
*/
static JTextField tf;
public static void main(String[] args)
{
JFrame frame = new JFrame("Kalkulator");
frame.setSize(250, 350);
frame.setLocation(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(null);
tf = new JTextField();
tf.setBounds(10, 10, 230, 50);
Font f = new Font("New Times Roman",Font.BOLD,24);
tf.setFont(f);
tf.setHorizontalAlignment(JTextField.RIGHT);
frame.add(tf);
JButton button1 = new JButton("1");
button1.setBounds(30, 80, 40, 40);
button1.setBorder(null);
frame.add(button1);
JButton button2 = new JButton("2");
button2.setBounds(100, 80, 40, 40);
button2.setBorder(null);
frame.add(button2);
JButton button3 = new JButton("3");
button3.setBounds(170, 80, 40, 40);
button3.setBorder(null);
frame.add(button3);
JButton button4 = new JButton("4");
button4.setBounds(30, 120, 40, 40);
button4.setBorder(null);
frame.add(button4);
JButton button5 = new JButton("5");
button5.setBounds(100, 120, 40, 40);
button5.setBorder(null);
frame.add(button5);
JButton button6 = new JButton("6");
button6.setBounds(170, 120, 40, 40);
button6.setBorder(null);
frame.add(button6);
JButton button7 = new JButton("7");
button7.setBounds(30, 160, 40, 40);
button7.setBorder(null);
frame.add(button7);
JButton button8 = new JButton("8");
button8.setBounds(100, 160, 40, 40);
button8.setBorder(null);
frame.add(button8);
JButton button9 = new JButton("9");
button9.setBounds(170, 160, 40, 40);
button9.setBorder(null);
frame.add(button9);
JButton button0 = new JButton("0");
button0.setBounds(30, 200, 40, 40);
button0.setBorder(null);
frame.add(button0);
JButton buttonplus = new JButton("+");
buttonplus.setBounds(100, 200, 40, 40);
buttonplus.setBorder(null);
frame.add(buttonplus);
JButton buttonminus = new JButton("-");
buttonminus.setBounds(170, 200, 40, 40);
buttonminus.setBorder(null);
frame.add(buttonminus);
JButton buttondzielenie = new JButton("/");
buttondzielenie.setBounds(30, 240, 40, 40);
buttondzielenie.setBorder(null);
frame.add(buttondzielenie);
JButton buttonmnozenie = new JButton("*");
buttonmnozenie.setBounds(100, 240, 40, 40);
buttonmnozenie.setBorder(null);
frame.add(buttonmnozenie);
JButton buttonrowna = new JButton("=");
buttonrowna.setBounds(170, 240, 40, 40);
buttonrowna.setBorder(null);
frame.add(buttonrowna);
button1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "1");
}
});
button2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "2");
}
});
button3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "3");
}
});
button4.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "4");
}
});
button5.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "5");
}
});
button6.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "6");
}
});
button7.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "7");
}
});
button8.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "8");
}
});
button9.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "9");
}
});
button0.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
tf.setText(tf.getText() + "0");
}
});
frame.setVisible(true);
}
}