Sei sulla pagina 1di 14

P.O.O.

1.- Varios panels en un mismo frame import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PVentanas extends JFrame{ JPanel pAltas; JPanel pCons; JLabel lAltas; JLabel lCons; JButton bAltas; JButton bCons; public PVentanas(){ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setLayout(null); setSize(400,300); setLocation(10,30); pAltas = new JPanel(); lAltas = new JLabel("Altas"); pCons = new JPanel(); lCons = new JLabel(); bAltas = new JButton(); bCons = new JButton("Consultas"); add(pAltas); add(pCons); add(bAltas); add(bCons); pAltas.add(lAltas); pCons.add(lCons); inicializar(); } public void inicializar() { pAltas.setBounds(20, 20, 200, 150); pAltas.setLayout(null); pAltas.setBackground(Color.green); lAltas.setBounds(1, 1, 200, 40); lAltas.setHorizontalAlignment(SwingConstants.CENTER); pAltas.setVisible(false);

L.S.C. Lourdes E. Ramrez Fernndez

-1-

P.O.O. 1

pCons.setBounds(25, 25, 200, 150); pCons.setLayout(null); pCons.setBackground(Color.gray); lCons.setBounds(1, 1, 200, 40); lCons.setText("Consultas"); lCons.setHorizontalAlignment(SwingConstants.CENTER); pCons.setVisible(false); bAltas.setBounds(250, 50, 100, 35); bAltas.setText("Altas"); bAltas.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { pCons.setVisible(false); pAltas.setVisible(true); } }); bCons.setBounds(250, 100, 100, 35); bCons.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { pAltas.setVisible(false); pCons.setVisible(true); } });

public static void main(String[] args) { PVentanas ob = new PVentanas(); ob.setVisible(true); } }

L.S.C. Lourdes E. Ramrez Fernndez

-2-

P.O.O. 1

2.- Dibujar y pintar figuras geomtricas


import java.awt.*; import javax.swing.*; public class Figuras extends JFrame { private JPanel jContentPane = null; Font f; public Figuras() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); f= new Font("Arial",Font.ITALIC+Font.BOLD,14); setSize(500, 350); setTitle("Figuras"); jContentPane = new JPanel(); inicializar();

private void inicializar() { jContentPane.setLayout(null); jContentPane.setBackground(Color.gray); }

public void paint(Graphics g){ g.setColor(Color.red); g.drawLine(25,80,475,80); g.setFont(f); g.setColor(Color.DARK_GRAY); g.drawString("Ejemplo de dibujos en Java",145,55); g.setColor(Color.blue); g.drawRect(75,100,75,75); g.setColor(Color.yellow); g.fillOval(250,100,100,55); g.setColor(Color.cyan); g.fillRect(75,250,75,75); g.setColor(Color.MAGENTA); g.drawOval(250,250,100,55); } public static void main(String[] args){ Figuras obj= new Figuras(); obj.setVisible(true);

L.S.C. Lourdes E. Ramrez Fernndez

-3-

P.O.O. 1

3.- Botn y etiqueta // se agrega un botn y una etiqueta import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Bienvenida extends JFrame { private JLabel lSaludo; private JButton bAceptar; public Bienvenida() { super(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setSize(300, 200); setTitle("Primer aplicacion"); setLayout(null); setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12)); lSaludo = new JLabel(); bAceptar = new JButton(); add(lSaludo); add(bAceptar); initialize();

private void initialize() { lSaludo.setBounds(new java.awt.Rectangle(21,36,239,33)); lSaludo.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14)); lSaludo.setText(""); bAceptar.setBounds(new Rectangle(172,106,84,30)); bAceptar.setText("Aceptar"); bAceptar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { lSaludo.setText("Bienvenido a Java Visual"); } });

public static void main(String[] args){ Bienvenida obj= new Bienvenida(); obj.setVisible(true); }

L.S.C. Lourdes E. Ramrez Fernndez

-4-

P.O.O. 1

4.- Aplicando layout: GridLayout, // modificar propiedad layout:GridLaout // se agrega dos botones y una etiqueta import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Saludo extends JFrame { private private private private JLabel etiqueta; JButton bSaludo; JButton bDespedida; String msg;

