¡Urgente! Tarea de práctica de diseño de programación, ¡los expertos pueden ayudar!
Espero que esto pueda resultarle útil
import javax.swing.*;
import java.awt.*; java.awt.event.*;
la clase pública CardLayoutDemo extiende JFrame implementa ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
distribuidor CardLayout privado;
deckPanel JPanel privado;
CardLayoutDemo público ( )
{
setSize(ANCHO, ALTO);
addWindowListener(new WindowDestroyer( ));
setTitle("Demostración de CardLayout");
Contenedor contentPane = getContentPane( );
contentPane.setLayout(new BorderLayout( ));
deckPanel = new JPanel( );
dealer = nuevo CardLayout( );
deckPanel.setLayout(distribuidor);
JPanel startCardPanel = nuevo JPanel( );
startCardPanel.setLayout(nuevo FlowLayout( ));
startCardPanel.setBackground(Color.LIGHT_GRAY);
JLabel startLabel = new JLabel("Hola");
startCardPanel.add(startLabel); p>
p>
deckPanel.add("start", startCardPanel);
JPanel greenCardPanel = new JPanel( );
greenCardPanel.setLayout(new FlowLayout( ));
p>greenCardPanel.setBackground(Color.GREEN);
JLabel goLabel = new JLabel("Ir");
greenCardPanel.add( goLabel);
deckPanel.add("green", greenCardPanel);
JPanel redCardPanel = nuevo JPan
el( );
redCardPanel.setLayout(new FlowLayout());
redCardPanel.setBackground(Color.RED);
JLabel stopLabel = new JLabel( "Detener");
redCardPanel.add(stopLabel);
deckPanel.add("red", redCardPanel);
contentPane.add(deckPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel( );
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setLayout(new FlowLayout( ));
JButton stopButton = new JButton("Rojo");
stopButton.addActionListener(this);
buttonPanel.add(stopButton);
JButton goButton = new JButton("Verde");
goButton.addActionListener(this);
buttonPanel.add(goButton);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
contentPane. add(buttonPanel, BorderLayout.SOUTH);
dealer.first(deckPanel);//Opcional
}
public void actionPerformed(ActionEvent e) p>
{
String actionCommand = e.getActionCommand( );
if (actionCommand.equals("Red"))
dealer.show (deckPanel, "rojo");
else if (actionCommand.equals("Green"))
dealer.show(deckPanel, "verde");
else if (actionCommand.equals("Reset"))
dealer.show(deckPanel, "start");
else
System.out.println("Error en la demostración de CardLayout.");
}