Subido por Angel Miguel De la Rosa Morillo

tarea tema windows form

Anuncio
JAVA -PROFESOR: ING. FREIDY NUÑEZ
TAREA JAVA TEMA DISEÑO.
Realizar los ejercicios usando los mismos diseños mostrados.
1-Vamos a crear un imitador, como si fuera un espejo. Tendremos dos pares de
conjunto de elementos separados (puedes usar un separador) y cuando nosotros
pinchamos en un elemento o escribimos en un campo, se debe cambiar el otro
lado.
Por ejemplo, si yo tengo un campo de texto y escribo en él, el campo de texto que
es su reflejo también recibirá ese texto.
Puedes usar los elementos que quieras, ejemplo: JTextField, JRadioButton,
JCheckBox, JTextArea, JSpinner, etc.
Solo puedes modificar de un lado, el otro conjunto no lo podéis modificar, es decir,
que no es bidireccional.
import java.awt.EventQueue;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JLabel;
java.awt.Font;
javax.swing.border.BevelBorder;
java.awt.Color;
javax.swing.JSeparator;
javax.swing.JRadioButton;
javax.swing.JCheckBox;
javax.swing.JTextField;
javax.swing.JComboBox;
javax.swing.JSpinner;
javax.swing.ButtonGroup;
javax.swing.DefaultComboBoxModel;
JAVA -PROFESOR: ING. FREIDY NUÑEZ
import
import
import
import
import
import
import
import
import
java.awt.SystemColor;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
java.awt.event.KeyAdapter;
java.awt.event.KeyEvent;
java.awt.event.ItemListener;
java.awt.event.ItemEvent;
javax.swing.event.ChangeListener;
javax.swing.event.ChangeEvent;
public class Espejo
{
private JFrame frmEspejo;
private JTextField textFieldOg;
private JTextField textFieldEp;
JRadioButton OpcionOg1, OpcionOg2,OpcionOg3,
OpcionEp1,OpcionEp2,OpcionEp3;
JCheckBox OpcionOg4, OpcionOg5, OpcionOg6, OpcionEp4, OpcionEp5,
OpcionEp6;
JComboBox comboBoxOg,comboBoxEp;
JSpinner spinnerOg, spinnerEp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Espejo window = new Espejo();
window.frmEspejo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Espejo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmEspejo = new JFrame();
frmEspejo.setTitle("Imitador");
frmEspejo.setBounds(100, 100, 515, 462);
frmEspejo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEspejo.getContentPane().setLayout(null);
ButtonGroup grupo= new ButtonGroup();
ButtonGroup grupo2 = new ButtonGroup();
JAVA -PROFESOR: ING. FREIDY NUÑEZ
JPanel panel = new JPanel();
panel.setBackground(SystemColor.control);
panel.setBorder(new BevelBorder(BevelBorder.LOWERED,
Color.LIGHT_GRAY, null, null, null));
panel.setBounds(10, 10, 474, 397);
frmEspejo.getContentPane().add(panel);
panel.setLayout(null);
JLabel LabelOg = new JLabel("Original");
LabelOg.setFont(new Font("Tahoma", Font.BOLD, 18));
LabelOg.setBounds(10, 0, 77, 36);
panel.add(LabelOg);
JSeparator separator = new JSeparator();
separator.setBounds(0, 213, 484, 22);
panel.add(separator);
OpcionOg1 = new JRadioButton("Opcion 1");
OpcionOg1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg1_actionPerformed(e);
}
});
OpcionOg1.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionOg1.setBounds(10, 67, 103, 21);
panel.add(OpcionOg1);
grupo.add(OpcionOg1);
OpcionOg2 = new JRadioButton("Opcion 2");
OpcionOg2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg2_actionPerformed(e);
}
});
OpcionOg2.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionOg2.setBounds(10, 110, 103, 21);
panel.add(OpcionOg2);
grupo.add(OpcionOg2);
OpcionOg3 = new JRadioButton("Opcion 3");
OpcionOg3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg3_actionPerformed(e);
}
});
OpcionOg3.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionOg3.setBounds(10, 155, 103, 21);
panel.add(OpcionOg3);
grupo.add(OpcionOg3);
OpcionOg4 = new JCheckBox("Opcion 4");
OpcionOg4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg4_actionPerformed(e);
}
});
OpcionOg4.setFont(new Font("Tahoma", Font.PLAIN, 18));
JAVA -PROFESOR: ING. FREIDY NUÑEZ
OpcionOg4.setBounds(177, 67, 103, 21);
panel.add(OpcionOg4);
OpcionOg5 = new JCheckBox("Opcion 5");
OpcionOg5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg5_actionPerformed(e);
}
});
OpcionOg5.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionOg5.setBounds(177, 110, 103, 21);
panel.add(OpcionOg5);
OpcionOg6 = new JCheckBox("Opcion 6");
OpcionOg6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_OpcionOg6_actionPerformed(e);
}
});
OpcionOg6.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionOg6.setBounds(177, 155, 103, 21);
panel.add(OpcionOg6);
textFieldOg = new JTextField();
textFieldOg.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
do_textFieldOg_keyTyped(e);
}
});
textFieldOg.setFont(new Font("Tahoma", Font.PLAIN, 18));
textFieldOg.setBounds(314, 62, 133, 31);
panel.add(textFieldOg);
textFieldOg.setColumns(10);
comboBoxOg = new JComboBox();
comboBoxOg.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
do_comboBoxOg_itemStateChanged(e);
}
});
comboBoxOg.setFont(new Font("Tahoma", Font.PLAIN, 18));
comboBoxOg.setModel(new DefaultComboBoxModel(new String[]
{"Item 1", "Item 2", "Item 3", "Item 4"}));
comboBoxOg.setToolTipText("");
comboBoxOg.setBounds(314, 103, 133, 34);
panel.add(comboBoxOg);
spinnerOg = new JSpinner();
spinnerOg.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
do_spinnerOg_stateChanged(e);
}
});
spinnerOg.setFont(new Font("Tahoma", Font.PLAIN, 18));
spinnerOg.setBounds(314, 147, 133, 36);
panel.add(spinnerOg);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
JLabel LabelEp = new JLabel("Espejo");
LabelEp.setFont(new Font("Tahoma", Font.BOLD, 18));
LabelEp.setBounds(10, 213, 77, 36);
panel.add(LabelEp);
OpcionEp1 = new JRadioButton("Opcion 1");
OpcionEp1.setEnabled(false);
OpcionEp1.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp1.setBounds(10, 255, 103, 21);
panel.add(OpcionEp1);
grupo2.add(OpcionEp1);
OpcionEp2 = new JRadioButton("Opcion 2");
OpcionEp2.setEnabled(false);
OpcionEp2.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp2.setBounds(10, 299, 103, 21);
panel.add(OpcionEp2);
grupo2.add(OpcionEp2);
OpcionEp3 = new JRadioButton("Opcion 3");
OpcionEp3.setEnabled(false);
OpcionEp3.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp3.setBounds(10, 348, 103, 21);
panel.add(OpcionEp3);
grupo2.add(OpcionEp3);
OpcionEp4 = new JCheckBox("Opcion 4");
OpcionEp4.setEnabled(false);
OpcionEp4.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp4.setBounds(177, 255, 103, 21);
panel.add(OpcionEp4);
OpcionEp5 = new JCheckBox("Opcion 5");
OpcionEp5.setEnabled(false);
OpcionEp5.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp5.setBounds(177, 299, 103, 21);
panel.add(OpcionEp5);
OpcionEp6 = new JCheckBox("Opcion 6");
OpcionEp6.setEnabled(false);
OpcionEp6.setFont(new Font("Tahoma", Font.PLAIN, 18));
OpcionEp6.setBounds(177, 348, 103, 21);
panel.add(OpcionEp6);
textFieldEp = new JTextField();
textFieldEp.setForeground(new Color(255, 255, 255));
textFieldEp.setEnabled(false);
textFieldEp.setFont(new Font("Tahoma", Font.PLAIN, 18));
textFieldEp.setColumns(10);
textFieldEp.setBounds(314, 245, 133, 31);
panel.add(textFieldEp);
comboBoxEp = new JComboBox();
comboBoxEp.setModel(new DefaultComboBoxModel(new String[]
{"Item 1", "Item 2", "Item 3", "Item 4"}));
comboBoxEp.setEnabled(false);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
comboBoxEp.setToolTipText("");
comboBoxEp.setFont(new Font("Tahoma", Font.PLAIN, 18));
comboBoxEp.setBounds(314, 291, 133, 36);
panel.add(comboBoxEp);
spinnerEp = new JSpinner();
spinnerEp.setEnabled(false);
spinnerEp.setFont(new Font("Tahoma", Font.PLAIN, 18));
spinnerEp.setBounds(314, 340, 133, 36);
panel.add(spinnerEp);
}
//Evento de los botones
protected void do_OpcionOg1_actionPerformed(ActionEvent e) {
this.OpcionEp1.setSelected(true);
}
protected void do_OpcionOg2_actionPerformed(ActionEvent e) {
this.OpcionEp2.setSelected(true);
}
protected void do_OpcionOg3_actionPerformed(ActionEvent e) {
this.OpcionEp3.setSelected(true);
}
protected void do_OpcionOg4_actionPerformed(ActionEvent e) {
this.OpcionEp4.setSelected(this.OpcionOg4.isSelected());
}
protected void do_OpcionOg5_actionPerformed(ActionEvent e) {
this.OpcionEp5.setSelected(this.OpcionOg5.isSelected());
}
protected void do_OpcionOg6_actionPerformed(ActionEvent e) {
this.OpcionEp6.setSelected(this.OpcionOg6.isSelected());
}
//Evento del text
protected void do_textFieldOg_keyTyped(KeyEvent e) {
this.textFieldEp.setText(this.textFieldOg.getText());
}
//Evento de comboBox
protected void do_comboBoxOg_itemStateChanged(ItemEvent e) {
this.comboBoxEp.setSelectedIndex(this.comboBoxOg.getSelectedIndex());
}
//Evento de spinner
protected void do_spinnerOg_stateChanged(ChangeEvent e) {
this.spinnerEp.setValue((Integer)this.spinnerOg.getValue());
}
}
JAVA -PROFESOR: ING. FREIDY NUÑEZ
2-Crea un generador de números gráfico. Nosotros escribiremos seleccionaremos
dos números en unos JSpinner (contadores) y se nos mostrara en un JTextField,
el número generado entre esos dos números, al pulsar en el botón. El JTextField
no debe ser editable.
import java.awt.EventQueue;
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.border.BevelBorder;
javax.swing.JLabel;
javax.swing.JOptionPane;
import
import
import
import
import
import
import
import
import
java.awt.Font;
javax.swing.JSpinner;
javax.swing.JTextField;
javax.swing.JButton;
java.awt.Color;
java.awt.event.ActionListener;
java.lang.reflect.GenericArrayType;
java.awt.event.ActionEvent;
javax.swing.SpinnerNumberModel;
public class GeneradorNumero {
private JFrame frmGeneradorDeNmeros;
private JTextField txt;
JSpinner n1, n2;
JButton boton;
JAVA -PROFESOR: ING. FREIDY NUÑEZ
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GeneradorNumero window = new
GeneradorNumero();
window.frmGeneradorDeNmeros.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GeneradorNumero() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmGeneradorDeNmeros = new JFrame();
frmGeneradorDeNmeros.setTitle("Generador de numeros ");
frmGeneradorDeNmeros.setBounds(100, 100, 305, 347);
frmGeneradorDeNmeros.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
;
frmGeneradorDeNmeros.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
null, null, null));
panel.setBounds(10, 10, 259, 286);
frmGeneradorDeNmeros.getContentPane().add(panel);
panel.setLayout(null);
JLabel numero1 = new JLabel("Numero 1");
numero1.setFont(new Font("Tahoma", Font.PLAIN, 14));
numero1.setBounds(10, 50, 79, 41);
panel.add(numero1);
JLabel Numero2 = new JLabel("Numero 2");
Numero2.setFont(new Font("Tahoma", Font.PLAIN, 14));
Numero2.setBounds(10, 101, 79, 41);
panel.add(Numero2);
n1 = new JSpinner();
n1.setModel(new SpinnerNumberModel(Integer.valueOf(0), null,
null, Integer.valueOf(1)));
n1.setFont(new Font("Tahoma", Font.PLAIN, 14));
n1.setBounds(150, 56, 85, 29);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
panel.add(n1);
n2 = new JSpinner();
n2.setFont(new Font("Tahoma", Font.PLAIN, 14));
n2.setBounds(150, 107, 85, 29);
panel.add(n2);
JLabel generarText = new JLabel("Numero generado ");
generarText.setFont(new Font("Tahoma", Font.PLAIN, 14));
generarText.setBounds(10, 169, 137, 29);
panel.add(generarText);
txt = new JTextField();
txt.setEditable(false);
txt.setBackground(new Color(255, 255, 255));
txt.setForeground(new Color(0, 0, 0));
txt.setBounds(150, 169, 87, 26);
panel.add(txt);
txt.setColumns(10);
boton = new JButton("Generar");
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_Generar_actionPerformed(e);
}
});
boton.setFont(new Font("Tahoma", Font.PLAIN, 14));
boton.setBounds(74, 227, 98, 29);
panel.add(boton);
}
protected
void do_Generar_actionPerformed(ActionEvent e) {
int num1 = (int)n1.getValue();
int num2 = (int)n2.getValue();
int generarnumero = (int)
Math.floor(Math.random()*(num2-num1+1)+(num1));
txt.setText(generarnumero+"");
}
}
JAVA -PROFESOR: ING. FREIDY NUÑEZ
3-Crea una mini encuesta gráfica. Daremos una serie de opciones para que el
usuario elija. La encuesta preguntará lo siguiente:
Elije un sistema operativo (solo una opción, JRadioButton)


