|
| 1 | +packagecom.applikdos; |
| 2 | + |
| 3 | +importjava.awt.event.ActionEvent; |
| 4 | +importjava.awt.event.ActionListener; |
| 5 | + |
| 6 | +importjavax.swing.*; |
| 7 | + |
| 8 | +publicclassFormularioEventosextendsJFrameimplementsActionListener { |
| 9 | + |
| 10 | +JButtonboton1,boton2; |
| 11 | + |
| 12 | +publicstaticvoidmain(String[]args) { |
| 13 | +newFormularioEventos(); |
| 14 | +} |
| 15 | + |
| 16 | +publicFormularioEventos() { |
| 17 | +setTitle("Formulario"); |
| 18 | +setSize(550,250); |
| 19 | +setLayout(null); |
| 20 | +setVisible(true); |
| 21 | + |
| 22 | +boton1 =newJButton(); |
| 23 | +boton1.setText("Aceptar"); |
| 24 | +boton1.setBounds(150,100,100,30); |
| 25 | +getContentPane().add(boton1); |
| 26 | +boton1.addActionListener(this); |
| 27 | + |
| 28 | +boton2 =newJButton(); |
| 29 | +boton2.setText("Cerrar"); |
| 30 | +boton2.setBounds(260,100,100,30); |
| 31 | +getContentPane().add(boton2); |
| 32 | +boton2.addActionListener(this); |
| 33 | +} |
| 34 | + |
| 35 | +publicvoidactionPerformed(ActionEventevento){ |
| 36 | + |
| 37 | +if(evento.getSource()==boton1){ |
| 38 | +JOptionPane.showMessageDialog(null,"Este es el boton 1"); |
| 39 | +} |
| 40 | + |
| 41 | +if(evento.getSource()==boton2){ |
| 42 | +JOptionPane.showMessageDialog(null,"Este es el boton 2"); |
| 43 | +} |
| 44 | + |
| 45 | +} |
| 46 | +} |