Foros del Web » Programación para mayores de 30 ;) » Java »

problema desconocido

Estas en el tema de problema desconocido en el foro de Java en Foros del Web. Porque no me funciona?. No lo entiendo, en teoria esta correcto, tendria que ir. Hos pongo el codigo de las dos class: La primera: import ...
  #1 (permalink)  
Antiguo 09/08/2010, 07:45
 
Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 15 años, 4 meses
Puntos: 0
problema desconocido

Porque no me funciona?.
No lo entiendo, en teoria esta correcto, tendria que ir.
Hos pongo el codigo de las dos class:

La primera:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.*;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;


public class felicidades extends JFrame implements ActionListener, ItemListener{
private JButton jbtn_boton;
private JLabel jlbl_label;
private JLabel jlbl2_label;
private JTextField text;

public felicidades () {
super("Felicidades");
setLayout(new FlowLayout());
this.definirVentana();
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = GridBagConstraints();

combo = new JComboBox(archivos);
label = new JLabel();
label.setIcon(iconos[0]);


gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = gbc.BOTH;
this.getContentPane().add(label,gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = gbc.BOTH;
this.getContentPane().add(combo,gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = gbc.BOTH;
this.getContentPane().add(jbtn_boton,gbc);
combo.addItemListener(this);
combo.addActionListener(this);
this.setVisible(true);

}






private GridBagConstraints GridBagConstraints() {
return null;
}






public void definirVentana() {
this.setLayout(new GridBagLayout());
jbtn_boton = new JButton("Sorpresa");
jlbl_label = new JLabel();
jlbl2_label = new JLabel();
this.add(jbtn_boton);
this.add(jlbl_label);
this.add(jlbl2_label);
jbtn_boton.addActionListener(this); }
private JComboBox combo;
private JLabel label;
private String[] archivos = {"1.JPG","2.JPG","3.JPG","4.JPG","5.JPG","6.JPG"," 7.JPG","8.JPG"};
private Icon[] iconos = {new ImageIcon(getClass().getResource(archivos[0])),new ImageIcon(getClass().getResource(archivos[1])),new ImageIcon(getClass().getResource(archivos[2])),new ImageIcon(getClass().getResource(archivos[3])),new ImageIcon(getClass().getResource(archivos[4])),new ImageIcon(getClass().getResource(archivos[5])),new ImageIcon(getClass().getResource(archivos[6])),new ImageIcon(getClass().getResource(archivos[7]))};






public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbtn_boton);
int text1 = ((int)(Math.random()*8));
label.setIcon(iconos[text1]);}




public void itemStateChanged1(ItemEvent e) {
label.setIcon(iconos[combo.getSelectedIndex()]);
}







public void itemStateChanged(ItemEvent e) {


}


}






Y la segunda:

public class felicidadesventana {
public static void main(String[] args){
felicidades vc = new felicidades();

}
}




Gracias.
  #2 (permalink)  
Antiguo 09/08/2010, 10:49
 
Fecha de Ingreso: noviembre-2006
Mensajes: 56
Antigüedad: 17 años, 5 meses
Puntos: 1
Respuesta: problema desconocido

Hola, creo que te faltan dos cosas:
- inicializar la variable gbc, olvidaste el new: GridBagConstraints gbc = new GridBagConstraints();
- y en el main, llamar al método setVisible para mostrar el JFrame: vc.setVisible(true);

Un Saludo.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:22.