J'ai utilisé ce code et j'ai un problème dans l'étape très basique de l'utilisation de l'opérateur. De plus, j'ai même des problèmes à prendre plus d'un chiffre. S'il vous plaît, ajoutez simplement les déclarations manquantes qui pourraient m'aider. Dans le code donné, je l'ai supprimé ces étapes qui ont créé des problèmes en fonction actionPerformed
comment utiliser les opérateurs tout en faisant calc en Java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class calculator1 implements ActionListener
{
private JFrame f;
private JButton a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
JTextField tf;
String msg="";
public calculator1()
{
f=new JFrame("Calculator");
f.setLayout(null);
a=new JButton("1");
a.setActionCommand("1");
a1=new JButton("2");
a1.setActionCommand("2");
a2=new JButton("3");
a2.setActionCommand("3");
a3=new JButton("4");
a3.setActionCommand("4");
a4=new JButton("5");
a4.setActionCommand("5");
a5=new JButton("6");
a5.setActionCommand("6");
a6=new JButton("7");
a6.setActionCommand("7");
a7=new JButton("8");
a7.setActionCommand("8");
a8=new JButton("9");
a8.setActionCommand("9");
a9=new JButton("0");
a9.setActionCommand("0");
a10=new JButton("+");
a10.setActionCommand("+");
a11=new JButton("-");
a11.setActionCommand("-");
a12=new JButton("*");
a12.setActionCommand("*");
a13=new JButton("/");
a13.setActionCommand("/");
a14=new JButton("=");
a14.setActionCommand("=");
a15=new JButton("00");
a15.setActionCommand("00");
tf= new JTextField(30);
}
public void launchframe()
{
f.setSize(500,600);
a.setBounds(100,200,50,50);
a.addActionListener(this);
a1.setBounds(160,200,50,50);
a1.addActionListener(this);
a2.setBounds(220,200,50,50);
a2.addActionListener(this);
a3.setBounds(100,300,50,50);
a3.addActionListener(this);
a4.setBounds(160,300,50,50);
a4.addActionListener(this);
a5.setBounds(220,300,50,50);
a5.addActionListener(this);
a6.setBounds(100,400,50,50);
a6.addActionListener(this);
a7.setBounds(160,400,50,50);
a7.addActionListener(this);
a8.setBounds(220,400,50,50);
a8.addActionListener(this);
a9.setBounds(100,500,50,50);
a9.addActionListener(this);
a10.setBounds(300,200,50,50);
a10.addActionListener(this);
a11.setBounds(300,300,50,50);
a11.addActionListener(this);
a12.setBounds(300,400,50,50);
a12.addActionListener(this);
a13.setBounds(300,500,50,50);
a13.addActionListener(this);
a14.setBounds(160,500,50,50);
a14.addActionListener(this);
a15.setBounds(220,500,50,50);
a15.addActionListener(this);
f.add(a);
f.add(a1);
f.add(a2);
f.add(a3);
f.add(a4);
f.add(a5);
f.add(a6);
f.add(a7);
f.add(a8);
f.add(a9);
f.add(a10);
f.add(a11);
f.add(a12);
f.add(a13);
f.add(a14);
f.add(a15);
tf.setBounds(100,150,250,30);
f.add(tf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
tf.setText(s);
}
public static void main(String[]arg)
{
calculator1 c1=new calculator1();
c1.launchframe();
}
}
Je sais que le code est juste le contour que j'ai enlevé ces codes un ont demandé ces "lignes spécifiques seulement" – tushar
Je ne vais pas écrire le code pour vous. Suivez simplement les étapes de mon plan et réfléchissez-y par vous-même. –
Je vais sûrement juste me justifier que pourquoi il n'y a pas de logique – tushar