Results 1 to 4 of 4
  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    27
    Rep Power
    0

    Question Help with components layout

    Hi ppl;

    I have a problem how the applet is opening, every component are in strange psositions, any help please?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    import javax.swing.ButtonGroup;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.ListSelectionModel;
    
    
    
    public class teste extends JFrame implements ActionListener
    {
     private JTextField t1;
     private JLabel l1, l2, l3, l4;
     private JPanel p1,p2,p3,p4, p5;
     private JRadioButton rb1, rb2;
     private JCheckBox cb1,cb2,cb3;
     private ButtonGroup grupo;
     private JTextArea ta1;
     private JButton bt1, bt2;
     
     
     public void init()
     {
      setLayout(new GridLayout(5,2));
      //1º linha
      p1 = new JPanel();
      p1.setLayout(new GridLayout(1,2));
      l1 = new JLabel("Nome");
      t1 = new JTextField(10);
      p1.add(l1);
      p1.add(t1);
      
      //2ª linha
      p2 = new JPanel();
      p2.setLayout(new GridLayout(1,2));
      l2 = new JLabel("Género");
      rb1 = new JRadioButton("Maculino");
      rb1.addActionListener(this);
      rb2 = new JRadioButton("Feminino");
      rb2.addActionListener(this);
      p2.add(l2);
      p2.add(rb1);
      p2.add(rb2);
      ButtonGroup grupo=new ButtonGroup();
      grupo.add(rb1);
      grupo.add(rb2);
      
      //3ª linha
      p3 = new JPanel();
      p3.setLayout(new GridLayout(1,2));
      l3 = new JLabel("Passatempos");
      cb1 = new JCheckBox("Ler");
      cb2 = new JCheckBox("Jogar");
      cb3 = new JCheckBox("Viajar");
      p3.add(l3);
      p3.add(cb1);
      p3.add(cb2);
      p3.add(cb3);
      
      //4ª Linha
      p4 = new JPanel();
      p4.setLayout(new GridLayout(1,2));
      l4 = new JLabel("Morada");
      ta1 = new JTextArea(5,20);
      p4.add(l4);
      p4.add(ta1);
      
      //5ª linha
      p5 = new JPanel();
      p5.setLayout(new GridLayout(1,2));
      bt1 = new JButton("Limpar");
      bt1.addActionListener(this);
      bt2 = new JButton("Ver +");
      bt2.addActionListener(this);
      p5.add(bt1);
      p5.add(bt2);
      
      //Componentes na applet
      add(p1);
      add(p2);
      add(p3);
      add(p4);
      add(p5);
     }
    
    
     @Override
     public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      
     }
    }

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Help with components layout

    Quote Originally Posted by alphasil View Post
    I have a problem how the applet is opening,
    What Applet?

    every component are in strange psositions
    They will be where the layout managers you use place them. Have you gone through Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing) ?

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  3. #3
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help with components layout

    Sorry, instead of JFrame is JApplet.

    But i have already saw that site but can't put all together and organized.

    I want to have 5 rows and 2 columns and each cell has one component.


    any help to resolve this issue?


    regards

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Help with components layout

    5 rows and 2 columns sounds like a GridLayout(5, 2) -- which you already have, but the components you are adding to this layout are panels each with its own GridLayout(1, 2) -- which will give you 4 components across.

    Just reading may not be enough; you need to actually do the exercises in the layout tutorial trail.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. layout struts 2 forms components vertically
    By saleh_neu in forum Web Frameworks
    Replies: 1
    Last Post: 01-12-2011, 09:30 AM
  2. Help needed in layout of components
    By silversurfer2in in forum AWT / Swing
    Replies: 2
    Last Post: 05-27-2010, 11:50 AM
  3. Components Layout in a JPanel
    By jboy in forum New To Java
    Replies: 4
    Last Post: 10-11-2009, 12:08 PM
  4. Layout problem / add components from another class
    By ehochedez in forum AWT / Swing
    Replies: 9
    Last Post: 09-02-2009, 10:42 AM
  5. Inconsistent layout w/dynamic resize of components
    By donb2000 in forum AWT / Swing
    Replies: 3
    Last Post: 07-26-2008, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •