Search code examples
javaalignmentjlabel

How to Align Multiple JLabels?


OKay.. So I have 4 different J Labels which I want to display once the User Presses the "HELP" button. I have tried the following:

lblRuleNumberOne = new JLabel (" 1) Choose who plays first. ",     SwingConstants.LEFT); 
lblRuleNumberTwo = new JLabel (" 2) Each player in his turn drops one of his checkers down any of the slots in the top of the grid ", SwingConstants.LEFT); 
lblRuleNumberThree = new JLabel (" 3) The play alternates until one of the players gets four checkers of his colour in a row ", SwingConstants.LEFT); 
lblRuleNumberFour = new JLabel (" 4) The first player to get four in a row wins ", SwingConstants.LEFT);  

But when I ran the Code, this is what it prints:

[        1) Rule Number.....       ]
[2) Rule Number......              ]
[3) Rules Number ........          ]
[        4) Rule Number .....      ]

It should all be Something Like This:

[1) Rule Number .......]
[2) Rule Number .......]
[3) Rule Number .......]
[4) Rule Number .......]

This is my full Code:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class connectFourDesign extends JPanel { 

  private static final long serialVersionUID = 1L; 

  // Welcome Screen
  private JLabel lblWelcome;

  // Buttons for Welcome Screen
  private JButton playButton;
  private JButton helpButton; 
  private JButton quitButton;

  // Buttons for Playing Option 
  private JButton humanVsCom;
  private JButton multiplayer;
  private JButton withTimeLimit;
  private JButton noTimeLimit;

  // Go Back Button
  private JButton goBack;  

  private JLabel lblInstructions;
  private JLabel lblRuleNumberOne;
  private JLabel lblRuleNumberTwo;
  private JLabel lblRuleNumberThree;
  private JLabel lblRuleNumberFour;

  public connectFourDesign () { 

    setBackground(Color.black);        

    lblWelcome = new JLabel (" Connect 4 ", SwingConstants.CENTER);    
    lblWelcome.setFont(new Font("Astron Boy Rg", Font.ITALIC, 110));
    lblWelcome.setForeground(Color.white);
    add(lblWelcome);
    lblWelcome.setVisible(true);

    lblInstructions = new JLabel (" Instruction  ", SwingConstants.CENTER);    
    lblInstructions.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    lblInstructions.setForeground(Color.red);
    add(lblInstructions);
    lblInstructions.setVisible(false);

    lblRuleNumberOne = new JLabel (" 1) Choose who plays first. ".trim(), SwingConstants.LEFT); 
    lblRuleNumberTwo = new JLabel (" 2) Each player in his turn drops one of his checkers down any of the slots in the top of the grid ".trim(), SwingConstants.LEFT); 
    lblRuleNumberThree = new JLabel (" 3) The play alternates until one of the players gets four checkers of his colour in a row ".trim(), SwingConstants.LEFT); 
    lblRuleNumberFour = new JLabel (" 4) The first player to get four in a row wins ".trim(), SwingConstants.LEFT);  



    lblRuleNumberOne.setFont(new Font("Astron Boy Rg", Font.ITALIC, 30));
    lblRuleNumberOne.setForeground(Color.white);
    add(lblRuleNumberOne);
    lblRuleNumberOne.setVisible(false);


    lblRuleNumberTwo.setFont(new Font("Astron Boy Rg", Font.ITALIC, 22));
    lblRuleNumberTwo.setForeground(Color.white);
    add(lblRuleNumberTwo);
    lblRuleNumberTwo.setVisible(false);


    lblRuleNumberThree.setFont(new Font("Astron Boy Rg", Font.ITALIC, 22));
    lblRuleNumberThree.setForeground(Color.white);
    add(lblRuleNumberThree);
    lblRuleNumberThree.setVisible(false);


    lblRuleNumberFour.setFont(new Font("Astron Boy Rg", Font.ITALIC, 22));
    lblRuleNumberFour.setForeground(Color.white);
    add(lblRuleNumberFour);
    lblRuleNumberFour.setVisible(false);

    playButton = new JButton (" Play ");
    playButton.setFont(new Font("Astron Boy Rg", Font.ITALIC, 98));
    playButton.setBackground(Color.black);
    playButton.setForeground(Color.GREEN);
    add(playButton);
    playButton.setVisible(true);
    playButton.addActionListener(new playButtonListener());

    helpButton = new JButton ( " Help ");
    helpButton.setFont(new Font("Astron Boy Rg", Font.ITALIC, 98));
    helpButton.setBackground(Color.black);
    helpButton.setForeground(Color.magenta);
    add(helpButton);
    helpButton.setVisible(true);
    helpButton.addActionListener(new helpButtonListener());

    quitButton = new JButton ( " Quit ");
    quitButton.setFont(new Font("Astron Boy Rg", Font.ITALIC, 98));
    quitButton.setBackground(Color.black);
    quitButton.setForeground(Color.orange);
    add(quitButton);
    quitButton.setVisible(true); 
    quitButton.addActionListener(new CloseListener());

    humanVsCom = new JButton ( " Human vs Computer ");
    humanVsCom.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    humanVsCom.setBackground(Color.black);
    humanVsCom.setForeground(Color.magenta);
    add(humanVsCom);
    humanVsCom.setVisible(false);
    humanVsCom.addActionListener(new humanVsComputerButtonListener());

    multiplayer = new JButton ( " Multiplayer ");
    multiplayer.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    multiplayer.setBackground(Color.black);
    multiplayer.setForeground(Color.orange);
    add(multiplayer);
    multiplayer.setVisible(false);
    multiplayer.addActionListener(new multiplayerButtonListener());

    withTimeLimit = new JButton ( " with Time ");
    withTimeLimit.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    withTimeLimit.setBackground(Color.black);
    withTimeLimit.setForeground(Color.magenta);
    add(withTimeLimit);
    withTimeLimit.setVisible(false);
    withTimeLimit.addActionListener(new withTimeLimitButtonListener());

    noTimeLimit = new JButton ( " without Time ");
    noTimeLimit.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    noTimeLimit.setBackground(Color.black);
    noTimeLimit.setForeground(Color.orange);
    add(noTimeLimit);
    noTimeLimit.setVisible(false);   
    noTimeLimit.addActionListener(new noTimeLimitButtonListener());

    goBack = new JButton ( " Go Back "); 
    goBack.setFont(new Font("Astron Boy Rg", Font.ITALIC, 50));
    goBack.setBackground(Color.black);
    goBack.setForeground(Color.BLUE);
    add(goBack);
    goBack.setVisible(false); 
    goBack.addActionListener(new goBackButtonListener());
  } 


  private class playButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {      

      if (event.getSource() == playButton) { 

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(true);
        multiplayer.setVisible(true);
        withTimeLimit.setVisible(true);
        noTimeLimit.setVisible(true);
        goBack.setVisible(true);
      }
    }
  }

  private class goBackButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {      

      if (event.getSource() == goBack) {         

        lblWelcome.setVisible(true);
        playButton.setVisible(true);
        helpButton.setVisible(true); 
        quitButton.setVisible(true);
        humanVsCom.setVisible(false);
        multiplayer.setVisible(false);
        withTimeLimit.setVisible(false);
        noTimeLimit.setVisible(false);
        lblInstructions.setVisible(false);
        lblRuleNumberOne.setVisible(false);
        lblRuleNumberTwo.setVisible(false);
        lblRuleNumberThree.setVisible(false);
        lblRuleNumberFour.setVisible(false);
        goBack.setVisible(false);        
      }
    }
  }

  private class CloseListener implements ActionListener{
    public void actionPerformed(ActionEvent event) {
      if (event.getSource() == quitButton) {           
        System.exit(0);
      }
    }
  }

  private class humanVsComputerButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) { 

      if (event.getSource() == humanVsCom) {

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(true);
        multiplayer.setVisible(false);
        withTimeLimit.setVisible(false);
        noTimeLimit.setVisible(false);
        goBack.setVisible(false);       

        Thread thread =new Thread() {

          public void run() {
            humanVsCom.setText(" Game Starts In ");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            humanVsCom.setText("3");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            humanVsCom.setText("2");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            humanVsCom.setText("1");  
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }

            humanVsCom.setVisible(false);
          }
        };
        thread.start();
      }
    }
  }

  private class multiplayerButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {    

      if (event.getSource() == multiplayer) {

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(false);
        multiplayer.setVisible(true);
        withTimeLimit.setVisible(false);
        noTimeLimit.setVisible(false);
        goBack.setVisible(false);       

        Thread thread =new Thread() {

          public void run() {
            multiplayer.setText(" Game Starts In ");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            multiplayer.setText("3");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            multiplayer.setText("2");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            multiplayer.setText("1");  
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }             
            multiplayer.setVisible(false);
          }
        };
        thread.start();
      }
    }
  }

  private class withTimeLimitButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {    

      if (event.getSource() == withTimeLimit) {

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(false);
        multiplayer.setVisible(false);
        withTimeLimit.setVisible(true);
        noTimeLimit.setVisible(false);
        goBack.setVisible(false);       

        Thread thread =new Thread() {

          public void run() {
            withTimeLimit.setText(" Game Starts In ");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            withTimeLimit.setText("3");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            withTimeLimit.setText("2");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            withTimeLimit.setText("1");  
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }             
            withTimeLimit.setVisible(false);
          }
        };
        thread.start();
      }
    }
  }

  private class noTimeLimitButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {    

      if (event.getSource() == noTimeLimit) {

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(false);
        multiplayer.setVisible(false);
        withTimeLimit.setVisible(false);
        noTimeLimit.setVisible(true);
        goBack.setVisible(false);       

        Thread thread =new Thread() {

          public void run() {
            noTimeLimit.setText(" Game Starts In ");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            noTimeLimit.setText("3");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            noTimeLimit.setText("2");
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }
            noTimeLimit.setText("1");  
            try {
              Thread.sleep(1000);                        
            }catch (Exception e) {
            }             
            noTimeLimit.setVisible(false);
          }
        };
        thread.start();
      }
    }
  }

  private class helpButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {    

      if (event.getSource() == helpButton) {

        lblWelcome.setVisible(true);
        playButton.setVisible(false);
        helpButton.setVisible(false); 
        quitButton.setVisible(false);
        humanVsCom.setVisible(false);
        multiplayer.setVisible(false);
        withTimeLimit.setVisible(false);
        noTimeLimit.setVisible(false);
        lblInstructions.setVisible(true);
        lblRuleNumberOne.setVisible(true);
        lblRuleNumberTwo.setVisible(true);
        lblRuleNumberThree.setVisible(true);
        lblRuleNumberFour.setVisible(true);
        goBack.setVisible(true);    

      }
    }
  }
}

Solution

  • Use a different LayoutManager. You are using the default layout manager, which is a FlowLayout that tries to place all of the children horizontally, and when there is not enough room, it wraps to the next line.