Un formulario en Java que pida elegir las carreras en pantalla y que muestre en pantalla las carreras elegidas de Derecho, Sistemas, Contabilidad y Economía.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package checkbox;
/**
*
* @author CBTIS73
*/
public class Ccheckbox extends javax.swing.JFrame {
void carreras(){
String mensaje=("carreras elegidas:");
if(chkDerecho.isSelected()){
mensaje=mensaje+"Derecho";
}
if(chkSistemas.isSelected()){
mensaje=mensaje+"sistemas";
}
if(chkContabilidad.isSelected()){
mensaje=mensaje+"Contabilidad";
}
if(chkEconomia.isSelected()){
mensaje=mensaje+"Economia";
}
lblRespuesta.setText(mensaje);
}
void limpiar(){
lblRespuesta.setText("");
if(chkDerecho.isSelected()){
chkDerecho.setSelected(false);
}
if(chkSistemas.isSelected()){
chkSistemas.setSelected(false);
}
if(chkContabilidad.isSelected()){
chkContabilidad.setSelected(false);
}
if(chkEconomia.isSelected()){
chkEconomia.setSelected(false);
}
}
/**
* Creates new form Ccheckbox
*/
public Ccheckbox() {
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() {
jLabel1 = new javax.swing.JLabel();
chkDerecho = new javax.swing.JCheckBox();
chkSistemas = new javax.swing.JCheckBox();
chkContabilidad = new javax.swing.JCheckBox();
chkEconomia = new javax.swing.JCheckBox();
btnNuevo = new javax.swing.JButton();
btnAceptar = new javax.swing.JButton();
lblRespuesta = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Eleccion de Carreras");
chkDerecho.setText("Derecho");
chkSistemas.setText("Sistemas");
chkContabilidad.setText("Contabilidad");
chkEconomia.setText("Economia");
btnNuevo.setText("Nuevo");
btnNuevo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNuevoActionPerformed(evt);
}
});
btnAceptar.setText("Aceptar");
btnAceptar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAceptarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(chkEconomia)
.addComponent(chkContabilidad)
.addComponent(chkSistemas)
.addComponent(chkDerecho))
.addGap(94, 94, 94))
.addGroup(layout.createSequentialGroup()
.addGap(103, 103, 103)
.addComponent(jLabel1)
.addContainerGap(112, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(btnAceptar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNuevo)
.addGap(39, 39, 39))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblRespuesta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(22, 22, 22))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(chkDerecho)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkSistemas)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkContabilidad)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(chkEconomia)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnNuevo)
.addComponent(btnAceptar))
.addGap(18, 18, 18)
.addComponent(lblRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(73, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
carreras();
// TODO add your handling code here:
}
private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {
limpiar();
// 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(Ccheckbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Ccheckbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Ccheckbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Ccheckbox.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 Ccheckbox().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAceptar;
private javax.swing.JButton btnNuevo;
private javax.swing.JCheckBox chkContabilidad;
private javax.swing.JCheckBox chkDerecho;
private javax.swing.JCheckBox chkEconomia;
private javax.swing.JCheckBox chkSistemas;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblRespuesta;
// End of variables declaration
}