Вы находитесь на странице: 1из 4

MEXICO

PROGRAMACIN ORIENTADA A OBJETOS 2


ACTIVIDAD 3
UNIDAD 2
MENS Y VENTANAS
FACILITADOR (A): JEANETTE CORINA CASTAEDA CORRAL
ALUMNO (A): DIXIE ROSARIO CENICEROS ALVARADO
AL12501794

17 de julio de 2014
Ahora que aprendiste todas las caractersticas de funcionamiento para los mens y
ventanas, ests listo para realizar las siguientes instrucciones. Propsito: Constru
ir mens
y ventanas grficas, as como identificar su declaracin e implementar su m
anipulacin.
Instrucciones:
1. Identifica los diferentes tipos de mens y ventanas grficas vistas en el desarro
llo de la
unidad.
2. Identifica su estructura algortmica (pasos) y sintctica (cdigo).
3. Crea un proyecto en NetBeans, que contenga un programa donde apliq
ues todos los
contenedores y controles identificados en los puntos 1 y 2.
.

Descripcin del cdigo


package poo2_u2_a3_dica;
import
import
import
import
import
/**
*

java.awt.Container;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.io.File;
javax.swing.*;

* @author dx
*/
public class POO2_U2_A3_DICA extends JFrame implements ActionListener{
private JMenuBar barra;
private JMenu elemento1,elemento2;
private JMenuItem sub1,sub2,sub3,sub4;
public POO2_U2_A3_DICA() {
setLayout(null);
barra=new JMenuBar();
setJMenuBar(barra);
elemento1=new JMenu("Opciones");
barra.add(elemento1);
sub1=new JMenuItem("Abrir Ventana");
sub1.addActionListener(this);
elemento1.add(sub1);
sub2=new JMenuItem("Menu Emergente");
sub2.addActionListener(this);
elemento1.add(sub2);
sub3=new JMenuItem("Salir");
sub3.addActionListener(this);
elemento1.add(sub3);
elemento2=new JMenu("JFileChooser");
barra.add(elemento2);
sub4=new JMenuItem("File");
sub4.addActionListener(this);
elemento2.add(sub4);
}
@Override
public void actionPerformed(ActionEvent e) {
Container f=this.getContentPane();
if (e.getSource()==sub1) {
Ventana2 vtn =new Ventana2();
vtn.Ventana2();
}
if (e.getSource()==sub2) {
int a = JOptionPane.showConfirmDialog(null,"Deseas continuar");
String b = JOptionPane.showInputDialog("Ingresa un dato");
Object[] valores = { "Uno", "Dos", "Tres" };
Object valorElegido = JOptionPane.showInputDialog(null,"Elige",
"Entrada",
JOptionPane.INFORMATION_MESSAGE, null,valores, valores[0])
;
JOptionPane.showMessageDialog(null, "Mensaje", "Cuidado",
JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, "Algo fallo", "Error",
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Sin icono", "Plano",
JOptionPane.PLAIN_MESSAGE);
Object[] opciones = { "Yes", "No" };
JOptionPane.showOptionDialog(null, "Presiona Yes Para continuar",
"Selecciona",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,null, opciones, opciones[0]);
}

if (e.getSource()==sub3) {
System.exit(0);
}
if (e.getSource()==sub4) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
File file = fc.getSelectedFile();
System.out.print(file);
}
}
public static void main(String[] ar) {
POO2_U2_A3_DICA frame =new POO2_U2_A3_DICA();
frame.setBounds(50,50,300,200);
frame.setVisible(true);
}
}
package poo2_u2_a3_dica;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import
import
import
import

javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JMenuItem;
javax.swing.JPopupMenu;

/**
*
* @author dx
*/
public class Ventana2 {
JPopupMenu popup = new JPopupMenu();
public void Ventana2(){
JFrame frame = new JFrame("POO2_U2_A2_DICA");
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e){ System.exit(0);
} });
JMenuItem menu0 = new JMenuItem("Esto es un Menu Poppup");
JMenuItem menu1 = new JMenuItem("Menu Poppup");
JMenuItem menu2 = new JMenuItem("Item del Menu Poppup");
popup.add(menu0);popup.add(menu1); popup.add(menu2);
frame.add(popup, BorderLayout.LINE_START);
JLabel lbl1 = new JLabel();
lbl1.setText("Aqui hay un menu poppup");
frame.add(lbl1, BorderLayout.LINE_END);
}

public void dispose() {


throw new UnsupportedOperationException("Not supported yet."); //To chan
ge body
of generated methods, choose Tools | Templates.
}
}
Cdigo de colores:
Amarillo. Todas las libreras a importar
Verde lima. Se declaran los componentes a usar.
Turquesa. Se declara el JFrame y se le integran los JPanel para saber dnde va
cada uno.
Fucsia. Configuracin de los diferentes tipos de mens y componentes
Gris. Se cierra el JFrame invocado pero no termina el programa
Morado. Creamos objetos e invocamos la clase

Вам также может понравиться