java - 如何对齐多个JLabel?

标签 java alignment jlabel

好吧..所以我有4个不同的J标签,我想在用户按下“帮助”按钮时显示它们。我已经尝试过以下方法:

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

      }
    }
  }
}

最佳答案

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

关于java - 如何对齐多个JLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069290/

相关文章:

java - 包含 XML 模式本地副本的 XML 文件

java - 力扣 : Counting Elements with O(n) time Complexity

单元格中的 CSS 水平菜单对齐方式

iPhone 表格单元格标签未对齐

java - KeyListener 不工作/没有被调用?

java - jboss 4 java.util.Properties 类型的 JNDI 资源

java - 我如何通知我的应用程序一个文件已从 SDCard (Android) 中删除?

HTML Float 问题 - 应该很简单但无法正常工作

java - 如何获得链接到 JButton 的 JLabel?

java - 在 JLabel 上的 gif 和 png 之间切换