Results 1 to 4 of 4
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Help with centering a panel in JFrame.

    Okay so I have the codes all written but I didn't really look at that I had to color the center of the frame. I have base code for it but it just doesn't want t ocolor the cnter frame for me for some reason. I have been looking around and still am having problem with it only coloring the center of the frame.

    Here is my code. Please shed some light on my struggle.

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JMenu; 
    import javax.swing.JMenuBar; 
    import javax.swing.JMenuItem;
    
    public class MenuColors
    {  
       public static void main(String[] args)        
       { 
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          frame.add(panel); 
          panel.setBackground(Color.WHITE);
          panel.setLayout(new BorderLayout());
          frame.add(panel, BorderLayout.CENTER);
          
          JMenuBar colorMenu = new JMenuBar();     
          frame.setJMenuBar(colorMenu);
          JMenu menu = new JMenu("File");
          final JMenuItem redItem = new JMenuItem("Red");  
          final JMenuItem greenItem = new JMenuItem("Green");  
          final JMenuItem blueItem = new JMenuItem("Blue");      
          menu.add(redItem);
          menu.add(greenItem);
          menu.add(blueItem);
          colorMenu.add(menu);
          
          final JLabel label = new JLabel("Please select a color from the menu.");
          frame.add(label, BorderLayout.SOUTH);
          
          class ClickListener implements ActionListener 
          {
             public void actionPerformed(ActionEvent event)   
             {  
                if(event.getSource() == redItem)
                   panel.setBackground(Color.RED); 
                else if(event.getSource() == greenItem)
                   panel.setBackground(Color.GREEN);
                else if(event.getSource() == blueItem)
                   panel.setBackground(Color.BLUE);
             }            
          }
          ActionListener listener = new ClickListener();//Create the listener
          redItem.addActionListener(listener);
          greenItem.addActionListener(listener);      
          blueItem.addActionListener(listener);
          frame.setSize(2000, 2000);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }   
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with centering a panel in JFrame.

    The documentation for BorderLayout says:

    "the CENTER component may stretch both horizontally and vertically to fill any space left over."

    So you are centering it. BTW, is your screen really 2000 x 2000 pixels?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Help with centering a panel in JFrame.

    Quote Originally Posted by jim829 View Post
    The documentation for BorderLayout says:

    "the CENTER component may stretch both horizontally and vertically to fill any space left over."

    So you are centering it. BTW, is your screen really 2000 x 2000 pixels?

    Regards,
    Jim
    It isn't, I just tried to see if the panel was bigger then the original size. So I should implement the other panel areas and it would be good.
    Last edited by SmokeyWoody; 11-20-2014 at 12:45 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with centering a panel in JFrame.

    Or you could try a different layout manager. Perhaps GridLayout. It all depends on exactly what you want to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. centering/sizing fields on a panel
    By philgrek in forum AWT / Swing
    Replies: 1
    Last Post: 11-20-2011, 11:01 AM
  2. panel is not visible on my Jframe
    By shruts in forum AWT / Swing
    Replies: 8
    Last Post: 08-03-2011, 03:18 PM
  3. Panel not appearing in JFrame
    By eLancaster in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 08:53 PM
  4. How to add panel to JFrame?
    By geekchick in forum New To Java
    Replies: 17
    Last Post: 08-01-2010, 06:23 PM
  5. Centering inside a JFrame
    By kahaj in forum AWT / Swing
    Replies: 9
    Last Post: 09-23-2009, 08:23 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
  •