Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My first UI 1

Status
Not open for further replies.

chals1

Technical User
Sep 3, 2003
73
ES
I don't figure out where's the bug on this code.
It doesn't show anything at all.

Code:
import java.awt.*;
import java.awt.event.*;

public class ventana extends Frame {

   Label texto1 = new Label("texto-1",Label.CENTER);
   Label texto2 = new Label("texto-2",Label.CENTER);
   Label texto3 = new Label("texto-3",Label.CENTER);

   TextField t1 = new TextField();
   TextField t2 = new TextField();
   TextField t3 = new TextField();

   Button b1 = new Button("añadir");
   Button b2 = new Button("borrar");

   TextArea area = new TextArea();


   public ventana() {
      super("Interfaz gráfico de usuario");

      Panel panel1 = new Panel();
      Panel panel2 = new Panel();
      add(panel1);
      add(panel2);

      panel1.setLayout(new GridLayout(3,2));
      panel1.add(t1);
      panel1.add(texto1);
      panel1.add(t2);
      panel1.add(texto2);
      panel1.add(t3);
      panel1.add(texto3);

      add(area);

      panel2.setLayout(new GridLayout(2,1));
      panel2.add(b1);  b1.addActionListener(new GestionaComponentes());
      panel2.add(b2);  b2.addActionListener(new GestionaComponentes());

      addWindowListener(new GestionaVentana());
   }


   class GestionaVentana extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }
   }

   class GestionaComponentes implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         Object fuente = e.getSource();
         if (fuente == b1) {
            //El botón b1 causó el evento

            }
         else {
            //El botón b2 causó el evento
         }

      }
   }










   public static void main(String args[]) {
      new ventana().show();
      }
}





 
Code:
import java.awt.*;
import java.awt.event.*;

public class ventana extends Frame {

   Label texto1 = new Label("texto-1",Label.CENTER);
   Label texto2 = new Label("texto-2",Label.CENTER);
   Label texto3 = new Label("texto-3",Label.CENTER);

   TextField t1 = new TextField();
   TextField t2 = new TextField();
   TextField t3 = new TextField();

   Button b1 = new Button("anadir");
   Button b2 = new Button("borrar");

   TextArea area = new TextArea();


   public ventana() {
      super("Interfaz grafico de usuario");
      setLayout(new GridLayout(3,1));

      Panel panel1 = new Panel();
      Panel panel2 = new Panel();

      add(panel1);
      add(panel2);

      panel1.setLayout(new GridLayout(3,2));
      panel1.add(t1);
      panel1.add(texto1);
      panel1.add(t2);
      panel1.add(texto2);
      panel1.add(t3);
      panel1.add(texto3);

      add(area);

      panel2.setLayout(new GridLayout(2,1));

      ActionListener GestionaComponentes = new ActionListener()
      {
       public void actionPerformed(ActionEvent e) 
        {
         Object fuente = e.getSource();
         if (fuente == b1) 
            {
            //El boton b1 causo el evento

            }
         else {
            //El boton b2 causo el evento
              }
        }
      };
      
      panel2.add(b1); 
      b1.addActionListener(GestionaComponentes);
      panel2.add(b2);
      b2.addActionListener(GestionaComponentes);

      addWindowListener(new GestionaVentana());
   }
   public static void main(String args[]) {
      ventana ventanaObj = new ventana();
      ventanaObj.setSize(400, 500);
      //ventanaObj.pack();
      ventanaObj.show();
      }
}

   class GestionaVentana extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }
   }
within another class which is a WindowAdapter GestionaVentana or an ActionListener, they do not know button b1. They know the event only.
I use inner class GestionaComponentes, it can recognize button b1 and b2.

Please Click the link: Thank chals1 for this valuable post!

 
The gui doesn't look as i expected.
I want the textfields on a column located at the left
and another column side by side with their labels
All that inside panel1 under GridLayout(3,2)

Later, i added to the main window by flowlayout a textarea to be placed onto the middle of the window.

Finally i added a column with 2 botons (panel2 under GridLayout(2,1) ) to the top most left corner of main window.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top