"No importa cuan perfecto creas sea tu código siempre habrá alguien que le hallé un error"
En esta ocasión seguimos con la serie de ejercicios usando el lenguaje de programación
Groovy. Usaremos la librería
javax.swing para crear aplicaciones gráficas.
1. Generar un número aleatorio. Esta aplicación tendrá:
- Dos botones JButton (Generar no. aleatorio y Salir)
- Un JLabel que nos servirá de separador
Para generar el número aleatorio usaremos la clase Random.
Random rand=new Random()
aleatorio=(int)rand.nextInt(100)
/*aleatorio.groovy */
import java.awt.event.*
import java.awt.*
import javax.swing.JLabel
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JOptionPane
import javax.swing.*
frame=new JFrame("Groovy en ejemplos ... no.5\n::codemonkeyjunior 2015::")
caja=Box.createVerticalBox()
btnCalcular=new JButton("<html><font color='red'>Generar</font> <font color='blue'>no. aleatorio</font></html>")
btnCalcular.addActionListener({
Random rand=new Random()
try {
aleatorio=(int)rand.nextInt(100)
}
catch(Exception e) {
JOptionPane.showMessageDialog(panel, "No se pudo generar el numero", "Error", JOptionPane.ERROR_MESSAGE);
}finally{
JOptionPane.showMessageDialog(null,"Numero aleatorio: "+String.valueOf(aleatorio))
}
} as ActionListener)
btnSalida=new JButton("<html>Desea <font color='red'> salir</font>?</html>")
btnSalida.addActionListener({
int seleccion = JOptionPane.showConfirmDialog(null,
"Deseas cerrar la ventana?",
"Seleccion",
JOptionPane.YES_NO_OPTION);
if(seleccion==0){
System.exit(0)
}
}as ActionListener)
caja.add(btnCalcular)
caja.add(new JLabel("<html><br/><br/></html>"))
caja.add(btnSalida)
frame.contentPane.add(caja)
frame.setSize(450,150)
frame.visible=true
frame.defaultCloseOperation=JFrame.EXIT_ON_CLOSE
Ejecutamos
:
Al dar clic sobre el botón
"Generar no. aleatorio" saldrá este mensaje:
Al Dar clic sobre el botón
"Desea salir?" saldrá:
2.En este ejemplo usaremos:
El usuario elegirá entre tres opciones (Java, Python,y Groovy) y comprobará el resultado al dar clic al botón.
/*elegir.groovy*/
import javax.swing.*
import java.awt.event.*
import java.awt.*
def ventana(){
frame=new JFrame(title: '::JCheckButton en Groovy::', size: [320,200], layout: new FlowLayout(), defaultCloseOperation: javax.swing.WindowConstants.EXIT_ON_CLOSE)
//podemos aplicarles estilo a los botones
btnEjecutar=new JButton("<html><font color='blue'>Ejecutar</font></html>")
btnEjecutar.setBackground(Color.white)
btnEjecutar.setToolTipText("inicio...")
btnEjecutar.addActionListener({
miCheck1=new JCheckBox("<html><h2><font color='red'>Java</font></h2></html>")
miCheck2=new JCheckBox("<html><h2><font color='green'>Python</font></h2></html>")
miCheck3=new JCheckBox("<html><h2><font color='blue'>Groovy</font></h2></html>")
panel=new JFrame(title: '::JCheckButton en Groovy::', size: [320,200], layout: new FlowLayout())
miLabel=new JLabel("<html></html>")
botonMostrar=new JButton("<html>Comprobar</html>")
botonMostrar.addActionListener({
if(miCheck1.isSelected() && miCheck3.isSelected() && miCheck2.isSelected()){
miLabel.setText("elegiste los tres lenguajes")
}else if(miCheck1.isSelected() && miCheck3.isSelected()){
miLabel.setText("elegiste: Java y Groovy")
}else if(miCheck1.isSelected() && miCheck2.isSelected()){
miLabel.setText("elegiste: Java y Python")
}
else if(miCheck2.isSelected() && miCheck3.isSelected()){
miLabel.setText("elegiste: Python y Groovy")
}
else if(miCheck2.isSelected()){
miLabel.setText("elegiste: Python")
}
else if(miCheck3.isSelected()){
miLabel.setText("elegiste: Groovy")
}
else if(miCheck1.isSelected()){
miLabel.setText("elegiste: Java")
}
else{
miLabel.setText("")
}
} as ActionListener)
panel.add miCheck1
panel.add miCheck2
panel.add miCheck3
panel.add miLabel
panel.add botonMostrar
try{
panel.show()
}
catch(Exception ex){
println "error en: "+ex.toString()
}
}as ActionListener)
frame.add btnEjecutar
frame.show()
}
ventana() //invocamos el método
Ejecutamos:
Al dar clic saldrán las opciones:
Y comprobamos: