Sie sind auf Seite 1von 5

Universidad Nacional del Altiplano Puno

Escuela Profesional de Ingeniera de Sistemas


CURSO: TECNOLOGA DE DESARROLLO DE APLICACIONES

GUA ACADMICA Nro. 12

TEMA: EJEMPLOS DE SWING

Apellidos y Nombres: Nro. Matrcula: .

OBJETIVOS:

Aprender la programacin Visual en el Lenguaje Java

REQUERIMIENTOS:

PC con Windows.
jdk-6u10-windows-i586-p.exe
netbeans-6.5-ml-windows.exe

PARTE 1: EJEMPLOS DE JAVA

1.1 EVENTOS DE UNA CAJA DE COMBINACIONES

A continuacin desarrollaremos una Aplicacin que maneja los eventos de la Caja de


Combinaciones.

a. Cree un nuevo proyecto de tipo Java Application y en la opcin Project Name coloque
pEventoComBox, y en la opcin Create Main Class coloque:
peventocombox.CEventoComBox, luego haga click en Finish

b. Copie y ejecute el siguiente cdigo (Archivo: CEventoComBox.java):


package peventocombox;

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

public class CEventoComBox extends JFrame implements ActionListener


{
private JComboBox Colores;
private Color color = Color.green;
private String Items[] = {"Verde","Amarillo","Azul","Blanco"};
private JPanel PanelSur;
private JPanel PanelCentro;
private Border Borde;
private JLabel Texto;

public CEventoComBox()
{
addWindowListener( new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } });

int iWidth = 400, iHeight = 150;


Toolkit oTk = Toolkit.getDefaultToolkit();
Dimension oDi = oTk.getScreenSize();
setSize(iWidth, iHeight);
setLocation((oDi.width / 2) - (iWidth / 2), (oDi.height / 2) - (iHeight / 2));

setTitle("Caja de Combinaciones con Eventos");


setResizable(false);

Container cMain = getContentPane();

Ing. Juan Antonio Flores Moroco 1


Universidad Nacional del Altiplano Puno
Escuela Profesional de Ingeniera de Sistemas
CURSO: TECNOLOGA DE DESARROLLO DE APLICACIONES
cMain.setLayout(new BorderLayout());

// Panel Inferior
PanelSur = new JPanel();
Colores = new JComboBox(Items);
PanelSur.add(Colores);
Colores.addActionListener(this);

//Panel del Centro


PanelCentro = new JPanel();
Borde = BorderFactory.createMatteBorder(8, 8, 8, 8, color);
PanelCentro.setBorder(Borde);
Texto = new JLabel("Verde");
PanelCentro.add(Texto);

cMain.add(PanelSur, BorderLayout.SOUTH);
cMain.add(PanelCentro, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
JComboBox CmbOrigen = (JComboBox)evt.getSource();
if((String)CmbOrigen.getSelectedItem() == "Verde")
color = Color.GREEN;
else if((String)CmbOrigen.getSelectedItem() == "Amarillo")
color = Color.YELLOW;
else if((String)CmbOrigen.getSelectedItem() == "Azul")
color = Color.BLUE;
else if((String)CmbOrigen.getSelectedItem() == "Blanco")
color = Color.WHITE;

Borde = BorderFactory.createMatteBorder(8, 8, 8, 8, color);


PanelCentro.setBorder(Borde);
Texto.setText((String)CmbOrigen.getSelectedItem());
}
public static void main(String[] args)
{
CEventoComBox oCmb = new CEventoComBox();
oCmb.setVisible(true);
}
}

Resultado:

1.2 EVENTOS DE UNA CAJA DE LISTAS

a. Cree un nuevo proyecto de tipo Java Application y en la opcin Project Name coloque
pEventoList, y en la opcin Create Main Class coloque: peventolist.CEventoList, luego
haga click en Finish

b. Copie y ejecute el siguiente cdigo (Archivo: CEventoList.java):


package peventolist;

import java.awt.*;

Ing. Juan Antonio Flores Moroco 2


Universidad Nacional del Altiplano Puno
Escuela Profesional de Ingeniera de Sistemas
CURSO: TECNOLOGA DE DESARROLLO DE APLICACIONES
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class CEventoList extends JFrame


