Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class prog1_ {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- okno nf=new okno();
- }
- }
- class okno extends JFrame
- {
- private JTextArea text;
- private double p1=0,p2=0;
- private int oper=0;
- private void btnclick(JButton btn)
- {
- String str=btn.getText();
- //JOptionPane.showMessageDialog(null, str);
- if (str =="EXIT")
- {
- System.exit(0);
- }
- else if (str=="c")
- {
- p1=0;p2=0;text.setText("");
- }
- else if(str=="+")
- {
- oper=1;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="-")
- {
- oper=2;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="*")
- {
- oper=3;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="/")
- {
- oper=4;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="=")
- {
- p2 = Double.parseDouble(text.getText());
- if(oper==1)
- {
- text.setText(""+(p1+p2));
- }
- else if(oper==2)
- {
- text.setText(""+(p1-p2));
- }
- else if(oper==3)
- {
- text.setText(""+(p1*p2));
- }
- else if(oper==4)
- {
- text.setText(""+(p1/p2));
- }
- }
- else
- {
- text.setText("" + text.getText() + str);
- }
- //JOptionPane.showMessageDialog(null, "Бутерброд");
- }
- public okno()
- {
- Container cont = getContentPane();
- JPanel pan = new JPanel();
- pan.setLayout(null);
- Font btnFont = new Font("serif",0,20);
- Font labFont = new Font("arial",1,30);
- Font textFont = new Font("arial",2,30);
- JButton[] btn = new JButton[17];
- for (int i=0;i<17;i++)
- {
- btn[i] = new JButton();
- btn[i].setSize(100,25);
- btn[i].setFont(btnFont);
- btn[i].setLocation(30,50 + 30*i);
- pan.add(btn[i]);
- btn[i].addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e){
- btnclick((JButton)e.getSource());
- }});
- }
- for(int i=0;i<10;i++)btn[i].setText(""+i);
- btn[10].setText("+");
- btn[11].setText("-");
- btn[12].setText("/");
- btn[13].setText("*");
- btn[14].setText("=");
- btn[15].setText("c");
- btn[16].setText("EXIT");
- JLabel lab = new JLabel("Resultat: ");
- lab.setFont(labFont);
- lab.setBounds(130,0,300,50);
- pan.add(lab);
- text = new JTextArea();
- text.setFont(textFont);
- text.setBounds(300,10,300,35);
- text.setForeground(new Color(0,0,100));
- text.setBackground(Color.WHITE);
- pan.add(text);
- cont.add(pan);
- setBounds(0,0,800,600);
- setTitle ("Calculator");
- setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement