Usar border layout para cambiar los colores de sus paneles respectivos:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package unidad4ejercicio8;
import java.awt.Color;
import java.util.Random;
import javax.swing.JPanel;
/**
*
* @author Juan
*/
public class VentanaBorder extends javax.swing.JFrame {
/**
* Creates new form VentanaBorder
*/
public VentanaBorder() {
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() {
panCentral = new javax.swing.JPanel();
btnCentro = new javax.swing.JButton();
panNorte = new javax.swing.JPanel();
btnNorte = new javax.swing.JButton();
panSur = new javax.swing.JPanel();
btnSur = new javax.swing.JButton();
panEste = new javax.swing.JPanel();
btnEste = new javax.swing.JButton();
panOeste = new javax.swing.JPanel();
btnOeste = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnCentro.setText("Central");
btnCentro.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCentroActionPerformed(evt);
}
});
javax.swing.GroupLayout panCentralLayout = new javax.swing.GroupLayout(panCentral);
panCentral.setLayout(panCentralLayout);
panCentralLayout.setHorizontalGroup(
panCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panCentralLayout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(btnCentro)
.addContainerGap(87, Short.MAX_VALUE))
);
panCentralLayout.setVerticalGroup(
panCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panCentralLayout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(btnCentro)
.addContainerGap(39, Short.MAX_VALUE))
);
getContentPane().add(panCentral, java.awt.BorderLayout.CENTER);
btnNorte.setText("Norte");
btnNorte.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNorteActionPerformed(evt);
}
});
javax.swing.GroupLayout panNorteLayout = new javax.swing.GroupLayout(panNorte);
panNorte.setLayout(panNorteLayout);
panNorteLayout.setHorizontalGroup(
panNorteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panNorteLayout.createSequentialGroup()
.addGap(166, 166, 166)
.addComponent(btnNorte)
.addContainerGap(169, Short.MAX_VALUE))
);
panNorteLayout.setVerticalGroup(
panNorteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panNorteLayout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(btnNorte)
.addContainerGap(55, Short.MAX_VALUE))
);
getContentPane().add(panNorte, java.awt.BorderLayout.PAGE_START);
btnSur.setText("Sur");
btnSur.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSurActionPerformed(evt);
}
});
javax.swing.GroupLayout panSurLayout = new javax.swing.GroupLayout(panSur);
panSur.setLayout(panSurLayout);
panSurLayout.setHorizontalGroup(
panSurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panSurLayout.createSequentialGroup()
.addGap(167, 167, 167)
.addComponent(btnSur)
.addContainerGap(178, Short.MAX_VALUE))
);
panSurLayout.setVerticalGroup(
panSurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panSurLayout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(btnSur)
.addContainerGap(46, Short.MAX_VALUE))
);
getContentPane().add(panSur, java.awt.BorderLayout.PAGE_END);
btnEste.setText("Este");
btnEste.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEsteActionPerformed(evt);
}
});
javax.swing.GroupLayout panEsteLayout = new javax.swing.GroupLayout(panEste);
panEste.setLayout(panEsteLayout);
panEsteLayout.setHorizontalGroup(
panEsteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panEsteLayout.createSequentialGroup()
.addContainerGap(17, Short.MAX_VALUE)
.addComponent(btnEste)
.addContainerGap())
);
panEsteLayout.setVerticalGroup(
panEsteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panEsteLayout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(btnEste)
.addContainerGap(39, Short.MAX_VALUE))
);
getContentPane().add(panEste, java.awt.BorderLayout.LINE_END);
btnOeste.setText("Oeste");
btnOeste.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOesteActionPerformed(evt);
}
});
javax.swing.GroupLayout panOesteLayout = new javax.swing.GroupLayout(panOeste);
panOeste.setLayout(panOesteLayout);
panOesteLayout.setHorizontalGroup(
panOesteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panOesteLayout.createSequentialGroup()
.addContainerGap()
.addComponent(btnOeste)
.addContainerGap(17, Short.MAX_VALUE))
);
panOesteLayout.setVerticalGroup(
panOesteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panOesteLayout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(btnOeste)
.addContainerGap(41, Short.MAX_VALUE))
);
getContentPane().add(panOeste, java.awt.BorderLayout.LINE_START);
pack();
}// </editor-fold>
private void btnNorteActionPerformed(java.awt.event.ActionEvent evt) {
pulsacion(evt);
}
private void btnOesteActionPerformed(java.awt.event.ActionEvent evt) {
pulsacion(evt); // TODO add your handling code here:
}
private void btnCentroActionPerformed(java.awt.event.ActionEvent evt) {
pulsacion(evt); // TODO add your handling code here:
}
private void btnEsteActionPerformed(java.awt.event.ActionEvent evt) {
pulsacion(evt);// TODO add your handling code here:
}
private void btnSurActionPerformed(java.awt.event.ActionEvent evt) {
pulsacion(evt); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(VentanaBorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VentanaBorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VentanaBorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VentanaBorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VentanaBorder().setVisible(true);
}
});
}
private void pulsacion(java.awt.event.ActionEvent evt)
{
Color nuevo;
Random al;
JPanel temp;
temp = new JPanel();
al =new Random();
nuevo=new Color(al.nextInt(256),al.nextInt(256),al.nextInt(256));
//asignamos a temp el panel dónde se ha pulsado el botón
if (evt.getSource().equals(btnNorte)) temp=panNorte;
else if (evt.getSource().equals(btnCentro)) panCentral.setBackground(nuevo);
else if (evt.getSource().equals(btnEste)) panEste.setBackground(nuevo);
else if (evt.getSource().equals(btnOeste)) panOeste.setBackground(nuevo);
else if (evt.getSource().equals(btnSur)) panSur.setBackground(nuevo);
//calculamos un color aleatorio
//asignamos al panel un color
temp.setBackground(nuevo);
}
// Variables declaration - do not modify
private javax.swing.JButton btnCentro;
private javax.swing.JButton btnEste;
private javax.swing.JButton btnNorte;
private javax.swing.JButton btnOeste;
private javax.swing.JButton btnSur;
private javax.swing.JPanel panCentral;
private javax.swing.JPanel panEste;
private javax.swing.JPanel panNorte;
private javax.swing.JPanel panOeste;
private javax.swing.JPanel panSur;
// End of variables declaration
}
No hay comentarios:
Publicar un comentario