{
private JList LstFruta;
private String Item[] = {"Manzana", "Pera", "Platano", "Uva", "Naranja"};
private JPanel PanelFruta;
private JLabel LblItem;

public CEventoList()
{
addWindowListener( new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } });

int iWidth = 400, iHeight = 150;


Toolkit oTk = Toolkit.getDefaultToolkit();
Dimension oDi = oTk.getScreenSize();
setSize(iWidth, iHeight);
setLocation((oDi.width / 2) - (iWidth / 2), (oDi.height / 2) - (iHeight / 2));

setTitle("Caja de Listas con Eventos");


setResizable(false);

Container cMain = getContentPane();


cMain.setLayout(new FlowLayout());

//Panel de Frutas
PanelFruta = new JPanel();

LstFruta = new JList(Item);


LstFruta.setVisibleRowCount(4);
LstFruta.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
LblItem = new JLabel("Seleccione una Fruta");

PanelFruta.add(new JScrollPane(LstFruta));
PanelFruta.add(LblItem);

LstFruta.addListSelectionListener( new ListSelectionListener()


{ public void valueChanged(ListSelectionEvent e)
{ LblItem.setText("A seleccionado: " + Item[LstFruta.getSelectedIndex()]); }
} );

cMain.add(PanelFruta);
}
public static void main(String[] args)
{
CEventoList oList = new CEventoList();
oList.setVisible(true);
}
}

Resultado:

Ing. Juan Antonio Flores Moroco 3


Universidad Nacional del Altiplano Puno
Escuela Profesional de Ingeniera de Sistemas
CURSO: TECNOLOGA DE DESARROLLO DE APLICACIONES

1.3 EVENTOS DE SELECCIN MULTIPLE DE UNA CAJA DE LISTAS

A continuacin desarrollaremos una Aplicacin que maneja la seleccin mltiple en caja de


Listas.

a. Cree un nuevo proyecto de tipo Java Application y en la opcin Project Name coloque
pEventListSel, y en la opcin Create Main Class coloque: peventlistsel.CEventListSel,
luego haga click en Finish

b. Copie y ejecute el siguiente cdigo (Archivo: CEventListSel.java):


package peventlistsel;

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

public class CEventListSel extends JFrame


{
private JList LstOrigen;
private JList LstDestino;
private String Item[] = {"Primero","Segundo", "Tercero", "Cuarto", "Quinto", "Sexto"};
private JPanel PanelMain;

public CEventListSel()
{
addWindowListener( new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } });

int iWidth = 400, iHeight = 150;


Toolkit oTk = Toolkit.getDefaultToolkit();
Dimension oDi = oTk.getScreenSize();
setSize(iWidth, iHeight);
setLocation((oDi.width / 2) - (iWidth / 2), (oDi.height / 2) - (iHeight / 2));

setTitle("Seleccion de elementos de la Caja de Listas");


setResizable(false);

Container oMain = getContentPane();


oMain.setLayout(new FlowLayout());

//Panel
PanelMain = new JPanel();

LstOrigen = new JList(Item);


LstOrigen.setVisibleRowCount(5);
LstOrigen.setFixedCellWidth(60);
LstOrigen.setFixedCellHeight(20);
LstOrigen.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

LstDestino = new JList();


LstDestino.setVisibleRowCount(5);
LstDestino.setFixedCellWidth(80);
LstDestino.setFixedCellHeight(20);
LstDestino.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

PanelMain.add(new JScrollPane(LstOrigen));
PanelMain.add(new JScrollPane(LstDestino));

LstOrigen.addListSelectionListener(new ListSelectionListener()
{ public void valueChanged(ListSelectionEvent e)
{ LstDestino.setListData(LstOrigen.getSelectedValues()); }
} );
oMain.add(PanelMain);
}

Ing. Juan Antonio Flores Moroco 4


Universidad Nacional del Altiplano Puno
Escuela Profesional de Ingeniera de Sistemas
CURSO: TECNOLOGA DE DESARROLLO DE APLICACIONES
public static void main(String[] args)
{
CEventListSel oListse = new CEventListSel();
oListse.setVisible(true);
}
}

Resultado:

Ing. Juan Antonio Flores Moroco 5

Das könnte Ihnen auch gefallen