gpt4 book ai didi

Java 对齐面板中的组件

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:04 24 4
gpt4 key购买 nike

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

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

}

}

最佳答案

一种方法是在面板上使用水平框布局。然后您可以使用glue来分隔组件组:

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

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

关于Java 对齐面板中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36827259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com