



Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!




Realizar programa que muestra 3 botones (al menos) distribuidos de manera aleatoria en una ventana, los botones deberán estar rotulados "Touch", "Presiona" y "Salir" (o algo parecido). Cuando el mouse se ubique encima de uno de ellos, el botón automáticamente deberá de reposicionarse aleatoriamente en la ventana, teniendo cuidado de no salirse de área visual. Cree un solo escucha para todos los botones utilizados.
Clase package CrazyButton; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class IguCrazyButton { private JFrame ventana; private JPanel panel; private JButton bot1, bot2, bot3; private int b1n1, b1n2, b2n1, b2n2, b3n1, b3n2; //constructor inicializa los componentes public IguCrazyButton() { ventana = new JFrame(); panel = new JPanel(); bot1 = new JButton(); bot2 = new JButton(); bot3 = new JButton();
//Lanzar la interfaz public void lanzar_IGU(){ ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ventana.setLocationRelativeTo(null); ventana.setVisible(true); }//cierre de public void Lanzar_IGU //Escucha(evento) para los botones class Esc_bot extends MouseAdapter{ @Override public void mouseEntered(MouseEvent e) { if (e.getSource()==bot1) { b1n1 = (int) (Math.random()300); b1n2 = (int) (Math.random()300); bot1.setText("bot1"); bot1.setBounds(b1n1, b2n2, 60, 15); }//cierre if bot if (e.getSource()==bot2) { b2n1 = (int) (Math.random()300); b2n2 = (int) (Math.random()300); bot2.setText("bot2"); bot2.setBounds(b2n1, b2n2, 60, 15); }//cierre if bot
if (e.getSource()==bot3) { b3n1 = (int) (Math.random()300); b3n2 = (int) (Math.random()300); bot3.setText("bot3"); bot3.setBounds(b3n1, b3n2, 60, 15); }//cierre if bot }//cierre de public void MouseEntered } //cierre de class Esc_bot extends MouseAdapter }//cierre de public class IguCrazyButton Clase Main package CrazyButton; public class PruebaIguCrazyButton { public static void main(String[] args) { IguCrazyButton ven = new IguCrazyButton(); } }