Java在面板中对齐组件

时间:2016-04-24 18:35:37

标签: java swing layout-manager border-layout

我正在尝试将组件添加到BorderLayout的SOUTH中,我需要它看起来像这个例子。

------------------------------------
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|_TxtField|Button_____________Label|

所以我需要一个JTextField和一个与左边对齐的JButton,以及一个与右边对齐的标签。我怎么能做到这一点?以下是我的代码,我尝试使用嵌套面板执行此操作:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BlackjackGUI{

    private JFrame frame;
    private JPanel panel, panelLeft, panelBottomLeft, panelBottomRight;
    private JButton newGameBtn, dealBtn, hitBtn, standBtn;
    private JLabel placeBetLbl, playerMoneyLbl;
    private JLabel playerCard1Lbl, playerCard2Lbl, playerCard3Lbl,
                   playerCard4Lbl, playerCard5Lbl, playerCard6Lbl, playerCard7Lbl;
    private JLabel dealerCard1Lbl, dealerCard2Lbl, dealerCard3Lbl, dealerCard4Lbl,
                   dealerCard5Lbl, dealerCard6Lbl, dealerCard7Lbl;  
    private JLabel playerCardValueLbl, dealerCardValueLbl;
    private JLabel spacer1, spacer2;
    private JTextField betInputBox;

    public BlackjackGUI(){
        createForm();

        addTextField();
        addButtons();
        addLabels();


        frame.add(panel);
        frame.setVisible(true);
    }

    public void createForm() {
        frame = new JFrame("Blackjack");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1200,800);

        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        Color c = new Color(0, 100, 0);
        panel.setBackground(c);


        panelLeft = new JPanel();
        Color panelLeftBG = new Color (23, 25, 100);
        panelLeft.setBackground(panelLeftBG);
        panel.add(panelLeft, BorderLayout.WEST);

        panelBottomLeft = new JPanel();
        Color panelBottomLeftBG = new Color (56, 12, 10);
        panelBottomLeft.setBackground(panelBottomLeftBG);
        panelBottomLeft.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel.add(panelBottomLeft, BorderLayout.SOUTH);

        panelBottomRight = new JPanel();
        Color panelBottomRightBG = new Color (12, 88, 40);
        panelBottomRight.setBackground(panelBottomRightBG);
        panelBottomRight.setLayout(new FlowLayout(FlowLayout.RIGHT));
        panel.add(panelBottomRight, BorderLayout.SOUTH);
    }

    public void addButtons() {

        newGameBtn = new JButton("New Game");
        panelLeft.add(newGameBtn, BorderLayout.WEST);
        newGameBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);

            }
        });

        dealBtn = new JButton("Deal");
        dealBtn.setPreferredSize(new Dimension (100, 50));
        panelBottomLeft.add(dealBtn);
        newGameBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);

            }
        });

    }

    public void addTextField() {

        betInputBox = new JTextField();
        betInputBox.setPreferredSize(new Dimension(175,50));
        panelBottomLeft.add(betInputBox);
    }

    public void addLabels() {

        placeBetLbl = new JLabel("Place your bets!");
        placeBetLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        panelBottomLeft.add(placeBetLbl);

        playerMoneyLbl = new JLabel("£2,500");
        playerMoneyLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        playerMoneyLbl.setLayout(new FlowLayout(FlowLayout.RIGHT));
        panelBottomRight.add(playerMoneyLbl);

    }

    public static void main(String[] args) {
    new BlackjackGUI();

    }

}

1 个答案:

答案 0 :(得分:0)

一种方法是在面板上使用水平Box Layout。然后,您可以使用glue分隔组件组:

JPanel panel = new JPanel();
panel.setLayout(...);
panel.add(textField);
panel.add(button);
panel.add(the glue here);
panel.add(label);

阅读How to Use BoxLayout上Swing教程中的部分,了解有关glue和工作示例的更多信息。