Java在面板中对齐组件 [英] Java aligning components in panels

查看:150
本文介绍了Java在面板中对齐组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add components to SOUTH of a BorderLayout I need it to look like this example.

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

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

So I need a JTextField and a JButton aligned to the left, and a label aligned to the right. How can I accomplish this? Here is my code below, I have tried to do this using nested panels:

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

    }

}


推荐答案

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

One way is to use a horizontal Box Layout on the panel. Then you can use glue to separate the groups of components:

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

阅读如何使用BoxLayout 获取有关 glue 和工作示例的更多信息。

Read the section from the Swing tutorial on How to Use BoxLayout for more information on the glue and working examples.

这篇关于Java在面板中对齐组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