如何对齐多个JLabel?

时间:2016-03-17 18:51:57

标签: java alignment jlabel

好的..所以我有4个不同的J标签,我想在用户按下" HELP"按钮。我尝试过以下方法:

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);  

但是当我运行代码时,这就是它打印的内容:

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

它应该都是这样的东西:

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

这是我的完整代码:

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);    

      }
    }
  }
}

3 个答案:

答案 0 :(得分:-1)

您是否尝试在字符串上使用.trim(),这会从字符串的两端删除空格?

所以,例如:

lblRuleNumberOne = new JLabel (" 1) Choose who plays first. ".trim(), SwingConstants.LEFT); 

答案 1 :(得分:-1)

您始终可以创建一个JLabel数组,以便能够迭代。然后,您可以为特定标签指定特定值。

private JLabel[] labels;
labels = new JLabel[4];
labels[3] = new JLabel("Rule 4 here");

如果您对修复字符串格式感兴趣,请在熟悉HTML的情况下查看StringBuilder。

答案 2 :(得分:-1)

使用其他LayoutManager。您正在使用默认布局管理器,它是一个尝试将所有子项水平放置的FlowLayout,当没有足够的空间时,它将换行到下一行。