Documente Academic
Documente Profesional
Documente Cultură
Unidad 2
Matrícula: ES172006480
Índice
Introducción ………………………………………………………………………………3
Menús en java ……………………………………………………………………………4
Ventanas en java ………………………………………………………………………...9
Código de la aplicación ……………………………………………………………..….11
Captura de pantalla de la aplicación ………………………………………………….31
Conclusión ……………………………………………………………………………….32
Bibliografía ……………………………………………………………………………….33
Introducción
JMenuBar: “es el elemento principal cuando vamos a crear menús, ya que provee
la barra donde se alojarán cada uno de los ítems u opciones deseadas.” (Henao, C.
tutorial y guía prácticas, lecciones aprendidas en el desarrollo de software)
Estructura algorítmica
object
component
container
JComponent
Crear JMenuBarr no
si
JMenuBar
Sintáctica
barraMenu = new JMenuBar();
barraMenu.add(menuArchivo);
setJMenuBar(barraMenu);
JMenu: “si bien el elemento anterior permite crear una Barra donde alojar el resto
de componentes, el JMenu es quien contiene dichos componentes, a este se le
agregan el resto de opciones, podemos asociarlo con un contenedor el cual aloja
otros elementos como botones, etiquetas, campos entre otros...... el JMenu
permite agregar los elementos o items correspondientes, así como otros JMenus.”
(Henao, C. tutorial y guía prácticas, lecciones aprendidas en el desarrollo de software)
Estructura algorítmica
object
component
container
JComponent
Crear
no
JAbstractBotton
si
si
crear
no
JMenuItem
si
Crear JMenu no
si
jMenu
Sintáctica
menuArchivo = new JMenu();
menuArchivo.add(menuItemNuevo);
menuArchivo.addSeparator();
object
component
container
JComponent
Crear
no
JAbstractBotton
si
crear
no
JMenuItem
si
jMenuItem
Sintáctica
menuItemNuevo = new JMenuItem();
menuItemNuevo.setText("Nuevo");
menuArchivo.add(menuItemNuevo);
object
component
container
JComponent
Crear
no
JAbstractBotton
si
si
crear
no
JMenuItem
si
Crear
JCheckboxMenu no
Item
si
JCheckboxMenuItem
Sintáctica
jCheckMenu1 = new JCheckBoxMenuItem("Check1");
menuOpciones.add(jCheckMenu1);
ventanas en java
JFrame “es una clase utilizada en Swing (biblioteca gráfica) para generar
ventanas sobre las cuales añadir distintos objetos con los que podrá interactuar o
no el usuario. A diferencia de JPanel, JFrame posee algunas nociones típicas de
una ventana como minimizar, cerrar, maximizar y poder moverla.” (Wikipedia
2018).
Sintáctica
import javax.swing.JFrame;
public class Formulario {
public static void main(String[] ar) {
JFrame f=new JFrame();
f.setBounds(10,10,300,200);
f.setVisible(true);
}
}
JDialog: son ventas secundarias debido a que admiten una ventana padre como
JFrame. Aunque ella misma puede ser también ventana padre
Sintáctica
public VentanaConfirmacion(){
contenedor=getContentPane();
contenedor.setLayout(null);
//Asigna un titulo a la barra de titulo
setTitle("CoDejaVu : Titulo Del JDialog");
//tamaño de la ventana
setSize(300,200);
//pone la ventana en el Centro de la pantalla
setLocationRelativeTo(null);
}
}
Sintáctica
Clase principal
package poo2;
public Tabla_marca() {
initComponents();
}
//componente hechos por interface grafica
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel_Tmarca.setText("MARCA");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//implementación de método de la tabla
package poo2;
public Tabla_precio() {
initComponents();
}
// componente hecho por interface grafica
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel_Tprecio.setText("Precio");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addGap(162, 162, 162)
.addComponent(jLabel_Tprecio))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 385,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel_Tprecio)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 283,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>
package poo2;
//se importa el paquete
import javax.swing.table.DefaultTableModel;
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText("TABLA DE VENTAS");
datosventa.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
jScrollPane1.setViewportView(datosventa);
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addGap(165, 165, 165)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>
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(Tabla_ventas.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Tabla_ventas.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Tabla_ventas.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Tabla_ventas.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
}
//</editor-fold>
}
JFrame de menú
package poo2;
public menu() {
initComponents();
}
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenuventa.setText("VENTAS");
jMenuItem_ventas.setText("ventas");
jMenuItem_ventas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem_ventasActionPerformed(evt);
}
});
jMenuventa.add(jMenuItem_ventas);
jMenuBar1.add(jMenuventa);
jMenuinventario.setText("INVENTARIO");
jMenuBar1.add(jMenuinventario);
jMenupersonal.setText("PERSONAL");
jMenuBar1.add(jMenupersonal);
jMenusalir.setText("SALIR");
jMenusalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenusalirActionPerformed(evt);
}
});
jMenuItemsalir.setText("Salir");
jMenuItemsalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemsalirActionPerformed(evt);
}
});
jMenusalir.add(jMenuItemsalir);
jMenuBar1.add(jMenusalir);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
}
private void jMenuItemsalirActionPerformed(java.awt.event.ActionEvent evt) {
// para dar la orde se salir al menu salir
System.exit(0);
}
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(menu.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
}
//</editor-fold>
JFrame de ventas
package poo2;
import java.util.LinkedList;
import javax.swing.JOptionPane;
public ventas() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText("VENTAS");
jLabel2.setText("Articulo");
jLabel3.setText("Precio");
jLabel4.setText("Marca");
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(40, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel3,
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4,
javax.swing.GroupLayout.Alignment.LEADING))
.addGap(26, 26, 26))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGap(13, 13, 13)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addContainerGap(23, Short.MAX_VALUE))
);
txtArticulo.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtArticuloKeyTyped(evt);
}
});
txtMarca.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtMarcaActionPerformed(evt);
}
});
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.LEADING)
.addComponent(txtArticulo)
.addComponent(txtPrecio)
.addComponent(txtMarca, javax.swing.GroupLayout.DEFAULT_SIZE,
111, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(txtArticulo,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtPrecio,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtMarca,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(17, Short.MAX_VALUE))
);
Btnguardar.setText("Guardar");
Btnguardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnguardarActionPerformed(evt);
}
});
Btnvisualizar.setText("Visualizar");
Btnvisualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnvisualizarActionPerformed(evt);
}
});
Btnsalir.setText("Salir");
Btnsalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnsalirActionPerformed(evt);
}
});
Btnlimpiar.setText("Limpiar");
Btnlimpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnlimpiarActionPerformed(evt);
}
});
Btnselecionar.setText("Selecionar");
Btnselecionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnselecionarActionPerformed(evt);
}
});
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addComponent(Btnguardar,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btnvisualizar,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btnsalir,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btnlimpiar,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btnselecionar,
javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(193, 193, 193)
.addComponent(jLabel1)))
.addContainerGap(56, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(Btnguardar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Btnlimpiar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Btnvisualizar)
.addGap(8, 8, 8)
.addComponent(Btnselecionar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btnsalir)
.addGap(0, 26, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
datosventa dv;
for(int i=0; i<contenedor.size(); i++)
{
dv = (datosventa) contenedor.get(i);
if(articuloselec.equalsIgnoreCase(dv.getArticulo()))
{
txtPrecio.setText(dv.getPrecio());
txtMarca.setText(dv.getMarca());
encontrado = i;
break;
}
}
}
}
}
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(ventas.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ventas.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ventas.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ventas.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
}
//</editor-fold>
Muestra una JFame el cual se llama por medio del botón visualizar
Conclusión
Henao, C.. (octubre 8, 2013). Menú Java Swing. marzo 03, 2018, del Sitio web:
http://codejavu.blogspot.mx/2013/09/menus-con-java-swing.html