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<JButton> listaBotones = 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(compsToExperiment, BorderLayout.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(color, imagen);
nuevoBoton.setHorizontalTextPosition(AbstractButton.RIGHT);
listaBotones.add(nuevoBoton);
}
}
Código:
SwingUtilities.updateComponentTreeUI(compsToExperiment); compsToExperiment.validate();