public Saludo() { super(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(600, 150); setTitle("Ejemplo GridLayout"); GridLayout gridLayout = new GridLayout(); //gridLayout.setRows(3); // una columna,tres renglones gridLayout.setRows(1); // tres columna,un renglones setLayout(gridLayout); etiqueta = new JLabel(); bSaludo = new JButton(); bDespedida = new JButton(); add(etiqueta); add(bSaludo); add(bDespedida); initialize();

msg="Adios"; } private void initialize() { etiqueta.setText(""); etiqueta.setFont(new java.awt.Font("Monotype Corsiva", java.awt.Font.BOLD, 16)); etiqueta.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); bSaludo.setText("Saludo"); bSaludo.setBackground(Color.orange); bSaludo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saludo();
L.S.C. Lourdes E. Ramrez Fernndez -5-

P.O.O. 1

} }); bDespedida.setText("Despedida"); bDespedida.setForeground(Color.blue); bDespedida.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { despedida(); } }); } private void saludo(){ etiqueta.setForeground(java.awt.Color.orange); etiqueta.setText("Bienvenido a Java Visual"); } private void despedida(){ etiqueta.setForeground(java.awt.Color.blue); etiqueta.setText(msg); } public static void main(String [] args){ Saludo obj = new Saludo(); obj.setVisible(true); } }

L.S.C. Lourdes E. Ramrez Fernndez

-6-

P.O.O. 1

5.- Altas y consultas a un arreglo de objetos // consta de 3 clases. public class alumno { String name; double[] calif=new double[3]; public double registro(String n,double c1,double c2) { name=n; calif[0]=c1; calif[1]=c2; calif[2]=(calif[0]+calif[1])/2; return calif[2]; } public String consulta() { String dato=new String(name + " " + calif[2]); return dato; }

// hay que agregar etiquetas,cajas de texto y botones import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Promedio extends JFrame { alumno[] al = new alumno[10]; int x; private private private private private private private private private JLabel lNombre; JLabel lcal1; JLabel lcal2; JTextField tName; JTextField tCal1; JTextField tCal2; JLabel lProm; JButton bRegistro; JButton bConsulta;

public Promedio() { super(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(400, 300); setTitle("Promedios"); setLayout(null);

L.S.C. Lourdes E. Ramrez Fernndez

-7-

P.O.O. 1

lProm = new JLabel(); lcal1 = new JLabel(); lcal2 = new JLabel(); lNombre = new JLabel(); tName = new JTextField(); tCal1 = new JTextField(); tCal2 = new JTextField(); bRegistro = new JButton(); bConsulta = new JButton(); add(lNombre); add(lcal1); add(lcal2); add(tName); add(tCal1); add(tCal2); add(lProm); add(bRegistro); add(bConsulta); initialize(); x=0; } private void initialize() { lProm.setBounds(new java.awt.Rectangle(221,148,144,30)); lProm.setText("Promedio"); lcal2.setBounds(new java.awt.Rectangle(16,125,103,18)); lcal2.setText("Segundo Parcial"); lcal1.setBounds(new java.awt.Rectangle(16,76,103,18)); lcal1.setText("Primer Parcial"); lNombre.setBounds(new java.awt.Rectangle(21,23,76,26)); lNombre.setText("Nombre:"); tName.setBounds(new java.awt.Rectangle(144,27,153,20)); tCal1.setBounds(new java.awt.Rectangle(149,79,59,20)); tCal2.setBounds(new java.awt.Rectangle(149,125,59,20));

bRegistro.setBounds(new java.awt.Rectangle(46,192,104,35)); bRegistro.setText("Registro"); bRegistro.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String n=tName.getText(); double c1=Double.parseDouble(tCal1.getText());
L.S.C. Lourdes E. Ramrez Fernndez -8-

P.O.O. 1

double c2=Double.parseDouble(tCal2.getText()); al[x]=new alumno(); double p=al[x].registro(n,c1,c2); lProm.setText("Promedio = " + p); x++; }); }

bConsulta.setBounds(197,194,99,33); bConsulta.setText("Consulta"); bConsulta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { promCons o = new promCons(); o.setVisible(true); o.mostrar(al,x); } }); } public static void main(String [] args){ Promedio obj = new Promedio(); obj.setVisible(true); } } // hay que agregar una area de texto dentro de un scrollpane import javax.swing.*; public class PromCons extends JFrame { private JScrollPane jScrollPane; private JTextArea jTextArea; public PromCons() { super(); setSize(300, 200); setTitle("JFrame"); setLayout(null); jScrollPane = new JScrollPane(); jTextArea = new JTextArea(); add(jScrollPane); add(jTextArea); initialize();

