Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help With Flow-Layout

  1. #1
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help With Flow-Layout

    Hi! I am going thro the flow layout tutorials and am attempting to set-up a panel with 3 labels that are left center and right in the panel. My code is so super close, I am sure it is something small I am missing, but my code has the images appear to be left, left-center, right

    What should I change in my code to make the images appear left - center - right?

    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.border.LineBorder;
    import java.awt.*;
     
    public class Data {
     
        JFrame frame		= new JFrame();
        JPanel mainPanel	= new JPanel();
        JPanel secondPanel  = new JPanel();
     
        String playerimage;
        String cpuimage;
        String BattlesWon = "0";
        String BattlesLost = "0";
        String Gold = "0";
        String PlayerLevel = "1";
     
        public static void main(String args[]) {
            new Data().start();
        }
     
        public void start() {
            JLabel myLabel = new JLabel("Enter Player Name:");
            JPanel pnlHeaderInfo = new JPanel(); 
            JPanel pnlCharacters = new JPanel();
            JPanel pnlNextButton = new JPanel();
     
            JTextField myText = new JTextField(20); 
            myLabel.setAlignmentX(JTextField.CENTER_ALIGNMENT);
     
            pnlHeaderInfo.setPreferredSize(new Dimension(300, 60));
            pnlHeaderInfo.setLayout(new FlowLayout());
            pnlHeaderInfo.add(myLabel);
            pnlHeaderInfo.add(myText);
            pnlHeaderInfo.setBorder(new LineBorder(Color.RED, 3));
            mainPanel.setPreferredSize(new Dimension(600, 400));
            mainPanel.add(pnlHeaderInfo);
     
            JLabel lblLeftChar1 = new JLabel();
            JLabel lblCenterChar2 = new JLabel();
            JLabel lblRightChar3 = new JLabel();
     
     
            lblLeftChar1.setMinimumSize(new Dimension(141,244));
            lblLeftChar1.setMaximumSize(new Dimension(141,244));
            lblLeftChar1.setIcon(new ImageIcon(getClass().getResource("/resources/__Link.jpg")));
     
            lblCenterChar2.setMinimumSize(new Dimension(141,244));
            lblCenterChar2.setMaximumSize(new Dimension(141,244));
            lblCenterChar2.setIcon(new ImageIcon(getClass().getResource("/resources/__Mario.png")));
     
            lblRightChar3.setMinimumSize(new Dimension(141,244));
            lblRightChar3.setMaximumSize(new Dimension(141,244));
            lblRightChar3.setIcon(new ImageIcon(getClass().getResource("/resources/__Pikachu.png")));
     
            pnlCharacters.setPreferredSize(new Dimension(496, 256));
            pnlCharacters.setLayout(new BorderLayout());
     
            pnlCharacters.add(lblLeftChar1, BorderLayout.LINE_START);
            pnlCharacters.add(lblCenterChar2, BorderLayout.CENTER);
            pnlCharacters.add(lblRightChar3, BorderLayout.LINE_END);
            pnlCharacters.setBorder((new LineBorder(Color.BLUE, 3)));
            mainPanel.add(pnlCharacters);
     
            JButton btnNext = new JButton("Start Game");
            btnNext.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    //hide the current main panel
                    mainPanel.setVisible(false);
                    beginstagetwo();
                }
            });
     
            btnNext.setMinimumSize(new Dimension(112,29));
            btnNext.setMaximumSize(new Dimension(112,29));
            pnlNextButton.add(btnNext, BorderLayout.CENTER);
            pnlNextButton.setBorder((new LineBorder(Color.GREEN)));
            mainPanel.add(pnlNextButton);
     
            frame.add(mainPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.pack(); 
            frame.setLocationRelativeTo(null); 
            frame.setVisible(true);
        }
        public void beginstagetwo() {
            JPanel pnlPlayerStats = new JPanel();
     
            pnlPlayerStats.setPreferredSize(new Dimension(300, 60));
     
            JLabel lblPlayerName = new JLabel("Player Name:");
            JTextField txtPlayerName = new JTextField(15);
            lblPlayerName.setAlignmentX(JTextField.CENTER_ALIGNMENT);
     
            JLabel lblPlayerGold = new JLabel("Gold:");
            JTextField txtPlayerGold = new JTextField(6);
            lblPlayerGold.setAlignmentX(JTextField.CENTER_ALIGNMENT);
     
            pnlPlayerStats.setLayout(new FlowLayout());
            pnlPlayerStats.add(lblPlayerName);
            pnlPlayerStats.add(txtPlayerName);
            pnlPlayerStats.add(lblPlayerGold);
            pnlPlayerStats.add(txtPlayerGold);
     
     
            pnlPlayerStats.setBorder(new LineBorder(Color.ORANGE, 3));
            secondPanel.setPreferredSize(new Dimension(600, 400));
            secondPanel.add(pnlPlayerStats);
            frame.add(secondPanel);
            frame.pack();
            frame.setVisible(true);
        }
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Help With Flow-Layout

    That's what FlowLayout does, it starts on the left and lays them out until the next one won't fit and starts over on the left again, going back and forth. You can change the width of your sub panels to have them better fit. However, if someone resizes the main window, the sub panels may readjust. You can always prevent resizing if you choose to do so.

    Regards,
    Jim

  3. #3
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help With Flow-Layout

    Quote Originally Posted by jim829 View Post
    That's what FlowLayout does, it starts on the left and lays them out until the next one won't fit and starts over on the left again, going back and forth. You can change the width of your sub panels to have them better fit. However, if someone resizes the main window, the sub panels may readjust. You can always prevent resizing if you choose to do so.

    Regards,
    Jim
    I thought that setting the max & min size of the labels with
    setMaximumSize
    setMinimumSize

    would combat this. If I change the width of my sub-panel the image still remains directly beside the left image so it's left, left-center, right. Let me upload a few images to illustrate this.

    Changing the width wider - still seems to leave the images left, left-center and right

    Am I "throwing it off" with something else in my code?
    Screen Shot 2019-01-14 at 4.45.47 PM.jpg
    Screen Shot 2019-01-14 at 4.46.26 PM.jpg

  4. #4
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help With Flow-Layout

    After further testing, I am not sure why but adding in alignment for the label itself caused everything to display as I desired
    lblCenterChar2.setHorizontalAlignment(JLabel.CENTER);

    Can someone tell me why this did the fix, but setting the alignment when adding the label to the panel did not fix it...meaning this line
    pnlCharacters.add(lblCenterChar2, BorderLayout.CENTER);

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Help With Flow-Layout

    Use setPreferredSize() to size your panels.

    Regards,
    Jim

Similar Threads

  1. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 04:35 PM
  2. If Else Flow Charts
    By bakemono44 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2012, 07:07 PM
  3. flow of struts
    By thirupathiswami017 in forum Java Servlet
    Replies: 1
    Last Post: May 24th, 2012, 06:12 PM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. Tips or suggestion to learn java
    By tj23 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2009, 06:05 AM