Java - Numero aleatorio en Jlabel

 
Vista:
Imágen de perfil de Bladimir Simoza

Numero aleatorio en Jlabel

Publicado por Bladimir Simoza (13 intervenciones) el 29/10/2011 17:37:02
Hola a todos. La pregunta es como puedo generar un numero aleatorio con un jButton y que este se exprese en un jLabel. Gracias de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Numero aleatorio en Jlabel

Publicado por Renzo (232 intervenciones) el 31/10/2011 17:20:22
Hola, esta es la solucion. Consta de dos archivos (el main y la ventana)


///////////el main //////////////////
public class Main {
public static void main(String[] args) {
JFrame jf=new NewJFrame();
jf.setVisible(true);
}
}
///////////el main //////////////////



///////////la ventana//////////////////

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("jLabel1");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(63, 63, 63)
.add(jButton1)
.add(79, 79, 79)
.add(jLabel1)
.addContainerGap(151, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(58, 58, 58)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1)
.add(jLabel1))
.addContainerGap(219, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int x=(int)(100*Math.random()+1);
jLabel1.setText("Random de 1 a 100 : "+x);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration

}
///////////la ventana//////////////////




Espero haberte ayudado.


Te invito a visitar mi web: http://www.imagineanddo.com
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar