织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

欧博abg¿como puedo hacer operar una calculadora

时间:2025-07-22 03:19来源: 作者:admin 点击: 0 次
Ya casi lo tienes, sólo te falta agregar código al actionPerformed y algunos detalles, aquí tu código modificado y funcionando (no es calculadora, sól

Ya casi lo tienes, sólo te falta agregar código al actionPerformed y algunos detalles, aquí tu código modificado y funcionando (no es calculadora, sólo imprime el valor correspondiente al botón)

import java.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public abstract class cal extends JFrame implements ActionListener{ private JPanel panel; public JButton boton, boton1, boton2, boton3,boton4, boton5, boton6, boton7, boton8, boton9,boton10,botonigual; private JTextField campoNumero1,campoNumero2,campoSuma; public JButton botonsuma; public JButton botonresta; public JButton botonmultiplicar; public JButton botondivision; public static void main(String[]args){ cal marco= new cal() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == this.boton1) System.out.println(1); else if (e.getSource() == this.boton2) System.out.println(2); else if (e.getSource() == this.boton3) System.out.println(3); else if (e.getSource() == this.boton4) System.out.println(4); else if (e.getSource() == this.boton5) System.out.println(5); else if (e.getSource() == this.boton6) System.out.println(6); else if (e.getSource() == this.boton7) System.out.println(7); else if (e.getSource() == this.boton8) System.out.println(8); else if (e.getSource() == this.boton9) System.out.println(9); else if (e.getSource() == this.boton10) System.out.println(10); else if (e.getSource() == this.botonsuma) System.out.println("+"); else if (e.getSource() == this.botonresta) System.out.println("-"); else if (e.getSource() == this.botonmultiplicar) System.out.println("*"); else if (e.getSource() == this.botondivision) System.out.println("/"); else System.out.println("="); } }; marco.setSize(180,220); marco.crearGUI(); marco.setVisible(true); } private void crearGUI(){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container ventana= getContentPane(); ventana.setLayout(new FlowLayout()); panel= new JPanel(); panel.setPreferredSize(new Dimension(120,20)); panel.setBackground(Color.white); ventana.add(panel); boton1= new JButton("1"); ventana.add(boton1); boton1.addActionListener(this); boton2=new JButton ("2"); ventana.add(boton2); boton2.addActionListener(this); boton3= new JButton ("3"); ventana.add (boton3); boton3.addActionListener(this); boton4= new JButton ("4"); ventana.add (boton4); boton4.addActionListener(this); boton5= new JButton ("5"); ventana.add (boton5); boton5.addActionListener(this); boton6= new JButton ("6"); ventana.add (boton6); boton6.addActionListener(this); boton7= new JButton ("7"); ventana.add (boton7); boton7.addActionListener(this); boton8= new JButton ("8"); ventana.add (boton8); boton8.addActionListener(this); boton9= new JButton ("9"); ventana.add (boton9); boton9.addActionListener(this); boton10= new JButton ("0"); ventana.add (boton10); boton10.addActionListener(this); botonsuma= new JButton ("+"); ventana.add (botonsuma); botonsuma.addActionListener(this); botonresta= new JButton ("-"); ventana.add (botonresta); botonresta.addActionListener(this); botonmultiplicar= new JButton ("*"); ventana.add (botonmultiplicar); botonmultiplicar.addActionListener(this); botondivision= new JButton ("/"); ventana.add (botondivision); botondivision.addActionListener(this); botonigual=new JButton("="); ventana.add(botonigual); botonigual.addActionListener(this); } }

Ahí está, como puedes ver tiene algunas modificaciones como e.getSource() que devuelve una referencia al botón que originó el evento y lo compara con this.boton y los botones son públicos esto es para que puedan hacer referencia correctamente a los mismos, pues eso se está sobreescribiendo desde un método estático (main).

Sin embargo, no es la única forma de hacerlo, acá te presento otras dos:

Sobreescribiendo el método actionPerformed afuera del main

