
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class gui
{
public static void main(String[] args)
{
new calc();
}
}
class calc extends JFrame{
private JPanel leftPanel;
private JPanel CenterPanel;
private JPanel RightPanel;
private JTextField input1;
private JTextField input2;
private JLabel answer;
private JButton plius;
private JButton minus;
private JButton gamr;
private JButton gako;
public calc(){ // constructor
JFrame Frame = new JFrame("Vakhtang Koroghlishvili");
Frame.setSize(400, 200);
Frame.setLocation(300,300);
Frame.setVisible(true);
Container contentPane =Frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JLabel("Calculator In Java", JLabel.CENTER), BorderLayout.NORTH);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(3,1));
leftPanel.add(new JLabel(" A ="));
leftPanel.add(new JLabel(" B ="));
leftPanel.add(new JLabel(" "));
contentPane.add(leftPanel, BorderLayout.WEST);
JPanel CenterPanel= new JPanel();
CenterPanel.setLayout(new GridLayout(3,1));
[COLOR="#B22222"][B] final JTextField input1=new JTextField();
final JTextField input2=new JTextField();
final JLabel answer = new JLabel();[/B][/COLOR]
CenterPanel.add(input1);
CenterPanel.add(input2);
CenterPanel.add(answer);
contentPane.add(CenterPanel, BorderLayout.CENTER);
JPanel RightPanel=new JPanel();
RightPanel.setLayout(new GridLayout(4,1));
JButton plius = new JButton("+");
JButton minus = new JButton("-");
JButton gamr = new JButton("*");
JButton gako = new JButton("/");
RightPanel.add(plius);
RightPanel.add(minus);
RightPanel.add(gamr);
RightPanel.add(gako);
contentPane.add(RightPanel, BorderLayout.EAST);
ActionListener l = new ActionListener(){
public void actionPerformed(ActionEvent e) {
double d1=Double.parseDouble(input1.getText());
double d2=Double.parseDouble(input1.getText());
double d3=d1+d2;
answer.setText("A+b="+d3);
}
};
plius.addActionListener(l);
ActionListener l2 = new ActionListener(){
public void actionPerformed(ActionEvent e) {
double d1=Double.parseDouble(input1.getText());
double d2=Double.parseDouble(input1.getText());
double d3=d1-d2;
answer.setText("A-b="+d3);
}
};
minus.addActionListener(l2);
}
}
I have some question:
1) I'm writing programs in Eclipse Java. Eclipse tells me that:
JTextField input1, and JLabel answer, must be final. I don't understand, why?
2) Sometimes the result is not correct, for example when I write 5+6, the program tells me that it is 10.0.
I don't understand why?


Sign In
Create Account



Back to top









