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

Ayuda Jframe y JButton

Estas en el tema de Ayuda Jframe y JButton en el foro de Java en Foros del Web. Buenas, tengo que hacer que al pulsar un boton se cree otro boton con un color asignado al azar entre 4, y al pulsar un ...
  #1 (permalink)  
Antiguo 06/11/2008, 13:04
 
Fecha de Ingreso: octubre-2008
Mensajes: 10
Antigüedad: 15 años, 6 meses
Puntos: 0
Exclamación Ayuda Jframe y JButton

Buenas, tengo que hacer que al pulsar un boton se cree otro boton con un color asignado al azar entre 4, y al pulsar un boton con color que cree un boton de ese mismo color, pero no consigo que funcione correctamente, por ejemplo al crear un boton tengo q pasar el raton por encima d los botones para verlos, y no se me ve la imagen del boton y tampoco se me ve el nombre entero del boton, por ejemplo en vez d amarillo se ve ama..., y tbn decir que no se como acer q se vean en varias filas, por ejemplo cuando creo el 5 boton que se vea en otra fila, es decir, por ejemplo 4 botones por fila, y creo q eso es todo q es bastante, ayuda please!! es la primera vez que toco esto

Código PHP:
package actividad1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author Miguel Angel
 */
public class Actividad1 extends JFrame{

    
FlowLayout experimentLayout = new FlowLayout();
    
LinkedList<JButtonlistaBotones = new LinkedList<JButton>();
    
JButton crearBotones = new JButton("Crear otro...");
    
    
/** Creates a new instance of actividad1 */
    
public Actividad1(String name) {
        
super(name);
    }
    
    public 
void addComponentsToPane(final Container pane) {
        final 
JPanel compsToExperiment = new JPanel();
        
compsToExperiment.setLayout(experimentLayout);
        
experimentLayout.setAlignment(FlowLayout.TRAILING);
        
        
//Add buttons to the experiment layout
        
compsToExperiment.add(crearBotones);
        
Iterator listaIt listaBotones.iterator();
        while(
listaIt.hasNext()){
            
JButton boton = (JButton)listaIt.next();
            final 
String nombre boton.getText();
            
compsToExperiment.add(boton);
            
boton.addActionListener(new ActionListener(){
                public 
void actionPerformed(ActionEvent e){
                    
crearBoton(nombre);
                    
//update the experiment layout
                    
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
//Set up the content pane.
                
addComponentsToPane(getContentPane());
                
//Display the window.
                
pack();
                
setVisible(true);
                
//update the experiment layout
                
compsToExperiment.validate();
                
compsToExperiment.repaint();
                }
            });
        }
        
//Left to right component orientation is selected by default
        
compsToExperiment.setComponentOrientation(
                
ComponentOrientation.LEFT_TO_RIGHT);
        
        
        
//Process the Apply component orientation button press
        
crearBotones.addActionListener(new ActionListener(){
            public 
void actionPerformed(ActionEvent e){
                
int numeroAzar = (int)(Math.random()*4);
                switch(
numeroAzar){
                    case 
0:
                        
crearBoton("Amarillo");
                        break;
                    case 
1:
                        
crearBoton("Azul");
                        break;
                    case 
2:
                        
crearBoton("Naranja");
                        break;
                    case 
3:
                        
crearBoton("Verde");
                        break;
                    default:
                        
crearBoton("Amarillo");
                }
                
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
//Set up the content pane.
                
addComponentsToPane(getContentPane());
                
//Display the window.
                
pack();
                
setVisible(true);
                
//update the experiment layout
                
compsToExperiment.validate();
                
compsToExperiment.repaint();
            }
        });
        
pane.add(compsToExperimentBorderLayout.CENTER);
    }
    
    
/**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    
private static void crearYMostrarGUI() {
        
//Create and set up the window.
        
Actividad1 frame = new Actividad1("Actividad1");
        
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
//Set up the content pane.
        
frame.addComponentsToPane(frame.getContentPane());
        
//Display the window.
        
frame.pack();
        
frame.setVisible(true);
    }
    
    public static 
void main(String[] args) {
        
/* Use an appropriate Look and Feel */
        
try {
            
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (
UnsupportedLookAndFeelException ex) {
            
ex.printStackTrace();
        } catch (
IllegalAccessException ex) {
            
ex.printStackTrace();
        } catch (
InstantiationException ex) {
            
ex.printStackTrace();
        } catch (
ClassNotFoundException ex) {
            
ex.printStackTrace();
        }
        
/* Turn off metal's use of bold fonts */
        
UIManager.put("swing.boldMetal"Boolean.FALSE);
        
//Schedule a job for the event dispatchi thread:
        //creating and showing this application's GUI.
        
javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public 
void run() {
                
crearYMostrarGUI();
            }
        });
    }
    
    public 
void crearBoton(String color){
        
ImageIcon imagen;
        if(
color.equals("Amarillo")){
            
imagen = new ImageIcon("botones/amarillo.gif");
        } else {
            if(
color.equals("Azul")){
                
imagen = new ImageIcon("botones/azul.jpg");
            } else {
                if(
color.equals("Naranja")){
                    
imagen = new ImageIcon("botones/naranja.jpg)");
                } else {
                    
imagen = new ImageIcon("botones/verde.jpg");
                }
            }
        }
        
JButton nuevoBoton = new JButton(colorimagen);
        
nuevoBoton.setHorizontalTextPosition(AbstractButton.RIGHT);
        
listaBotones.add(nuevoBoton);
    }

Tambien e visto un lado que poniendo esto se te actualiza pero no se la verdad
Código:
                SwingUtilities.updateComponentTreeUI(compsToExperiment); 
                compsToExperiment.validate();
  #2 (permalink)  
Antiguo 07/11/2008, 11:25
 
Fecha de Ingreso: noviembre-2008
Mensajes: 9
Antigüedad: 15 años, 6 meses
Puntos: 0
Respuesta: Ayuda Jframe y JButton

nuevoBoton.setVisible(true);
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 16:55.