import java.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class cal extends JFrame implements ActionListener{ private JPanel panel; private JButton boton, boton1, boton2, boton3,boton4, boton5, boton6, boton7, boton8, boton9,boton10,botonigual; private JTextField campoNumero1,campoNumero2,campoSuma; private JButton botonsuma; private JButton botonresta; private JButton botonmultiplicar; private JButton botondivision; public static void main(String[]args){ cal marco= new cal(); marco.setSize(180,220); marco.crearGUI(); marco.setVisible(true); } private void crearGUI(){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container ventana= getContentPane(); ventana.setLayout(new FlowLayout()); panel= new JPanel(); panel.setPreferredSize(new Dimension(120,20)); panel.setBackground(Color.white); ventana.add(panel); boton1= new JButton("1"); ventana.add(boton1); boton1.addActionListener(this); boton2=new JButton ("2"); ventana.add(boton2); boton2.addActionListener(this); boton3= new JButton ("3"); ventana.add (boton3); boton3.addActionListener(this); boton4= new JButton ("4"); ventana.add (boton4); boton4.addActionListener(this); boton5= new JButton ("5"); ventana.add (boton5); boton5.addActionListener(this); boton6= new JButton ("6"); ventana.add (boton6); boton6.addActionListener(this); boton7= new JButton ("7"); ventana.add (boton7); boton7.addActionListener(this); boton8= new JButton ("8"); ventana.add (boton8); boton8.addActionListener(this); boton9= new JButton ("9"); ventana.add (boton9); boton9.addActionListener(this); boton10= new JButton ("0"); ventana.add (boton10); boton10.addActionListener(this); botonsuma= new JButton ("+"); ventana.add (botonsuma); botonsuma.addActionListener(this); botonresta= new JButton ("-"); ventana.add (botonresta); botonresta.addActionListener(this); botonmultiplicar= new JButton ("*"); ventana.add (botonmultiplicar); botonmultiplicar.addActionListener(this); botondivision= new JButton ("/"); ventana.add (botondivision); botondivision.addActionListener(this); botonigual=new JButton("="); ventana.add(botonigual); botonigual.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == boton1) System.out.println("1"); else if (e.getSource() == boton2) System.out.println("2"); else if (e.getSource() == boton3) System.out.println("3"); else if (e.getSource() == boton4) System.out.println("4"); else if (e.getSource() == boton5) System.out.println("5"); else if (e.getSource() == boton6) System.out.println("6"); else if (e.getSource() == boton7) System.out.println("7"); else if (e.getSource() == boton8) System.out.println("8"); else if (e.getSource() == boton9) System.out.println("9"); else if (e.getSource() == boton10) System.out.println("10"); else if (e.getSource() == botonsuma) System.out.println("+"); else if (e.getSource() == botonresta) System.out.println("-"); else if (e.getSource() == botonmultiplicar) System.out.println("*"); else if (e.getSource() == botondivision) System.out.println("/"); else System.out.println("="); } }

Dará el mismo resultado, pero como ves los JButton ahora son privados, y no está el this.boton, sino solamente el botón correspondiente.

Añadiendo a cada botón un ActionListener diferente

import java.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class cal extends JFrame{ private JPanel panel; public JButton boton, boton1, boton2, boton3,boton4, boton5, boton6, boton7, boton8, boton9,boton10,botonigual; private JTextField campoNumero1,campoNumero2,campoSuma; public JButton botonsuma; public JButton botonresta; public JButton botonmultiplicar; public JButton botondivision; public static void main(String[]args){ cal marco= new cal(); marco.setSize(180,220); marco.crearGUI(); marco.setVisible(true); } private void crearGUI(){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container ventana= getContentPane(); ventana.setLayout(new FlowLayout()); panel= new JPanel(); panel.setPreferredSize(new Dimension(120,20)); panel.setBackground(Color.white); ventana.add(panel); boton1= new JButton("1"); ventana.add(boton1); boton1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println(1); } }); boton2=new JButton ("2"); ventana.add(boton2); //lo mismo pero usando lambda boton2.addActionListener(e -> { System.out.println(2); }); boton3= new JButton ("3"); ventana.add (boton3); boton3.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println(3); } }); boton4= new JButton ("4"); ventana.add (boton4); boton4.addActionListener(e -> { System.out.println(4); }); boton5= new JButton ("5"); ventana.add (boton5); boton5.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println(5); } }); boton6= new JButton ("6"); ventana.add (boton6); boton6.addActionListener(e -> { System.out.println(6); }); boton7= new JButton ("7"); ventana.add (boton7); boton7.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println(7); } }); boton8= new JButton ("8"); ventana.add (boton8); boton8.addActionListener(e -> { System.out.println(8); }); boton9= new JButton ("9"); ventana.add (boton9); boton9.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println(9); } }); boton10= new JButton ("0"); ventana.add (boton10); boton10.addActionListener(e -> { System.out.println(0); }); botonsuma= new JButton ("+"); ventana.add (botonsuma); botonsuma.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println("+"); } }); botonresta= new JButton ("-"); ventana.add (botonresta); botonresta.addActionListener(e -> { System.out.println("-"); }); botonmultiplicar= new JButton ("*"); ventana.add (botonmultiplicar); botonmultiplicar.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println("*"); } }); botondivision= new JButton ("/"); ventana.add (botondivision); botondivision.addActionListener(e -> { System.out.println("/"); }); botonigual=new JButton("="); ventana.add(botonigual); botonigual.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ System.out.println("="); } }); } }

Como puedes notar ahora cada addActionListener ya no recibe this como parámetro, sino un ActionListener con el método actionPerfomed sobreescrito, o una función lambda que es más corto el código, intercalé ambos para notar que realmente tienen el mismo resultado.

Con esto, solamente falta que en lugar de imprimir el valor se realicen operaciones con él, pero te lo dejo a ti, ya que no se trata de hacer todo el trabajo. Espero haberte ayudado.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-07-22 09:07 最后登录:2025-07-22 09:07
栏目列表
推荐内容