Ejemplos Interfaces Usuario 3

2,095 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Ejemplos Interfaces Usuario 3

  1. 1. ******************************************************************************************* EJEMPLO Menús ******************************************************************************************* public class MiVentana extends javax.swing.JFrame { /** Creates new form UnEjemplo */ public MiVentana() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { jPopupMenu1 = new javax.swing.JPopupMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem(); jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem(); jSeparator1 = new javax.swing.JSeparator(); jMenuItem2 = new javax.swing.JMenuItem(); jMenu1 = new javax.swing.JMenu(); jMenuItem3 = new javax.swing.JMenuItem(); jTextArea1 = new javax.swing.JTextArea(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu2 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem(); jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem(); jSeparator2 = new javax.swing.JSeparator(); jMenuItem4 = new javax.swing.JMenuItem(); jMenu4 = new javax.swing.JMenu(); jMenuItem6 = new javax.swing.JMenuItem(); jMenu3 = new javax.swing.JMenu(); jMenuItem1.setText(quot;Unoquot;); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jPopupMenu1.add(jMenuItem1); jCheckBoxMenuItem1.setText(quot;Dosquot;); jPopupMenu1.add(jCheckBoxMenuItem1); jRadioButtonMenuItem1.setText(quot;Tresquot;); jPopupMenu1.add(jRadioButtonMenuItem1); jPopupMenu1.add(jSeparator1); 1
  2. 2. jMenuItem2.setText(quot;Cuatroquot;); jPopupMenu1.add(jMenuItem2); jMenu1.setText(quot;Otrosquot;); jMenuItem3.setText(quot;Cincoquot;); jMenu1.add(jMenuItem3); jPopupMenu1.add(jMenu1); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTextArea1MouseClicked(evt); } }); getContentPane().add(jTextArea1, java.awt.BorderLayout.CENTER); jMenu2.setText(quot;Inglesquot;); jMenu2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenu2ActionPerformed(evt); } }); jMenuItem5.setText(quot;Onequot;); jMenuItem5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem5ActionPerformed(evt); } }); jMenu2.add(jMenuItem5); jCheckBoxMenuItem2.setText(quot;Towquot;); jMenu2.add(jCheckBoxMenuItem2); jRadioButtonMenuItem2.setText(quot;Threequot;); jMenu2.add(jRadioButtonMenuItem2); jMenu2.add(jSeparator2); jMenuItem4.setText(quot;Fourquot;); jMenu2.add(jMenuItem4); jMenu4.setText(quot;Menuquot;); 2
  3. 3. jMenuItem6.setText(quot;Fivequot;); jMenu4.add(jMenuItem6); jMenu2.add(jMenu4); jMenuBar1.add(jMenu2); jMenu3.setText(quot;Francesquot;); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); pack(); } private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jTextArea1.replaceSelection(quot;ONEquot;); } private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: System.out.println(quot;Menú en inglésquot;); } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jTextArea1.replaceSelection(quot;UNOquot;); } private void jTextArea1MouseClicked(java.awt.event.MouseEvent evt) { // Add your handling code here: if (evt.getButton()==java.awt.event.MouseEvent.BUTTON3) { this.jPopupMenu1.show(this.jTextArea1, evt.getX(), evt.getY()); } } /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { new MiVentana ().show(); } // Variables declaration - do not modify private javax.swing.JSeparator jSeparator2; 3
  4. 4. private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenu jMenu2; private javax.swing.JMenuItem jMenuItem5; private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenu jMenu4; private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenu jMenu3; private javax.swing.JMenuItem jMenuItem6; private javax.swing.JTextArea jTextArea1; private javax.swing.JSeparator jSeparator1; private javax.swing.JMenu jMenu1; private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2; private javax.swing.JMenuBar jMenuBar1; // End of variables declaration } 4
  5. 5. ******************************************************************************************* EJEMPLO Pintar-Imagen-Animación ******************************************************************************************* public class MiVentana extends javax.swing.JFrame { private MiPanel jPanel2; Thread t; /** Creates new form MiVentana */ public MiVentana() { initComponents(); jPanel2 = new MiPanel(); getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER); jPanel2.setBackground(java.awt.Color.yellow); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jPanel1.setBackground(java.awt.Color.orange); jButton1.setText(quot;elipsequot;); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton2.setText(quot;rectanguloquot;); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2); jButton3.setText(quot;imagenquot;); 5
  6. 6. jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPanel1.add(jButton3); jButton4.setText(quot;movimientoquot;); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jPanel1.add(jButton4); getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH); pack(); } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jPanel2.figura=quot;movimientoquot;; if (t==null) { t = new Thread() { public void run() { while (true) { try { Thread.currentThread().sleep(1000); } catch (Exception e) {} jPanel2.repaint(); } } }; t.start(); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jPanel2.figura=quot;rectanguloquot;; this.jPanel2.repaint(); // Esto también funciona, aunque mejor con repaint pues // envía el evento a la cola // this.jPanel2.paint(this.jPanel2.getGraphics()); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: 6
  7. 7. this.jPanel2.figura=quot;rectanguloquot;; this.jPanel2.repaint(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jPanel2.figura=quot;elipsequot;; this.jPanel2.repaint(); } /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { new MiVentana().show(); } // Variables declaration - do not modify private javax.swing.JButton jButton2; private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JPanel jPanel1; private javax.swing.JButton jButton4; // End of variables declaration } class MiPanel extends javax.swing.JPanel { String figura = quot;elipsequot;; public void paintComponent (java.awt.Graphics g) { super.paintComponent(g); // pinta el background java.awt.Dimension dim = this.getSize(); int ancho = dim.width; int alto = dim.height; if (figura==quot;rectanguloquot;) { g.fillRect(ancho/4, alto/4, ancho/2, alto/2); } else if (figura==quot;elipsequot;) { g.fillOval(ancho/4, alto/4, ancho/2, alto/2); } else if (figura==quot;imagenquot;) { java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); try { java.awt.Image imagen = toolkit.getImage(new java.net.URL(quot;http://www.netbeans.org/images/logo.gifquot;)); // La imagen aparecerá distorsionada. Si se quiere utilizar su tamaño real // se deberán utilizar quot;g.getWidth(this)quot; y quot;g.getHeight(this)quot; g.drawImage(imagen, ancho/4, alto/4, ancho/2, alto/2, this); } catch (java.net.MalformedURLException e) { 7
  8. 8. System.out.println(quot;URL mal formada: quot; + e); } } else if (figura==quot;movimientoquot;) { g.fillRect(x, alto/4, ancho/2, alto/2); if (x>this.getSize().width) x=0; x=x+10; cal = java.util.Calendar.getInstance(); hora = cal.getTime(); g.drawString(formateo.format(hora), x, alto/4-5); } } private int x; private java.util.Date hora; private java.util.Calendar cal; private java.text.DateFormat formateo = java.text.DateFormat.getTimeInstance(); } 8
  9. 9. ******************************************************************************************* EJEMPLO Animación-Timers (alternativa al EJEMPLO Pintar-Imagen-Animación) ******************************************************************************************* private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: this.jPanel2.figura=quot;movimientoquot;; javax.swing.Timer timer = new javax.swing.Timer(1000, new java.awt.event.ActionListener() { public void actionPerformed (java.awt.event.ActionEvent e) { jPanel2.repaint(); } }); timer.start(); } 9
  10. 10. ******************************************************************************************* EJEMPLO Movimientos-Ratón ******************************************************************************************* /* Este programa es un ejemplo de usos de la interface MouseMotionListener. En la zona amarilla se puede quot;arrastrar el ratonquot;, creandose un rectangulo. En la zona verde se puede mover el raton (sin arrastrar), apareciendo un mensaje, o bien se puede arrastrar el boton. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MiVentana extends JFrame { int xInitC; int yInitC; int xInit; int yInit; int xInitB; int yInitB; JPanel cuadro; JButton button; JPanel panel; public MiVentana() { setTitle(quot;Ejemplo de movimientos de ratonquot;); getContentPane().setLayout(new GridLayout(2,1)); cuadro=new JPanel(); cuadro.setBackground(Color.yellow); getContentPane().add(cuadro); panel=new JPanel(); panel.setLayout(null); panel.setBackground(Color.green); getContentPane().add(panel); button = new JButton(); button.setLabel(quot;buttonquot;); panel.add(button); button.setBounds(20,50,90,42); cuadro.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { xInitC=e.getX(); yInitC=e.getY(); } public void mouseReleased(MouseEvent e) { Graphics g =cuadro.getGraphics(); g.fillRect(xInitC, yInitC, (e.getX()-xInitC), (e.getY()-yInitC)); } }); cuadro.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g =cuadro.getGraphics(); cuadro.paint(g); // pinta background g.drawRect(xInitC, yInitC, (e.getX()-xInitC), (e.getY()-yInitC)); } 10
  11. 11. }); button.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { xInitB=button.getBounds().x; yInitB=button.getBounds().y; xInit=e.getX(); yInit=e.getY(); System.out.println(quot;Evento(1):quot; + e); } }); button.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { button.setBounds(xInitB+e.getX()-xInit, yInitB+e.getY()-yInit, button.getBounds().width, button.getBounds().height); xInitB+=e.getX()-xInit; yInitB+=e.getY()-yInit; } }); panel.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { Graphics g = panel.getGraphics(); panel.paint (g); // pinta background g.drawString(quot;Raton en posicion (quot; + e.getX() + quot;,quot; + e.getY() + quot;) del panel ...quot;, 10, 20); } }); } static public void main(String args[]) { MiVentana v = new MiVentana(); v.setSize(405,305); v.setVisible(true); } } 11
  12. 12. ****************************************************************************************** EJEMPLO Applets ******************************************************************************************* public class MiApplet2 extends javax.swing.JApplet { StringBuffer texto; MiPanel panel; public void añadirTexto (String s) { texto.append(s); repaint(); } public void init () { System.out.println(quot;INIT ....quot;); añadirTexto (quot;init ...quot;); } public void start () { System.out.println(quot;START ....quot;); añadirTexto (quot;start ...quot;); } public void stop () { System.out.println(quot;STOP ....quot;); añadirTexto (quot;stop ...quot;); } public void destroy () { System.out.println(quot;DESTROY ....quot;); añadirTexto (quot;destroy ...quot;); } /** Creates new form MiApplet2 */ public MiApplet2() { initComponents(); texto = new StringBuffer(); panel = new MiPanel(); getContentPane().add(panel, java.awt.BorderLayout.CENTER); panel.setBackground(java.awt.Color.orange); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu1.setText(quot;Menuquot;); jMenuItem1.setText(quot;Itemquot;); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); } 12
  13. 13. // Variables declaration - do not modify private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; // End of variables declaration class MiPanel extends javax.swing.JPanel { public void paintComponent(java.awt.Graphics g) { super.paintComponent(g); g.drawString(texto.toString(), 20, 20); } } } 13

×