Windows

Linux


Mac
Elije tu especialidad (pueden seleccionar ninguna o varias opciones,
JCheckBox)
Programación
Diseño gráfico
Administración
 Horas dedicadas en el ordenador (usaremos un slider entre 0 y 10)
Para el slider, recomiendo usar un JLabel, que diga qué valor tiene el slider, usar
el evento stateChanged.y al final mostrar un mensaje similar al que está debajo de
la primera imagen.
JAVA -PROFESOR: ING. FREIDY NUÑEZ
import java.awt.EventQueue;
import
import
import
import
import
javax.swing.ButtonGroup;
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JLabel;
javax.swing.JOptionPane;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
java.awt.Font;
javax.swing.JRadioButton;
javax.swing.JSeparator;
javax.swing.border.BevelBorder;
java.awt.Color;
javax.swing.JCheckBox;
javax.swing.UIManager;
javax.swing.JSlider;
java.beans.PropertyChangeListener;
java.beans.PropertyChangeEvent;
javax.swing.SwingConstants;
javax.swing.event.ChangeListener;
javax.swing.event.ChangeEvent;
javax.swing.JButton;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
JAVA -PROFESOR: ING. FREIDY NUÑEZ
public class encuesta implements ChangeListener{
private JFrame frmEncuesta;
JRadioButton Windows,Mac,Linux;
JSlider slider;
private JSeparator separator;
private JLabel especialidad;
JCheckBox Programacion,DG,Administracion;
private JLabel lblNewLabel_1;
private JSeparator separator_1;
JLabel hora,SO,PL,AL,DL;
String n,sistem,pT,aT,dT;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
encuesta window = new encuesta();
window.frmEncuesta.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public encuesta() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmEncuesta = new JFrame();
frmEncuesta.setTitle("Encuesta");
frmEncuesta.setBounds(100, 100, 571, 555);
frmEncuesta.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEncuesta.getContentPane().setLayout(null);
ButtonGroup grupo= new ButtonGroup();
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
null, null, null));
panel.setBounds(10, 10, 523, 494);
frmEncuesta.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Elije un sistema operativo
(solo una opción)");
JAVA -PROFESOR: ING. FREIDY NUÑEZ
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel.setBounds(10, 0, 331, 68);
panel.add(lblNewLabel);
Windows = new JRadioButton("Windows");
Windows.setFont(new Font("Tahoma", Font.PLAIN, 16));
Windows.setBounds(6, 56, 103, 21);
Windows.addChangeListener(this);
panel.add(Windows);
grupo.add(Windows);
Mac = new JRadioButton("Mac");
Mac.setFont(new Font("Tahoma", Font.PLAIN, 16));
Mac.setBounds(6, 118, 103, 21);
Mac.addChangeListener(this);
panel.add(Mac);
grupo.add(Mac);
Linux = new JRadioButton("Linux");
Linux.setFont(new Font("Tahoma", Font.PLAIN, 16));
Linux.setBounds(6, 84, 103, 21);
Linux.addChangeListener(this);
panel.add(Linux);
grupo.add(Linux);
separator = new JSeparator();
separator.setForeground(new Color(160, 160, 160));
separator.setBackground(new Color(255, 255, 255));
separator.setBounds(10, 156, 503, 22);
panel.add(separator);
especialidad = new JLabel("Elije tu especialidad (pueden
seleccionar ninguna o varias opciones)");
especialidad.setFont(new Font("Tahoma", Font.PLAIN, 16));
especialidad.setBounds(10, 166, 486, 44);
panel.add(especialidad);
Programacion = new JCheckBox("Programacion");
Programacion.setFont(new Font("Tahoma", Font.PLAIN, 16));
Programacion.setBounds(6, 216, 133, 21);
panel.add(Programacion);
DG = new JCheckBox("Diseño grafico");
DG.setFont(new Font("Tahoma", Font.PLAIN, 16));
DG.setBounds(6, 251, 133, 21);
panel.add(DG);
Administracion = new JCheckBox("Administracion");
Administracion.setFont(new Font("Tahoma", Font.PLAIN, 16));
Administracion.setBounds(6, 286, 133, 21);
panel.add(Administracion);
lblNewLabel_1 = new JLabel("Horas dedicadas en el ordenador
");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(10, 335, 248, 32);
panel.add(lblNewLabel_1);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
separator_1 = new JSeparator();
separator_1.setForeground(UIManager.getColor("Button.shadow"));
separator_1.setBackground(Color.WHITE);
separator_1.setBounds(10, 319, 503, 22);
panel.add(separator_1);
hora = new JLabel("");
hora.setFont(new Font("Tahoma", Font.PLAIN, 16));
hora.setBounds(10, 387, 103, 32);
panel.add(hora);
slider = new JSlider();
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setFont(new Font("Tahoma", Font.PLAIN, 14));
slider.addChangeListener(this);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setToolTipText("");
slider.setValue(0);
slider.setMaximum(10);
slider.setBounds(128, 365, 311, 82);
panel.add(slider);
JButton boton = new JButton("Generar");
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_boton_actionPerformed(e);
}
});
boton.setFont(new Font("Tahoma", Font.PLAIN, 16));
boton.setBounds(197, 455, 123, 25);
panel.add(boton);
SO = new JLabel("");
SO.setBounds(437, 139, 55, 0);
panel.add(SO);
PL = new JLabel("");
PL.setBounds(367, 233, 45, 0);
panel.add(PL);
DL = new JLabel("");
DL.setBounds(367, 257, 45, 0);
panel.add(DL);
AL = new JLabel("");
AL.setBounds(367, 292, 45, 0);
panel.add(AL);
}
protected
void do_boton_actionPerformed(ActionEvent e) {
JAVA -PROFESOR: ING. FREIDY NUÑEZ
if(Programacion.isSelected()) {
pT = "Programacion ";
PL.setText(pT);
}else {
pT = "";
PL.setText(pT);
}
if(DG.isSelected()) {
dT = "Diseño grafico ";
DL.setText(dT);
}else {
dT = "";
DL.setText(dT);
}
if(Administracion.isSelected()) {
aT = "Administracion ";
AL.setText(aT);
}else {
aT = ("");
AL.setText(aT);
}
JOptionPane.showMessageDialog(null, "Tu sistema operativo favorito
es: "+sistem +"\n Tus especialidades son: "+pT+dT+aT+"\nLas horas que
duras en el ordenador son: "+n);
}
@Override
public void stateChanged(ChangeEvent e) {
if(Windows.isSelected()) {
sistem = "Windows";
SO.setText(sistem);
}
if(Linux.isSelected()) {
sistem = "Linux";
SO.setText(sistem);
}
if(Mac.isSelected()) {
sistem = "Mac";
SO.setText(sistem);
}
n = ("" + slider.getValue());
hora.setText(n);
}
}
JAVA -PROFESOR: ING. FREIDY NUÑEZ
4-Crea una simple lista de películas. tendremos un JComboBox, donde almacenaremos
las películas, que vayamos almacenando en un campo de texto. Al pulsar el
botón Añadir la película que hayamos metido, se introducirá en el JComboBox
import java.awt.EventQueue;
import
import
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JLabel;
java.awt.Font;
javax.swing.JTextField;
javax.swing.JComboBox;
javax.swing.JButton;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
public class Pelicula {
JFrame frmPeliculas;
JTextField agregaP;
JComboBox ListaP;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pelicula window = new Pelicula();
window.frmPeliculas.setVisible(true);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Pelicula() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmPeliculas = new JFrame();
frmPeliculas.setTitle("Peliculas");
frmPeliculas.setBounds(100, 100, 511, 222);
frmPeliculas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmPeliculas.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 0, 477, 174);
frmPeliculas.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Escribe el titulo de una
pelicula");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(10, 24, 230, 25);
panel.add(lblNewLabel);
agregaP = new JTextField();
agregaP.setFont(new Font("Tahoma", Font.PLAIN, 16));
agregaP.setBounds(20, 59, 196, 25);
panel.add(agregaP);
agregaP.setColumns(10);
JLabel lblPeliculas = new JLabel("Peliculas");
lblPeliculas.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblPeliculas.setBounds(315, 24, 92, 25);
panel.add(lblPeliculas);
ListaP = new JComboBox();
ListaP.setBounds(301, 59, 145, 25);
panel.add(ListaP);
JButton boton = new JButton("Añadir");
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_boton_actionPerformed(e);
}
});
boton.setFont(new Font("Tahoma", Font.PLAIN, 16));
JAVA -PROFESOR: ING. FREIDY NUÑEZ
boton.setBounds(20, 104, 85, 25);
panel.add(boton);
}
protected
void do_boton_actionPerformed(ActionEvent e) {
String p = agregaP.getText();
ListaP.addItem(p);
agregaP.setText("");
agregaP.requestDefaultFocus();
}
}
El diseño de estos ejercicios es decisión de ustedes.
5-Una biblioteca maneja libros, algunos de ellos son originales y otros son fotocopias. No
todos los libros se prestan. a) Crear la clase Libro b) Agregar atributos: título, original y
prestable c) Agregar métodos de instancia: ‘getOriginal’, ‘getTitulo’ y ‘getPrestable’. d)
Agregar métodos de instancia ' esOriginal' y ' sePresta' que retornen el valor booleano
correspondiente. e) Agregar métodos de instancia ‘setTítulo’ , ‘setOriginal’ y ‘setPrestable’
f) Crear un método main en una clase para prueba que permita obtener 2 instancias de
Libro, uno de ellos es original y no se presta, el otro es fotocopia y se presta. Utilizar los
métodos de instancia para realizar estas operaciones. Mostrar los libros creados
import java.awt.EventQueue;
import java.awt.Image;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
javax.swing.ImageIcon;
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JScrollPane;
javax.swing.JTable;
javax.swing.border.BevelBorder;
javax.swing.table.DefaultTableModel;
javax.swing.JLabel;
java.awt.Font;
javax.swing.JTextField;
javax.swing.JComboBox;
javax.swing.DefaultComboBoxModel;
javax.swing.JButton;
java.awt.event.ActionListener;
JAVA -PROFESOR: ING. FREIDY NUÑEZ
import
import
import
import
java.awt.event.ActionEvent;
javax.swing.border.CompoundBorder;
javax.swing.JSeparator;
javax.swing.SwingConstants;
public class Biblioteca {
private JFrame frmBiblioteca;
private JTextField text;
private JTable table;
private JFrame frmListaDeLibros;
JButton Agregar;
JComboBox Selec ;
String titulo, original, prestable;
public String getPrestable() {
return prestable;
}
public void setPrestable(String prestable) {
this.prestable = prestable;
}
public boolean isEsOriginal() {
return esOriginal;
}
public void setEsOriginal(boolean esOriginal) {
this.esOriginal = esOriginal;
}
public boolean isSePresta() {
return sePresta;
}
public void setSePresta(boolean sePresta) {
this.sePresta = sePresta;
}
boolean esOriginal;
boolean sePresta;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
JAVA -PROFESOR: ING. FREIDY NUÑEZ
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Biblioteca window = new Biblioteca();
window.frmBiblioteca.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Biblioteca() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings("serial")
private void initialize() {
frmBiblioteca = new JFrame();
frmBiblioteca.setTitle("Biblioteca ");
frmBiblioteca.setBounds(100, 100, 832, 277);
frmBiblioteca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmBiblioteca.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
null, null, null));
panel.setBounds(10, 10, 288, 193);
frmBiblioteca.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Biblioteca ");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
lblNewLabel.setBounds(10, 10, 107, 36);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Nombre del libro: ");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1.setBounds(10, 56, 135, 36);
panel.add(lblNewLabel_1);
text = new JTextField();
text.setBounds(136, 62, 135, 30);
panel.add(text);
text.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("Tipo de libro:");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1_1.setBounds(10, 109, 135, 36);
panel.add(lblNewLabel_1_1);
JAVA -PROFESOR: ING. FREIDY NUÑEZ
Selec = new JComboBox();
Selec.setModel(new DefaultComboBoxModel(new String[]
{"Original", "Fotocopia"}));
Selec.setBounds(136, 119, 135, 21);
panel.add(Selec);
JButton Boton = new JButton("Agregar");
Boton.setFont(new Font("Tahoma", Font.PLAIN, 17));
Boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Biblioteca a = new Biblioteca();
a.setTitulo(text.getText());
a.setOriginal(String.valueOf(Selec.getSelectedItem()));
a.setPrestable(String.valueOf(Selec.getSelectedItem()));
if (a.getOriginal() == "Original") {
a.esOriginal = false;
}else {
a.esOriginal = true;
}
int numCols = table.getModel().getColumnCount();
Object [] fila = new Object[numCols];
fila[0] = a.getTitulo();
fila[1] = a.getOriginal();
fila[2] = a.esOriginal;
((DefaultTableModel) table.getModel()).addRow(fila);
}
});
Boton.setBounds(10, 155, 135, 23);
panel.add(Boton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(new CompoundBorder());
scrollPane.setBounds(323, 10, 469, 193);
frmBiblioteca.getContentPane().add(scrollPane);
table = new JTable();
table.setToolTipText("");
table.setFont(new Font("Tahoma", Font.PLAIN, 16));
table.setModel(new DefaultTableModel(new Object[][] {},new
String[] {"Titulo", "Condicion", "Se puede prestar"}) {
Class[] columnTypes = new Class[] {String.class,
String.class, Boolean.class};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
boolean[] columnEditables = new boolean[] {false,
false, false};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
JAVA -PROFESOR: ING. FREIDY NUÑEZ
});
table.getColumnModel().getColumn(2).setPreferredWidth(106);
scrollPane.setViewportView(table);
table.getColumnModel().getColumn(2).setPreferredWidth(106);
scrollPane.setViewportView(table);
JSeparator separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
separator.setBounds(308, 10, 4, 193);
frmBiblioteca.getContentPane().add(separator);
}
}
6-Crear la clase Triángulo. Debe incluir los siguientes métodos que devuelven un valor
booleano:
a) esEscaleno
b) esIsósceles
c) esEquilátero
d) tieneAnguloRecto
Agregar el código necesario para probarla.
import java.awt.EventQueue;
import
import
import
import
import
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JTabbedPane;
javax.swing.JPanel;
javax.swing.JLabel;
java.awt.Font;
javax.swing.JTextField;
javax.swing.JButton;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
javax.swing.JSeparator;
javax.swing.BoxLayout;
javax.swing.border.TitledBorder;
JAVA -PROFESOR: ING. FREIDY NUÑEZ
public class Triangulo {
JLabel R;
private JFrame frmTringulo;
private JTextField text1;
private JTextField text2;
private JTextField text3;
boolean esEscaleno;
boolean esIsosceles;
boolean esEquilatero;
public boolean isEsEscaleno() {
return esEscaleno;
}
public void setEsEscaleno(boolean esEscaleno) {
this.esEscaleno = esEscaleno;
}
public boolean isEsIsosceles() {
return esIsosceles;
}
public void setEsIsosceles(boolean esIsosceles) {
this.esIsosceles = esIsosceles;
}
public boolean isEsEquilatero() {
return esEquilatero;
}
public void setEsEquilatero(boolean esEquilatero) {
this.esEquilatero = esEquilatero;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Triangulo window = new Triangulo();
window.frmTringulo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Triangulo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JAVA -PROFESOR: ING. FREIDY NUÑEZ
frmTringulo = new JFrame();
frmTringulo.setTitle("Triángulo");
frmTringulo.setBounds(100, 100, 477, 304);
frmTringulo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTringulo.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 427, 247);
frmTringulo.getContentPane().add(panel);
panel.setLayout(null);
text1 = new JTextField();
text1.setFont(new Font("Tahoma", Font.PLAIN, 16));
text1.setBounds(83, 54, 83, 26);
panel.add(text1);
text1.setColumns(10);
JLabel labe1 = new JLabel("Lado 1");
labe1.setFont(new Font("Tahoma", Font.PLAIN, 16));
labe1.setBounds(10, 54, 95, 26);
panel.add(labe1);
JLabel labe = new JLabel("Ingrese la medidas de cada lado");
labe.setFont(new Font("Tahoma", Font.BOLD, 15));
labe.setBounds(10, 10, 255, 26);
panel.add(labe);
JLabel labe2 = new JLabel("Lado 2");
labe2.setFont(new Font("Tahoma", Font.PLAIN, 16));
labe2.setBounds(10, 101, 95, 26);
panel.add(labe2);
JLabel labe3 = new JLabel("Lado 3");
labe3.setFont(new Font("Tahoma", Font.PLAIN, 16));
labe3.setBounds(10, 153, 95, 26);
panel.add(labe3);
text2 = new JTextField();
text2.setFont(new Font("Tahoma", Font.PLAIN, 16));
text2.setColumns(10);
text2.setBounds(83, 101, 83, 26);
panel.add(text2);
text3 = new JTextField();
text3.setFont(new Font("Tahoma", Font.PLAIN, 16));
text3.setColumns(10);
text3.setBounds(83, 153, 83, 26);
panel.add(text3);
JButton btnNewButton = new JButton("Comprobar");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Triangulo t = new Triangulo() ;
String text = "";
int ca = Integer.parseInt(text1.getText());
int cb = Integer.parseInt(text2.getText());
int cc = Integer.parseInt(text3.getText());
JAVA -PROFESOR: ING. FREIDY NUÑEZ
if (ca != cb && cb!= cc) {
t.esEscaleno= true;
text = "El triangulo es: Escaleno";
}else if (ca == cb && cb==cc) {
t.esEquilatero = true;
text = "El triangulo es: Equilatero";
}else if (ca == cb || cb== cc){
t.esIsosceles = true;
text = "El triangulo es: Isosceles";
}
R.setText(text);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnNewButton.setBounds(39, 194, 135, 21);
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(191, 96, 226, 31);
panel.add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
R = new JLabel("");
panel_1.add(R);
R.setFont(new Font("Tahoma", Font.BOLD, 16));
}
}
7. Un club tiene socios.
a) Crear la clase Socio con variables de instancia: nombre, número y variable de Clase:
PróximoNúmero.
b) Agregar los métodos de acceso y modificación
c) Inicializar en 1 el próximo número.
d) Crear un socio, mostrar sus datos
e) Crear otro socio, mostrar sus datos
JAVA -PROFESOR: ING. FREIDY NUÑEZ
JAVA -PROFESOR: ING. FREIDY NUÑEZ
import java.awt.EventQueue;
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.border.TitledBorder;
javax.swing.border.EtchedBorder;
java.awt.Color;
javax.swing.JLabel;
javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Window;
import
import
import
import
import
import
import
import
javax.swing.JTextField;
javax.swing.JButton;
javax.swing.JSpinner;
java.awt.event.ActionListener;
java.util.ArrayList;
java.util.List;
java.awt.event.ActionEvent;
javax.swing.SpinnerNumberModel;
public class Socio {
private JFrame frmClubDeSocios;
private JTextField textNombre;
private JTextField textNumero;
private JTextField NuevoNombre;
private JTextField NuevoNumero;
List<String> nombres = new ArrayList<String>();
List<String> numeros = new ArrayList<String>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Socio window = new Socio();
window.frmClubDeSocios.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Socio() {
initialize();
}
/**
* Initialize the contents of the frame.
JAVA -PROFESOR: ING. FREIDY NUÑEZ
*/
private void initialize() {
frmClubDeSocios = new JFrame();
frmClubDeSocios.setTitle("Club de socios");
frmClubDeSocios.setBounds(100, 100, 585, 287);
frmClubDeSocios.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmClubDeSocios.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new
EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new
Color(160, 160, 160)), "Agregar Miembro", TitledBorder.LEADING,
TitledBorder.TOP, null, new Color(0, 0, 0)));
panel.setBounds(10, 10, 216, 165);
frmClubDeSocios.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNonbre = new JLabel("Nombre:");
lblNonbre.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNonbre.setBounds(10, 25, 73, 41);
panel.add(lblNonbre);
JLabel lblNumero = new JLabel("Numero:");
lblNumero.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNumero.setBounds(10, 76, 73, 41);
panel.add(lblNumero);
textNombre = new JTextField();
textNombre.setFont(new Font("Tahoma", Font.PLAIN, 15));
textNombre.setBounds(93, 32, 96, 31);
panel.add(textNombre);
textNombre.setColumns(10);
textNumero = new JTextField();
textNumero.setFont(new Font("Tahoma", Font.PLAIN, 15));
textNumero.setColumns(10);
textNumero.setBounds(93, 83, 96, 31);
panel.add(textNumero);
JButton ButtonAgregar = new JButton("Agregar");
ButtonAgregar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nombres.add("");
numeros.add("");
nombres.add(textNombre.getText());
numeros.add(textNumero.getText());
JOptionPane.showMessageDialog(panel,"Socio
agregado exitosamente");
}
});
ButtonAgregar.setFont(new Font("Tahoma", Font.PLAIN, 16));
ButtonAgregar.setBounds(49, 127, 96, 21);
panel.add(ButtonAgregar);
JPanel panel_1 = new JPanel();
JAVA -PROFESOR: ING. FREIDY NUÑEZ
panel_1.setBorder(new TitledBorder(new
EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new
Color(160, 160, 160)), "Modificar Miembro", TitledBorder.LEADING,
TitledBorder.TOP, null, new Color(0, 0, 0)));
panel_1.setBounds(250, 10, 311, 223);
frmClubDeSocios.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel lblNonbre_1 = new JLabel("Nuevo nombre:");
lblNonbre_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNonbre_1.setBounds(10, 73, 123, 41);
panel_1.add(lblNonbre_1);
JLabel lblNumero_1 = new JLabel("Nuevo numero:");
lblNumero_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNumero_1.setBounds(10, 124, 123, 41);
panel_1.add(lblNumero_1);
NuevoNombre = new JTextField();
NuevoNombre.setFont(new Font("Tahoma", Font.PLAIN, 15));
NuevoNombre.setColumns(10);
NuevoNombre.setBounds(143, 78, 96, 31);
panel_1.add(NuevoNombre);
NuevoNumero = new JTextField();
NuevoNumero.setFont(new Font("Tahoma", Font.PLAIN, 15));
NuevoNumero.setColumns(10);
NuevoNumero.setBounds(143, 129, 96, 31);
panel_1.add(NuevoNumero);
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(Integer.valueOf(1),
Integer.valueOf(1), null, Integer.valueOf(1)));
spinner.setBounds(143, 41, 96, 21);
panel_1.add(spinner);
JLabel lblNumero_1_1 = new JLabel("Numero actual ");
lblNumero_1_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNumero_1_1.setBounds(10, 29, 135, 41);
panel_1.add(lblNumero_1_1);
JButton ButtonAgregar_1 = new JButton("Modificar");
ButtonAgregar_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nuevo = (int)spinner.getValue();
nombres.set(nuevo, NuevoNombre.getText());
numeros.set(nuevo, NuevoNumero.getText());
JOptionPane.showMessageDialog(panel, "Modificacion
realizada exitosamente");
}
});
ButtonAgregar_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
ButtonAgregar_1.setBounds(85, 179, 123, 21);
panel_1.add(ButtonAgregar_1);
JButton btnNewButton = new JButton("Acceso");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JAVA -PROFESOR: ING. FREIDY NUÑEZ
JOptionPane.showMessageDialog(panel,"\n"+"Nombres:
" + nombres + "\n"+ "Numeros: " + numeros +
"\n","Socios",JOptionPane.PLAIN_MESSAGE);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnNewButton.setBounds(20, 181, 194, 39);
frmClubDeSocios.getContentPane().add(btnNewButton);
}
}
Descargar