I have some problems with the creation of a simple calculator, but when asking a question (like 2-1) the system returns 0.0 as the result. I am thinking that I am not using a good way to analise information. This is how I try to set the double 't' to the number inside the textbox.
insert = new JTextField();
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
i = insert.getText();
t = Double.parseDouble(i);
} finally {}
}; {}
});
insert.setBackground(new Color(135, 206, 235));
insert.setText("0");
insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
insert.setColumns(10);
insert.setBounds(0, 50, 292, 28);
frame.getContentPane().add(insert);
If the mistake is not there, this is the full code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.awt.event.InputMethodEvent;
public class Window {
double t, x, a, l, s;
String w, i;
Scanner dati = new Scanner(System. in );
private JFrame frame;
private JTextField result;
private JTextField insert;
private JTextField hint;
private JTextField score;
private final JButton help = new JButton("?");
private final JButton calculator = new JButton("=");
/**
* 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.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(135, 206, 235));
frame.getContentPane().setLayout(null);
calculator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (l < 6 && l > 0) {
if (l != 2 && l != 3 && l != 4 && l != 5); {
s = x + t;
}
if (l != 1 && l != 3 && l != 4 && l != 5); {
s = x - t;
}
if (l != 1 && l != 2 && l != 4 && l != 5); {
s = x * t;
}
if (l != 1 && l != 2 && l != 3 && l != 5); {
s = x / t;
}
if (l != 1 && l != 2 && l != 4 && l != 3); {
s = (x / 100) * t;
}
result.setText(Double.toString(s));
} else {
result.setText("Kļūda programmā :(");
}
t = 0;
x = 0;
l = 0;
a = 0;
}
});
calculator.setBounds(292, 57, 52, 20);
frame.getContentPane().add(calculator);
calculator.setBackground(new Color(135, 206, 235));
help.setBackground(new Color(135, 206, 235));
help.setBounds(292, 0, 52, 20);
frame.getContentPane().add(help);
score = new JTextField();
score.setEditable(false);
score.setText("\u0160eit rezult\u0101ts");
score.setColumns(10);
score.setBounds(344, 0, 100, 20);
frame.getContentPane().add(score);
hint = new JTextField();
hint.setEditable(false);
hint.setText("\u0160eit tavi skait\u013Ci");
hint.setBounds(344, 57, 100, 20);
frame.getContentPane().add(hint);
hint.setColumns(10);
insert = new JTextField();
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
i = insert.getText();
t = Double.parseDouble(i);
} finally {}
}; {}
});
insert.setBackground(new Color(135, 206, 235));
insert.setText("0");
insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
insert.setColumns(10);
insert.setBounds(0, 50, 292, 28);
frame.getContentPane().add(insert);
result = new JTextField();
result.setEditable(false);
result.setBackground(new Color(135, 206, 235));
result.setBounds(0, 0, 292, 28);
result.setText("Hei!");
result.setFont(new Font("Tahoma", Font.PLAIN, 18));
frame.getContentPane().add(result);
result.setColumns(10);
JButton add = new JButton("+");
add.setBackground(new Color(135, 206, 250));
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (a != 0) {
x = x + t;
t = 0;
l = 1;
} else {
x = t;
t = 0;
a = 1;
l = 1;
}
}
});
add.setBounds(0, 28, 89, 23);
frame.getContentPane().add(add);
JButton subtract = new JButton("-");
subtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x - t;
t = 0;
l = 2;
} else {
x = t;
t = 0;
a = 1;
l = 2;
}
}
});
subtract.setBackground(new Color(135, 206, 235));
subtract.setBounds(89, 28, 89, 23);
frame.getContentPane().add(subtract);
JButton multiply = new JButton("x");
multiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x * t;
t = 0;
l = 3;
} else {
x = t;
t = 0;
a = 1;
l = 3;
}
}
});
multiply.setBackground(new Color(135, 206, 235));
multiply.setForeground(new Color(0, 0, 0));
multiply.setBounds(178, 28, 89, 23);
frame.getContentPane().add(multiply);
JButton divide = new JButton("/");
divide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x / t;
t = 0;
l = 4;
} else {
x = t;
t = 0;
a = 1;
l = 4;
}
}
});
divide.setBackground(new Color(135, 206, 235));
divide.setBounds(266, 28, 89, 23);
frame.getContentPane().add(divide);
JButton percent = new JButton("%");
percent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = (x / 100) * t;
t = 0;
l = 5;
} else {
x = t;
t = 0;
a = 1;
l = 5;
}
}
});
percent.setBackground(new Color(135, 206, 235));
percent.setBounds(355, 28, 89, 23);
frame.getContentPane().add(percent);
frame.setBounds(100, 100, 460, 115);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dati.close();
}
}
Possibly I am using a bad way to calculate.
If you want a simple explanation, I am trying to create a calculator with five functions - +, -, *, /, % - and has one text box to insert the numbers you want to calculate - insert, click function button, insert, click function button, when done - the = button. '?' button is planned to give a description of the calculator. I am making it for a school project, by the way.
Thanks in advance.
P.S. Kļūda programmā translates to fault in program.