Botones en java
Publicado por Marcos Aguilar (1 intervención) el 23/02/2012 22:23:50
Buenas tardes, tengo una duda con java...
Estoy practicando, y tengo este codigo hecho:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ejemplin extends JFrame implements ActionListener{
private JButton boton, boton1, boton2;
private JPanel panel;
public static void main(String[]args){
ejemplin marco = new ejemplin();
marco.setSize (400, 300);
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 (300,200));
panel.setBackground(Color.white);
ventana.add(panel);
boton = new JButton ("Dibujar");
ventana.add (boton);
boton.addActionListener(this);
boton1= new JButton ("Suma");
ventana.add(boton1);
boton1.addActionListener(this);
boton2= new JButton ("Multimplicacion");
ventana.add(boton2);
boton2.addActionListener(this);
}
public void actionPerformed (ActionEvent event){
if (event.getSource() == boton);
Graphics papel = panel.getGraphics();
papel.drawOval(20,40,50,60);
if (event.getSource() == boton1);
int resultado;
int numero1;
int numero2;
String numero1string;
String numero2string;
numero1string = JOptionPane.showInputDialog("Teclea un numero cualquiera");
numero1 = Integer.parseInt(numero1string);
numero2string = JOptionPane.showInputDialog("Teclee otro numero");
numero2 = Integer.parseInt(numero2string);
resultado = numero1 + numero2;
JOptionPane.showMessageDialog(null," La suma de los numeros es: "+resultado);
if (event.getSource() == boton2);
int numero3;
int numero4;
int resultado2;
String numero3string;
String numero4string;
numero3string = JOptionPane.showInputDialog("Teclea un numero");
numero3 = Integer.parseInt(numero3string);
numero4string = JOptionPane.showInputDialog("Ingrese otro numero");
numero4 = Integer.parseInt(numero4string);
resultado2 = numero3 * numero4;
JOptionPane.showMessageDialog(null,"La multiplicacion de los numeros anteriores es:" +resultado2);
}
}
El problema es que cuando oprimo el boton de dibujar, se dibuja el circulo y me tira las ventanas que me piden los numeros para sumarse y para multiplicar, y si le clickeo por ejemplo, al de sumar, se dibuja, se suma y se multiplica... y yo lo que deseo es que hagan su trabajo por separado.
Les agradeceria mucho si me pudieran ayudar, ya que soy principiante y tengo esa duda.
Gracias.
Estoy practicando, y tengo este codigo hecho:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ejemplin extends JFrame implements ActionListener{
private JButton boton, boton1, boton2;
private JPanel panel;
public static void main(String[]args){
ejemplin marco = new ejemplin();
marco.setSize (400, 300);
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 (300,200));
panel.setBackground(Color.white);
ventana.add(panel);
boton = new JButton ("Dibujar");
ventana.add (boton);
boton.addActionListener(this);
boton1= new JButton ("Suma");
ventana.add(boton1);
boton1.addActionListener(this);
boton2= new JButton ("Multimplicacion");
ventana.add(boton2);
boton2.addActionListener(this);
}
public void actionPerformed (ActionEvent event){
if (event.getSource() == boton);
Graphics papel = panel.getGraphics();
papel.drawOval(20,40,50,60);
if (event.getSource() == boton1);
int resultado;
int numero1;
int numero2;
String numero1string;
String numero2string;
numero1string = JOptionPane.showInputDialog("Teclea un numero cualquiera");
numero1 = Integer.parseInt(numero1string);
numero2string = JOptionPane.showInputDialog("Teclee otro numero");
numero2 = Integer.parseInt(numero2string);
resultado = numero1 + numero2;
JOptionPane.showMessageDialog(null," La suma de los numeros es: "+resultado);
if (event.getSource() == boton2);
int numero3;
int numero4;
int resultado2;
String numero3string;
String numero4string;
numero3string = JOptionPane.showInputDialog("Teclea un numero");
numero3 = Integer.parseInt(numero3string);
numero4string = JOptionPane.showInputDialog("Ingrese otro numero");
numero4 = Integer.parseInt(numero4string);
resultado2 = numero3 * numero4;
JOptionPane.showMessageDialog(null,"La multiplicacion de los numeros anteriores es:" +resultado2);
}
}
El problema es que cuando oprimo el boton de dibujar, se dibuja el circulo y me tira las ventanas que me piden los numeros para sumarse y para multiplicar, y si le clickeo por ejemplo, al de sumar, se dibuja, se suma y se multiplica... y yo lo que deseo es que hagan su trabajo por separado.
Les agradeceria mucho si me pudieran ayudar, ya que soy principiante y tengo esa duda.
Gracias.
Valora esta pregunta


0