Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default Help needed in layout of components

    hi all....i am a newbie in AWT/SWING
    i want to arrange components in my frame like this

    label : text field
    label:text field
    button

    how to do it...i am not getting anywhere even after reading loads of layout tutorials
    my code goes like this
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    
    public class FirstSwing implements ActionListener {
    
       JButton button;
    
       public void go()
       {
    	   JFrame frame=new JFrame();
           frame.setTitle("IWCn");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Toolkit toolkit =  Toolkit.getDefaultToolkit ();
           Dimension size = toolkit.getScreenSize();
           frame.setSize(size.width, size.height-28);
           frame.setLocation(0,0);
           
           Icon icon = new ImageIcon("C:/Users/Shilpa/Desktop/iwcn.png");
           JLabel iwcn= new JLabel(icon);
           frame.getContentPane().add(iwcn,BorderLayout.NORTH);
           
           JPanel panel=new JPanel();
           panel.setLayout(new FlowLayout(FlowLayout.CENTER));
           frame.getContentPane().add(panel);
           
           JLabel userName=new JLabel("Username : ");
           panel.add(userName);
          
           JTextField field = new JTextField(20);
           panel.add(field);
           /*JLabel password=new JLabel("Password");
           panel.add(password);
           JTextField field1 = new JTextField(20);
           panel.add(field1);*/
           
           
           button=new JButton("Submit");
           button.addActionListener(this);
           
           button.setBounds(580,500,100,30);
           JLabel label=new JLabel("\u00a9 Copywright IWCn",JLabel.CENTER);
           frame.getContentPane().add(label,BorderLayout.SOUTH);
           
           
           
      
           frame.setVisible(true);
       }
    
        public static void main(String[] args) {
        	FirstSwing gui=new FirstSwing();
        	gui.go();
    
        } 
        public void actionPerformed(ActionEvent event){
        	button.setText("I have been clicked");
        }
    }

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    here's one way (without fullscreen)

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    
    class FirstSwing implements ActionListener {
    
       JButton button=new JButton("Submit");
    
       public void go()
       {
         JPanel infoPanel = new JPanel(new GridLayout(2,2));
         JLabel userName=new JLabel("Username : ");
         JTextField field = new JTextField(10);
         JLabel password=new JLabel("Password");
         JTextField field1 = new JTextField(10);
         infoPanel.add(userName); infoPanel.add(field);
         infoPanel.add(password); infoPanel.add(field1);
    
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(button);
    
         JPanel holdingPanel = new JPanel(new BorderLayout());
         holdingPanel.add(infoPanel);
         holdingPanel.add(buttonPanel,BorderLayout.SOUTH);
    
         JFrame frame=new JFrame();
         frame.setTitle("IWCn");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(new JLabel(new ImageIcon("C:/Users/Shilpa/Desktop/iwcn.png")),BorderLayout.NORTH);
         frame.getContentPane().add(holdingPanel);
         frame.getContentPane().add(new JLabel("\u00a9 Copywright IWCn",JLabel.CENTER),BorderLayout.SOUTH);
    
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
       }
    
        public static void main(String[] args) {
          FirstSwing gui=new FirstSwing();
          gui.go();
    
        }
        public void actionPerformed(ActionEvent event){
          button.setText("I have been clicked");
        }
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

Similar Threads

  1. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 09:41 AM
  2. Components Layout in a JPanel
    By jboy in forum New To Java
    Replies: 4
    Last Post: 10-11-2009, 01:08 PM
  3. Layout problem / add components from another class
    By ehochedez in forum AWT / Swing
    Replies: 9
    Last Post: 09-02-2009, 11:42 AM
  4. Inconsistent layout w/dynamic resize of components
    By donb2000 in forum AWT / Swing
    Replies: 3
    Last Post: 07-26-2008, 03:40 PM
  5. Help needed with sizing components
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 05:40 AM

Posting Permissions

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