Results 1 to 4 of 4
  1. #1
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default Not understanding panels and layout?

    I'm not understanding how to set up panels and frames to display objects. My homework problem is to display a numeric keypad, above the keypad show a label that display the numbers selected, to the right of the keypad is another button to clear display. It says to user a border layout to manage overall presentation and grid layout to manage keypad buttons. Put a border around keypad buttons and a border around the display.

    I'm not really understanding what's going on. I've tried to set this up from examples online and in the book, but I'm lost now.

    I had originally gotten the grid with the keypad but adding the border layout to everything has confused me? This is what I've ended up with. Can anyone help me sort it out?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class KeyPadPanel extends JFrame implements ActionListener
    {
       JFrame window = new JFrame();
    	window.setBorder (BorderFactory.createLineBorder (Color.black, 2));
    	JButton b1 = new JButton ("1");
    	JButton b2 = new JButton ("2");
    	JButton b3 = new JButton ("3");
    	JButton b4 = new JButton ("4");
    	JButton b5 = new JButton ("5");
    	JButton b6 = new JButton ("6");
    	JButton b7 = new JButton ("7");
    	JButton b8 = new JButton ("8");
    	JButton b9 = new JButton ("9");
    	JButton bPound = new JButton ("#");
    	JButton b0 = new JButton ("0");
    	JButton bStar = new JButton ("*");
    	JButton bClear = new JButton("Clear");
    	JTextField tf = new JTextField();
    	
       public KeyPadPanel()
    	{
    	   window.setSize(300,300);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setLayout(new BorderLayout ());
    		
    		JPanel KeyPadPanel = new JPanel(new GridLayout(4,3));
    	   KeyPadPanel.setBorder(lineBorder);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    		b5.addActionListener(this);
    		b6.addActionListener(this);
    		b7.addActionListener(this);
    		b8.addActionListener(this);
    		b9.addActionListerner(this);
    		bPound.addActionListener(this);
    		b0.addActionListener(this);
    		bStar.addActionListner(this);
    		
          KeyPadPanel.add (b1);
    		KeyPadPanel.add (b2);
    		KeyPadPanel.add (b3);
    		KeyPadPanel.add (b4);
    		KeyPadPanel.add (b5);
    		KeyPadPanel.add (b6);
    		KeyPadPanel.add (b7);
    		KeyPadPanel.add (b8);
    		KeyPadPanel.add (b9);
    		KeyPadPanel.add (bPound);
    		KeyPadPanel.add (b0);
    		KeyPadPanel.add (bStar);
    		
    	   JPanel Clear = new JPanel();
    		bClear.addActionListener(this);
    		clear.add(bClear);
    		
    		JPanel Display = new JPanel();
    		JLabel label = new JLabel();
    		label.add(tf);
    		display.add(label);
    		window.add(Display, BorderLayout.NORTH);
    		window.add(KeyPadPanel, BorderLayout.West);
    		window.add(Clear, BorderLayout.East);
    		
    		window.setVisible(true);
    	}	
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by lost_soul View Post
    I'm not really understanding what's going on.

    when you extend a JFrame you don't have to instantiate it. you can instantiate the class KeyPadPanel from an other class within a main method. and afaik there is no setBorder method in the class JFrame. give it a try and let us now when you get in troubles.

  4. #4
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    I deconstructed what I had. Another example in my text uses a seperate program to what it calls instaniates and displays the panels. I would create a new frame in the new program and then add the panel from the keypad program to the frame? I've got frame to display doing it this way.

    Something like this?

    Java Code:
    import javax.swing.*;
    
    
    public class KeyPadDemo
    {
    
       public static void main (String[] args)
    	{
          JFrame frame = new JFrame ("KeyPad Demo");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		
    		KeyPadPanel panel = new KeyPadPanel();
    	
    		
    		frame.getContentPane().add(panel);
    		frame.pack();
    		frame.setVisible(true);	
    	}
    	
    }

    I just don't understand how to make the border panel and grid panels. The way it is now the clear and display are part of the grid but they should be outside of the grid? I don't know how to set it up in that way? Do I need a seperate public method for each type of layout or are they somehow nested? I'm not quite sure how to get the border panel outside of the grid panel?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    	
    public class KeyPadPanel extends JPanel
    {
       private JTextField textfield;
     
    	 public KeyPadPanel()
    	 {
    	    JButton b1 = new JButton ("1");
    	    JButton b2 = new JButton ("2");
           JButton b3 = new JButton ("3");
    	    JButton b4 = new JButton ("4");
    	    JButton b5 = new JButton ("5");
    	    JButton b6 = new JButton ("6");
    	    JButton b7 = new JButton ("7");
    	    JButton b8 = new JButton ("8");
    	    JButton b9 = new JButton ("9");
    	    JButton bPound = new JButton ("#");
    	    JButton b0 = new JButton ("0");
    	    JButton bStar = new JButton ("*");
    		 
    		 JButton bclear = new JButton("clear");
    		 textfield = new JTextField(5); 
    
           JPanel panel = new JPanel();
    		 setLayout (new BorderLayout());
    		 
    		 add (textfield, BorderLayout.NORTH);
    		 add (bclear, BorderLayout.EAST);
    		 setBackground (Color.green);
    		 
    		 JPanel grid = new JPanel();
    		 setLayout (new GridLayout (4,3));
    // 	    b1.addActionListener(this);
    // 		 b2.addActionListener(this);
    // 		 b3.addActionListener(this);
    // 		 b4.addActionListener(this);
    // 		 b5.addActionListener(this);
    // 		 b6.addActionListener(this);
    // 		 b7.addActionListener(this);
    // 		 b8.addActionListener(this);
    // 		 b9.addActionListerner(this);
    // 		 bPound.addActionListener(this);
    // 		 b0.addActionListener(this);
    // 		 bStar.addActionListner(this);
    
    		 
    	       add (b1);
    		    add (b2);
    		    add (b3);
    	       add (b4);
    		    add (b5);
    		    add (b6);
    		    add (b7);
    		    add (b8);
    		    add (b9);
    		    add (bPound);
    		    add (b0);
    		    add (bStar);
           
    		    setBorder (BorderFactory.createLineBorder (Color.green, 2));
    	      
    			 
    	  }
    		 
    }
    Last edited by lost_soul; 04-12-2010 at 01:49 AM.

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, 08:41 AM
  2. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 PM
  3. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  4. Help understanding packages and classpaths
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 09:22 PM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 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
  •