如何在动作事件中将一个文本字段转换为3个字段

时间:2018-12-30 09:54:59

标签: java swing

我正在开发一种可以计算复数的计算器:当我单击复数运算符时,我希望它向我显示3个字段来在上面写数字,而不是一个字段。

示例:

(10+2i)+(6+3i)=16+5i

我制作了可以执行此操作的类,现在我想为我的界面创建一个动作事件,以将其从普通的计算器转换为具有3个文本字段的复杂计算器。

   package calculatrice;

   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import javax.swing.*;
   import java.math.*;
   import com.jgoodies.forms.factories.DefaultComponentFactory;
   import org.eclipse.wb.swing.FocusTraversalOnArray;
   import javax.swing.border.MatteBorder;

   public class Calculatrice01 {

    private JFrame frame;
    private JTextField textField;
    double number1,number2,result;
    String operation,answer; 
    private JFrame panel ;
    private JTextField field1;
    private JTextField field2;
    private JTextField field3;
    private JTextField field4;
    private JTextField field5; 

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calculatrice01 window = new Calculatrice01();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }


    public Calculatrice01() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(102, 102, 
          102));
        frame.setBackground(new Color(102, 102, 102));
        frame.setBounds(100, 100, 576, 286);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setTitle("Calculator Made By DO.Yasser");
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);                  

        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.RIGHT);
        textField.setText("");
        textField.setBounds(12, 12, 539, 32);
        textField.setBackground(Color.WHITE);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        textField.setFont(new Font("Liberation Mono",Font.BOLD,15));        

        JButton btnCE = new JButton("CE");
        btnCE.setBackground(new Color(255, 153, 0));
        btnCE.setBounds(466, 56, 80, 30);
        btnCE.setFont(new Font("Tahoma",Font.BOLD,15));
        frame.getContentPane().add(btnCE);
        btnCE.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setBounds(12, 12, 539, 32);
                textField.setText(null);                    
            }
        });

        JButton btnDiv = new JButton("÷");
        btnDiv.setBackground(new Color(255, 153, 0));
        btnDiv.setBounds(466, 98, 80, 30);
        btnDiv.setFont(new Font("Tahoma",Font.BOLD,15));
        frame.getContentPane().add(btnDiv);
        btnDiv.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                number1 =Double.parseDouble(textField.getText());
                textField.setText("");
                operation="/";
            }
        });

        JButton btnMul = new JButton("X");
        btnMul.setBackground(new Color(255, 153, 0));
        btnMul.setBounds(466, 137, 80, 30);
        btnMul.setFont(new Font("Tahoma",Font.BOLD,15));
        frame.getContentPane().add(btnMul);
        btnMul.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                number1=Double.parseDouble(textField.getText());
                textField.setText("");
                operation="x";
            }
        });

        JButton btnSub = new JButton("-");
        btnSub.setFont(new Font("Tahoma",Font.BOLD,15));
        btnSub.setBackground(new Color(255, 153, 0));
        btnSub.setBounds(466, 174, 80, 30);
        frame.getContentPane().add(btnSub);
        btnSub.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                number1=Double.parseDouble(textField.getText());
                textField.setText("");
                operation="-";
            }
        });

        JButton btnPlus = new JButton("+");
        btnPlus.setBackground(new Color(255, 153, 0));
        btnPlus.setFont(new Font("Tahoma",Font.BOLD,15));
        btnPlus.setBounds(466, 216, 80, 30);
        frame.getContentPane().add(btnPlus);
        btnPlus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                number1=Double.parseDouble(textField.getText());
                textField.setText("");
                operation="+";
            }
        });

        JButton btnSqrt = new JButton("√");
        btnSqrt.setBackground(new Color(255, 153, 0));
        btnSqrt.setFont(new Font("Tahoma",Font.BOLD,15));
        btnSqrt.setBounds(286, 56, 80, 30);
        frame.getContentPane().add(btnSqrt);
        btnSqrt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                number1 = 
       Double.parseDouble(String.valueOf(textField.getText()));
                number1 = Math.sqrt(number1);
                textField.setText(String.valueOf(number1));
            }
        });

        JButton btn8 = new JButton("8");
        btn8.setFont(new Font("Tahoma",Font.BOLD,15));
        btn8.setBounds(286, 98, 80, 30);
        frame.getContentPane().add(btn8);
        btn8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn8.getText();
                textField.setText(num);
            }
        });

        JButton btn5 = new JButton("5");
        btn5.setFont(new Font("Tahoma",Font.BOLD,15));
        btn5.setBounds(286, 137, 80, 30);
        frame.getContentPane().add(btn5);
        btn5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn5.getText();
                textField.setText(num);
            }
        });

        JButton btn2 = new JButton("2");
        btn2.setFont(new Font("Tahoma",Font.BOLD,15));
        btn2.setBounds(286, 174, 80, 30);
        frame.getContentPane().add(btn2);
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn2.getText();
                textField.setText(num);
            }
        });

        JButton btnPoint = new JButton(".");
        btnPoint.setFont(new Font("Tahoma",Font.BOLD,15));
        btnPoint.setBounds(286, 216, 80, 30);
        frame.getContentPane().add(btnPoint);
        btnPoint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(!textField.getText().contains(".")) {
                    String num=textField.getText()+btnPoint.getText();
                    textField.setText(num);
                }
            }
        });

        JButton btnPow = new JButton("X^y");
        btnPow.setFont(new Font("Tahoma",Font.BOLD,15));
        btnPow.setBackground(new Color(255, 153, 0));
        btnPow.setBounds(100, 100, 80, 30);
        frame.getContentPane().add(btnPow);
        btnPow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                number1=Double.parseDouble(textField.getText());
                textField.setText("");
                operation="x^y";
            }
        });

        JButton btn9 = new JButton("9");
        btn9.setFont(new Font("Tahoma",Font.BOLD,15));
        btn9.setBounds(378, 98, 80, 30);
        frame.getContentPane().add(btn9);
        btn9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String num=textField.getText()+btn9.getText();
                textField.setText(num);
            }
        });

        JButton btn6 = new JButton("6");
        btn6.setFont(new Font("Tahoma",Font.BOLD,15));
        btn6.setBounds(378, 137, 80, 30);
        frame.getContentPane().add(btn6);
        btn6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn6.getText();
                textField.setText(num);
            }
        });

        JButton btn3 = new JButton("3");
        btn3.setFont(new Font("Tahoma",Font.BOLD,15));
        btn3.setBounds(378, 174, 80, 30);
        frame.getContentPane().add(btn3);
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn3.getText();
                textField.setText(num);
            }
        });

        JButton btnEq = new JButton("=");
        btnEq.setFont(new Font("Tahoma",Font.BOLD,15));
        btnEq.setForeground(Color.WHITE);
        btnEq.setBackground(new Color(51, 102, 204));
        btnEq.setBounds(378, 216, 80, 30);
        frame.getContentPane().add(btnEq);
        btnEq.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                String answer;
                number2=Double.parseDouble(textField.getText());
                if(operation=="+") {
                    number1+=number2;
                    textField.setText(String.valueOf(number1));
                }
                else if (operation =="-") {
                    number1-=number2;
                    textField.setText(String.valueOf(number1));
                }
                else if (operation =="x") {
                    number1*=number2;
                    textField.setText(String.valueOf(number1));
                }
                else if (operation =="/") {
                    number1=number1/number2;
                    textField.setText(String.valueOf(number1));
                }
                else if (operation =="x^y") {
                    number1=Math.pow(number1, number2);
                    textField.setText(String.valueOf(number1));
                }                                   
            }

        });

        JButton btnPI = new JButton("π");
        btnPI.setFont(new Font("Tahoma",Font.BOLD,15));
        btnPI.setBackground(new Color(255, 153, 0));
        btnPI.setBounds(194, 56, 80, 30);
        frame.getContentPane().add(btnPI);
        btnPI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText(String.valueOf(Math.PI));
            }
        });

        JButton btn7 = new JButton("7");
        btn7.setFont(new Font("Tahoma",Font.BOLD,15));
        btn7.setBounds(194, 98, 80, 30);
        frame.getContentPane().add(btn7);
        btn7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn7.getText();
                textField.setText(num);
            }
        });

        JButton btn4 = new JButton("4");
        btn4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn4.getText();
                textField.setText(num);
            }
        });
        btn4.setFont(new Font("Tahoma",Font.BOLD,15));
        btn4.setBounds(194, 137, 80, 30);
        frame.getContentPane().add(btn4);


        JButton btn1 = new JButton("1");
        btn1.setFont(new Font("Tahoma",Font.BOLD,15));
        btn1.setBounds(194, 174, 80, 30);
        frame.getContentPane().add(btn1);
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn1.getText();
                textField.setText(num);
            }
        });

        JButton btn0 = new JButton("0");
        btn0.setFont(new Font("Tahoma",Font.BOLD,15));
        btn0.setBounds(194, 216, 80, 30);
        frame.getContentPane().add(btn0);
        btn0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText()+btn0.getText();
                textField.setText(num);
            }
        });

        JButton btnFact = new JButton("X!");
        btnFact.setFont(new Font("Tahoma",Font.BOLD,15));
        btnFact.setBackground(new Color(255, 153, 0));
        btnFact.setBounds(100, 58, 80, 30);
        frame.getContentPane().add(btnFact);
        btnFact.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int fact = 
          Integer.parseInt(String.valueOf(textField.getText()));
                number1=fact;
                for(int i=1;i<fact;i++) {
                    number1*=i;
                }
                textField.setText(String.valueOf(number1));

            }
        });

        JButton btnSin = new JButton("Sin");
        btnSin.setFont(new Font("Tahoma",Font.BOLD,15));
        btnSin.setBackground(new Color(255, 153, 0));
        btnSin.setBounds(100, 216, 80, 30);
        frame.getContentPane().add(btnSin);


        JButton btnCos = new JButton("Cos");
        btnCos.setFont(new Font("Tahoma",Font.BOLD,15));
        btnCos.setBackground(new Color(255, 153, 0));
        btnCos.setBounds(100, 139, 80, 30);
        frame.getContentPane().add(btnCos);

        JButton btnTan = new JButton("Tan");
        btnTan.setFont(new Font("Tahoma",Font.BOLD,15));
        btnTan.setBackground(new Color(255, 153, 0));
        btnTan.setBounds(100, 176, 80, 30);
        frame.getContentPane().add(btnTan);

        JButton btnExp = new JButton("EXP");
        btnExp.setFont(new Font("Tahoma",Font.BOLD,15));
        btnExp.setBackground(new Color(255, 153, 0));
        btnExp.setBounds(12, 139, 80, 30);
        frame.getContentPane().add(btnExp);
        btnExp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String num=textField.getText();
                if (num=="") {
                    textField.setText(String.valueOf(Math.E));
                }else {
                number1 = 
            Double.parseDouble(String.valueOf(textField.getText()));
                number1 = Math.exp(number1);
                textField.setText(String.valueOf(number1));
                }


            }
        });

        JButton btnImg = new JButton("i");
        btnImg.setFont(new Font("Tahoma",Font.BOLD,15));
        btnImg.setBackground(new Color(255, 153, 0));
        btnImg.setBounds(12, 58, 80, 30);
        frame.getContentPane().add(btnImg);
        btnImg.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                //frame.setPreferredSize(new Dimension(650, 286));
                frame.setVisible(false);
                //panel.setVisible(true);
                textField.setBounds(12, 12, 50, 32);
                frame.getContentPane().add( field1);                    
            }
        });

        JButton btnPM = new JButton("±");
        btnPM.setFont(new Font("Tahom",Font.BOLD,15));
        btnPM.setBackground(new Color(255, 153, 0));
        btnPM.setBounds(12, 100, 80, 30);
        frame.getContentPane().add(btnPM);
        btnPM.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent arg0) {
                double num = 
           Double.parseDouble(String.valueOf(textField.getText()));
                num*=(-1);
                textField.setText(String.valueOf(num));

            }
        });

        JButton btnDel = new JButton("⌫");
        btnDel.setFont(new Font("Tahoma",Font.BOLD,15));
        btnDel.setBackground(new Color(255, 153, 0));
        btnDel.setBounds(378,56,80,30);
        frame.getContentPane().add(btnDel);
        btnDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String del = null;
                if(textField.getText().length()>0) {
                    StringBuilder str = new 
             StringBuilder(textField.getText());
                    str.deleteCharAt(textField.getText().length()-1);
                    del =str.toString();
                    textField.setText(del);
                }
            }
        });
        JToggleButton btnDeg = new JToggleButton("Deg");
        btnDeg.setBackground(Color.RED);
        btnDeg.setBounds(12, 178, 80, 30);;
        frame.getContentPane().add(btnDeg);


        JToggleButton btnRad = new JToggleButton("Rad");
        btnRad.setBackground(Color.RED);
        btnRad.setBounds(12, 217, 80, 30);;
        frame.getContentPane().add(btnRad);


        btnSin.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {

      number1=Double.parseDouble(String.valueOf(textField.getText()));
                if(btnDeg.isSelected()==true) {
                    btnRad.setSelected(false);

                    number1=Math.toRadians(number1);
                    double res = Math.sin(number1);

                    textField.setText(String.valueOf(res));
                }else {
                    btnRad.setSelected(true);

                    number1=Math.sin(number1);//Resultat en radian

                    textField.setText(String.valueOf(number1));                     
                }                   
            }
        });
        btnCos.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {

      number1=Double.parseDouble(String.valueOf(textField.getText()));
                if(btnDeg.isSelected()==true) {
                    btnRad.setSelected(false);
                    number1=Math.toRadians(number1);
                    double res = Math.cos(number1);

                    textField.setText(String.valueOf(res));
                }else {
                    btnRad.setSelected(true);
                    number1=Math.cos(number1);//Resultat en radian

                    textField.setText(String.valueOf(number1));                     
                }                   
            }
        });
        btnTan.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {

      number1=Double.parseDouble(String.valueOf(textField.getText()));
                if(btnDeg.isSelected()==true) {
                    btnRad.setSelected(false);
                    number1=Math.toRadians(number1);
                    double res = Math.tan(number1);

                    textField.setText(String.valueOf(res));
                }else {
                    btnRad.setSelected(true);

         number1=Math.tan(number1);//Resultat en radian


          textField.setText(String.valueOf(number1));

                }                   
            }
        });                 
    }
}

1 个答案:

答案 0 :(得分:0)

添加所有字段,并隐藏仅用于复杂计算的字段,即

field1.setVisible(false);

使用JButton,JCheckbox或JRadioButton让用户指出他/她想要执行复杂的计算,然后使其他字段可见。