2017-09-21 1 views
0

J'ai fait une simple calculatrice en utilisant java.le premier Jtextfield prend la première valeur.le second Jtextfield prend la deuxième valeur et le troisième Jtextfield montre la réponse calculée après avoir appuyé sur les boutons +, -, *,/sur GUI.comment écrire du texte dans différents JtextFields en utilisant les boutons gui. Comment changer le focus des boutons gui en différents JtextFields?

Je ne peux pas entrer les valeurs en utilisant les boutons de l'interface graphique dans plus de 1 JTextField.how pour obtenir les boutons de l'interface graphique pour entrer du texte dans les deux champs de texte ??

import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

    public class ui implements ActionListener { 
public JButton btn1; 
public JButton btn2; 
public JButton btn3; 
public JButton btn4; 
public JButton btn5; 
public JButton btn6; 
public JButton btn7; 
public JButton btn8; 
public JButton btn9; 
public JButton btn0; 
public JButton btnAdd; 
public JButton btnSub; 
public JButton btnMultiply; 
public JButton btnPoint; 
public JButton btnDivide; 
public JButton btnEquals; 
public JButton btnClear; 
public JButton btnExit; 
public static JTextField text; 
public static JTextField text2; 
public static JTextField text3; 


public static void ui() { 
    ui u1= new ui(); 

    JFrame f = new JFrame(); 
    f.setSize(300,500); 
    f.setTitle("my calculator"); 
    f.setResizable(false); 


    JPanel p = new JPanel(); 
    BoxLayout b = new BoxLayout(p,BoxLayout.Y_AXIS); 
    p.setLayout(b); 

    Font fo = new Font("ARIAL",Font.CENTER_BASELINE,30); 
    Font fo1 = new Font("ARIAL",Font.ITALIC,20); 

    u1.text = new JTextField(10); 
    u1.text.setSize(300,100); 
    u1.text.setText("0"); 

    u1.text.setFont(fo1); 
    u1.text.setBackground(Color.gray); 
    p.add(u1.text); 
    u1.text2 = new JTextField(10); 
    u1.text2.setSize(300,100); 
    u1.text2.setText("0"); 

    u1.text2.setFont(fo1); 
    u1.text2.setBackground(Color.gray); 
    p.add(u1.text2); 
    u1.text3 = new JTextField(10); 
    u1.text3.setSize(300,100); 
    u1.text3.setText("0"); 

    u1.text3.setFont(fo1); 
    u1.text3.setBackground(Color.gray); 
    p.add(u1.text3); 
    JPanel p1 = new JPanel(new GridLayout(6,3)); 
    u1.btn1 = new JButton("1"); 
    u1.btn1.setFont(fo); 
    p1.add(u1.btn1); 
    u1.btn1.addActionListener(u1); 

    u1.btn2 = new JButton("2"); 
    u1.btn2.setFont(fo); 
    p1.add(u1.btn2); 
    u1.btn2.addActionListener(u1); 

    u1.btn3 = new JButton("3"); 
    u1.btn3.setFont(fo); 
    p1.add(u1.btn3); 
    u1.btn3.addActionListener(u1); 

    u1.btn4 = new JButton("4"); 
    u1.btn4.setFont(fo); 
    p1.add(u1.btn4); 
    u1.btn4.addActionListener(u1); 

    u1.btn5 = new JButton("5"); 
    u1.btn5.setFont(fo); 
    p1.add(u1.btn5); 
    u1.btn5.addActionListener(u1); 

    u1.btn6 = new JButton("6"); 
    u1.btn6.setFont(fo); 
    p1.add(u1.btn6); 
    u1.btn6.addActionListener(u1); 

    u1.btn7 = new JButton("7"); 
    u1.btn7.setFont(fo); 
    p1.add(u1.btn7); 
    u1.btn7.addActionListener(u1); 

    u1.btn8 = new JButton("8"); 
    u1.btn8.setFont(fo); 
    p1.add(u1.btn8); 
    u1.btn8.addActionListener(u1); 

    u1.btn9 = new JButton("9"); 
    u1.btn9.setFont(fo); 
    p1.add(u1.btn9); 
    u1.btn9.addActionListener(u1); 



    u1.btnEquals = new JButton("="); 
    u1.btnEquals.setFont(fo); 
    u1.btnEquals.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnEquals); 
    u1.btnEquals.addActionListener(u1); 

    u1.btn0 = new JButton("0"); 
    u1.btn0.setFont(fo); 
    p1.add(u1.btn0); 
    u1.btn0.addActionListener(u1); 

    u1.btnPoint = new JButton("."); 
    u1.btnPoint.setFont(fo); 
    u1.btnPoint.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnPoint); 
    u1.btnPoint.addActionListener(u1); 

    u1.btnAdd = new JButton("+"); 
    u1.btnAdd.setFont(fo); 
    u1.btnAdd.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnAdd); 
    u1.btnAdd.addActionListener(u1); 


    u1.btnSub = new JButton("-"); 
    u1.btnSub.setFont(fo); 
    u1.btnSub.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnSub); 
    u1.btnSub.addActionListener(u1); 

    u1.btnMultiply = new JButton("*"); 
    u1.btnMultiply.setFont(fo); 
    u1.btnMultiply.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnMultiply); 
    u1.btnMultiply.addActionListener(u1); 

    u1.btnDivide = new JButton("/"); 
    u1.btnDivide.setFont(fo); 
    u1.btnDivide.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnDivide); 
    u1.btnDivide.addActionListener(u1); 

    u1.btnClear = new JButton("clear"); 
    u1.btnClear.setFont(fo); 
    u1.btnClear.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnClear); 
    u1.btnClear.addActionListener(u1); 

    u1.btnExit = new JButton("exit"); 
    u1.btnExit.setFont(fo); 
    u1.btnExit.setBackground(Color.getHSBColor(30,100,100)); 
    p1.add(u1.btnExit); 
    u1.btnExit.addActionListener(u1); 

    p.add(p1); 
    f.add(p); 

    f.setVisible(true); 


} 
    public void actionPerformed(ActionEvent e){ 
    if(e.getSource()==this.btnAdd){ 
     maths.add(); 

    } 
    else if (e.getSource()==this.btnSub){ 
     maths.sub(); 

    } 
    else if(e.getSource()==this.btnMultiply){ 
     maths.multiply(); 

    } 
    else if(e.getSource()==this.btnDivide){ 
     try { 
      maths.divide(); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 

    } 
    else if(e.getSource()==this.btnClear){ 
     text.setText("0"); 
     text2.setText("0"); 
     text3.setText("0");  } 
    else if(e.getSource()==this.btnExit){ 
     System.exit(0); 
    } 
    else if (e.getSource()==this.btn1){ 
     if(ui.text.hasFocus()==true){ 
      ui.text.setText("1"); 
     } 
     else{ 
      ui.text2.setText("1"); 
     } 
    } 

    } 





    } 

     public class maths { 
    public static void add(){ 
    float a=0,b=0,c=0; 
    a=Float.parseFloat(ui.text.getText()); 
    b=Float.parseFloat(ui.text2.getText()); 

    c=a+b; 
    ui.text3.setText(String.valueOf(c)); 
    } 
    public static void sub(){ 
    float a=0,b=0,c=0; 
    a=Float.parseFloat(ui.text.getText()); 
    b=Float.parseFloat(ui.text2.getText()); 
    c=a-b; 
    ui.text3.setText(String.valueOf(c)); 
    } 
    public static void multiply(){ 
    float a=0,b=0,c=0; 
    a=Float.parseFloat(ui.text.getText()); 
    b=Float.parseFloat(ui.text2.getText()); 
    c= a*b; 
    ui.text3.setText(String.valueOf(c)); 

    } 
    public static void divide()throws Exception{ 
    float a=0,b=0,c=0; 
    a=Float.parseFloat(ui.text.getText()); 
    b=Float.parseFloat(ui.text2.getText()); 
    c=a/b; 
    ui.text3.setText(String.valueOf(c)); 
    } 

    public class demo { 
    public static void main(String[] args) { 
    ui.ui(); 
    } 
    } 
+0

double possible de [Java Déterminer qui a textfield focus] (https://stackoverflow.com/questions/20409024/java-determine-which-textfield-has-focus) – Juliane

Répondre