gui calculator 10-23-2016, 08:44 PM
#1
I've recently started learning GUI and decided to practice that's why i've decided to start with a simple calculator .
I'm working now on a better version the look is done i still have some calculation code to work on i'll post it when i'm done.
Code:
package calgui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calc extends JFrame implements ActionListener {
private JFrame f1;
private JTextArea text1;
private JTextArea text2;
private JTextArea text3;
private JRadioButton sum, minus, division, multiply;
private JButton calculate, clear;
private JPanel pan1, pan2, pan3, pan4, pan5, pan6;
public Calc() {
pan1 = new JPanel();
pan2 = new JPanel();
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();
pan6 = new JPanel();
text1 = new JTextArea(1, 12);
text2 = new JTextArea(1, 12);
text3 = new JTextArea(3, 12);
sum = new JRadioButton("Sum");
minus = new JRadioButton("Minus");
division = new JRadioButton("division");
multiply = new JRadioButton("multiply");
calculate = new JButton("calculate");
clear = new JButton("clear");
ButtonGroup group = new ButtonGroup();
group.add(sum);
group.add(division);
group.add(minus);
group.add(multiply);
pan1.setLayout(new BorderLayout());
pan2.setLayout(new GridLayout(1, 1, 10, 0));
pan3.setLayout(new GridLayout(1, 1, 0, 10));
pan4.setLayout(new GridLayout(1, 1, 0, 10));
pan6.setLayout(new GridLayout(1, 2));
setLayout(new BorderLayout());
pan1.add(pan2, BorderLayout.NORTH);
pan1.add(pan3, BorderLayout.WEST);
pan1.add(pan4, BorderLayout.EAST);
pan2.add(text1);
pan2.add(text2);
pan3.add(sum);
pan3.add(minus);
pan4.add(division);
pan4.add(multiply);
pan5.add(text3);
pan6.add(calculate);
pan6.add(clear);
add(pan1, BorderLayout.NORTH);
add(pan5, BorderLayout.CENTER);
add(pan6, BorderLayout.SOUTH);
calculate.addActionListener(this);
clear.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(435, 150);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int i;
if (source == calculate) {
if (sum.isSelected()) {
i = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());
text3.setText(Integer.toString(i));
}
if (minus.isSelected()) {
i = Integer.parseInt(text1.getText()) - Integer.parseInt(text2.getText());
text3.setText(Integer.toString(i));
}
if (division.isSelected()) {
i = Integer.parseInt(text1.getText()) / Integer.parseInt(text2.getText());
text3.setText(Integer.toString(i));
}
if (multiply.isSelected()) {
i = Integer.parseInt(text1.getText()) * Integer.parseInt(text2.getText());
text3.setText(Integer.toString(i));
}
} else {
text1.setText(null);
text2.setText(null);
text3.setText(null);
}
}
}
Code:
package calgui;
public class CalGui {
public static void main(String[] args) {
new Calc();
}
}
I'm working now on a better version the look is done i still have some calculation code to work on i'll post it when i'm done.