Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Implemente y ejecute las implementaciones presentadas en el presente laboratorio, analice y luego comente
las líneas de código.
1. Creación de una ventana
import javax.swing.*;
public class Ventana extends JFrame{
public Ventana() {
super("Algorítmica II");
//Tamano de la ventama
this.setSize(300,200);
//Permite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Ventana() {
1
super("Algorítmica II");
definirVentana();
//Tamano de la ventama
this.setSize(400,400);
//Permite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* Programación por eventos
* 1. Debemos implementar la interface ActionListener
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Ventana() {
super("Algorítmica II");
definirVentana();
//this.setResizable(false);//Ventana tamaño fijo
this.setLocationRelativeTo(null);//Ventana centrada
//Tamano de la ventama
2
this.setSize(400,400);
//Petrmite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void definirVentana (){
//Ordena componentes secuencialmente
FlowLayout fl=new FlowLayout();
this.setLayout(fl);
//this.setLayout(new FlowLayout());
jbtnBoton=new JButton("Enviar");
jtxtTexto=new JTextField(20);
jlblLabel=new JLabel();
this.add(jtxtTexto);
this.add(jbtnBoton);
this.add(jlblLabel);
//
jbtnBoton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtnBoton)
jlblLabel.setText(jtxtTexto.getText());
}
}
public class PruebaVentanaEventos {
public static void main(String[] args) {
Ventana v=new Ventana();
}
}
/**
* Programacion por eventos
* 1. Debemos implementar la interface ItemListener
* 2. Eventos de JCheckBox
**/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Ventana() {
super("Algorítmica II: Fuentes");
3
definirVentana();
//Tamaño de la ventana
this.setSize(400,400);
//Permite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void definirVentana (){
//Ordena componentes secuencialmente
FlowLayout fl=new FlowLayout();
this.setLayout(fl);
//this.setLayout(new FlowLayout());
texto=new JTextField(20);
negrita=new JCheckBox("Negrita");
cursiva=new JCheckBox("Cursiva");
add(texto);
add(negrita);
add(cursiva);
//Métodos propios de JCheckBox
negrita.addItemListener(this);
cursiva.addItemListener(this);
}
//Redefiniendo metodo abstracto de la Interface ItemListener
public void itemStateChanged(ItemEvent e){
if(negrita.isSelected() && cursiva.isSelected()){
//Cambia texto a negrita y cursiva
fuente=new Font("Serief", Font.BOLD|Font.ITALIC, 15);
texto.setFont(fuente);
}
else{
if(cursiva.isSelected()){
//cambia texto a cursiva
fuente=new Font("Sans_Serif", Font.ITALIC, 15);
//fuente=new Font("Serief", Font.ITALIC, 15);
texto.setFont(fuente);//Asocia o aplica
}
else{
if(negrita.isSelected()){
//cambia texto a negrita
fuente=new Font("Sans_Serif", Font.BOLD, 15);
texto.setFont(fuente);
}
else{
//cambia texto plano
fuente=new Font("Sans_Serif", Font.PLAIN, 15);
texto.setFont(fuente);
}
}
}
}
}
4
5. Creación de una ventana para manejo de fuentes con JRadioButton
/**
* Programacion por eventos
* 1. Debemos implementar la interface ItemListener
* 2. Eventos de JRadioButton
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Ventana() {
super("Algorítmica II: Fuentes");
definirVentana();
//Tamano de la ventama
this.setSize(400,400);
//Permite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void definirVentana (){
//Ordena componentes secuencialmente
FlowLayout fl=new FlowLayout();
this.setLayout(fl);
texto=new JTextField(20);
negrita=new JRadioButton("Negrita");
cursiva=new JRadioButton("Cursiva");
ambos=new JRadioButton("Ambos");
plano=new JRadioButton("Plano");
grupo=new ButtonGroup();
//Asociando a la ventana
add(texto);
add(negrita);
add(cursiva);
add(ambos);
add(plano);
//Asociando al grupo
grupo.add(negrita);
grupo.add(cursiva);
grupo.add(ambos);
grupo.add(plano);
//Metodos de manejo de eventos
5
negrita.addItemListener(this);
cursiva.addItemListener(this);
ambos.addItemListener(this);
plano.addItemListener(this);
}
public Font obtenerFuente(int nf){
Font resu=null;
switch(nf){
case 0:
resu=new Font("Sans_Serif", Font.BOLD, 15);
break;
case 1:
resu=new Font("Sans_Serif", Font.ITALIC, 15);
break;
case 2:
resu=new Font("Sans_Serif", Font.BOLD|Font.ITALIC, 15);
break;
case 3:
resu=new Font("Sans_Serif", Font.PLAIN, 15);
break;
}
return resu;
}
//Redefiniendo metodo abstracto de la Interface ItemListener
public void itemStateChanged(ItemEvent e){
if(negrita.isSelected()){
texto.setFont(obtenerFuente(0));
}
else{
if(cursiva.isSelected()){
texto.setFont(obtenerFuente(1));
}
else{
if(ambos.isSelected()){
texto.setFont(obtenerFuente(2));
}
else{
if(plano.isSelected()){
texto.setFont(obtenerFuente(3));
}
}
}
}
}
}
public class PruebaVentanaJRadioButton {
public static void main(String[] args) {
Ventana v = new Ventana();
}
}
6
6. Creación de una ventana con JComboBox para manejo de gráficos
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Ventana() {
super("Algorítmica II - JComboBox");
definirVentana();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,400);
this.setVisible(true);
}
void definirVentana (){
//Ordena componentes secuencialmente
FlowLayout fl=new FlowLayout();
this.setLayout(fl);
combo=new JComboBox(archivos);
label=new JLabel();
label.setIcon(iconos[0]);
combo.addItemListener(this);
add(combo);
add(label);
}
7
7. Creación de una ventana que envía texto de un campo de texto a otro.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public Ventana() {
super("GridBagLayout - Algorítmica II");
//Tamañoo de la ventama
this.setSize(350,200);
//Permite cerrar la ventana y libera memoria
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
definirVentana();
this.setVisible(true);
}
area=new JTextArea();
scroll=new JScrollPane(area);
texto=new JTextField(20);
boton=new JButton("Enviar");
//componente scroll
gbc.gridx=0;//columna inicio
gbc.gridy=0;//fila inicio
gbc.gridwidth=2;//numero columnas
gbc.gridheight=1;//numero filas
gbc.weightx=1.0;//Cuanto crece cuando la ventana expande
gbc.weighty=1.0;
gbc.fill=GridBagConstraints.BOTH;//Horiz y vertical
add(scroll, gbc);
//componente texto
gbc.gridx=0;//columna inicio
gbc.gridy=1;//fila inicio
gbc.gridwidth=1;//numero columnas
gbc.gridheight=1;//numero filas
gbc.weightx=1.0;//crece en x
8
gbc.weighty=0.0;//crece en y
gbc.fill=GridBagConstraints.HORIZONTAL;
add(texto, gbc);
//componente boton
gbc.gridx=1;//columna inicio
gbc.gridy=1;//fila inicio
gbc.gridwidth=1;//numero columnas
gbc.gridheight=1;//numero filas
gbc.weightx=0.0;
gbc.weighty=0.0;
gbc.fill=GridBagConstraints.NONE;
add(boton, gbc);
boton.addActionListener(this);
}