L.S.C. Lourdes E. Ramrez Fernndez

-9-

P.O.O. 1

public void mostrar(alumno[] al, int x){ jTextArea.setText("\n"); for(int y=0;y<x;y++) { String dato=al[y].consulta(); jTextArea.append(dato + "\n"); } } private void initialize() { jScrollPane.setBounds(30,32,173,111); jScrollPane.setViewportView(jTextArea); }

L.S.C. Lourdes E. Ramrez Fernndez

- 10 -

P.O.O. 1

6.- Varios panels en una misma pantalla import javax.swing.*; public class principal extends JFrame { private private private private private private private private private private private private private private JPanel jContentPane = null; JPanel altas = null; JButton jButton = null; //altas JLabel jLabel = null; JLabel jLabel1 = null; JLabel jLabel2 = null; JButton jButton1 = null; //ok-altas JPanel bajas = null; // " JTextField jTextField = null; JButton jButton2 = null; //bajas JTextField jTextField1 = null; JTextField jTextField2 = null; JTextField jTextField3 = null; JLabel jLabel3 = null;

public principal() { super(); initialize(); } private void initialize() { this.setSize(501, 300); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getAltas(), null); jContentPane.add(getBajas(), null); jContentPane.add(getJButton(), null); jContentPane.add(getJButton2(), null); } return jContentPane; } private JPanel getAltas() { if (altas == null) { jLabel2 = new JLabel(); jLabel2.setBounds(new java.awt.Rectangle(17,90,84,16)); jLabel2.setText("calificacion");
L.S.C. Lourdes E. Ramrez Fernndez - 11 -

P.O.O. 1

} return altas;

jLabel1 = new JLabel(); jLabel1.setBounds(new java.awt.Rectangle(17,63,84,15)); jLabel1.setText("nombre"); jLabel = new JLabel(); jLabel.setText("matricula"); jLabel.setBounds(new java.awt.Rectangle(16,29,84,16)); altas = new JPanel(); altas.setLayout(null); altas.setBounds(new java.awt.Rectangle(89,7,309,203)); altas.setVisible(false); altas.add(jLabel, null); altas.add(jLabel1, null); altas.add(jLabel2, null); altas.add(getJTextField1(), null); altas.add(getJTextField2(), null); altas.add(getJTextField3(), null);

private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new java.awt.Rectangle(11,18,62,34)); jButton.setText("altas"); jButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //System.out.println("actionPerformed()"); altas.setVisible(true); } }); } return jButton; } private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setBounds(new java.awt.Rectangle(64,132,67,28)); jButton1.setText("ok"); } return jButton1; } private JPanel getBajas() { if (bajas == null) { jLabel3 = new JLabel(); jLabel3.setBounds(new java.awt.Rectangle(13,35,81,20)); jLabel3.setText("matricula");
L.S.C. Lourdes E. Ramrez Fernndez - 12 -

P.O.O. 1

bajas = new JPanel(); bajas.setLayout(null); bajas.setSize(new java.awt.Dimension(300,203)); bajas.add(getJTextField(), null); bajas.add(jLabel3, null); bajas.setBounds(new java.awt.Rectangle(89,7,308,203)); bajas.setVisible(false); bajas.add(getJButton1(), null); } return bajas; } private JTextField getJTextField() { if (jTextField == null) { jTextField = new JTextField(); jTextField.setBounds(new java.awt.Rectangle(112,44,95,20)); } return jTextField; } private JButton getJButton2() { if (jButton2 == null) { jButton2 = new JButton(); jButton2.setBounds(new java.awt.Rectangle(11,68,71,29)); jButton2.setText("bajas"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //System.out.println("actionPerformed()"); altas.setVisible(false); bajas.setVisible(true); } }); } return jButton2;

private JTextField getJTextField1() { if (jTextField1 == null) { jTextField1 = new JTextField(); jTextField1.setBounds(new java.awt.Rectangle(127,30,123,15)); } return jTextField1; } private JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new JTextField();
L.S.C. Lourdes E. Ramrez Fernndez - 13 -

P.O.O. 1

jTextField2.setBounds(new java.awt.Rectangle(132,62,147,17)); } return jTextField2; } private JTextField getJTextField3() { if (jTextField3 == null) { jTextField3 = new JTextField(); jTextField3.setBounds(new java.awt.Rectangle(132,91,75,15)); } return jTextField3; } }

L.S.C. Lourdes E. Ramrez Fernndez

- 14 -