在同一个 JPanel 上移动不同的元素

Moving different Elements on the same JPanel

所以,自从我上一个问题以来,我已经有了一些方法,并且改变了我现在做所有事情的方式。

我当前的问题是:我有三个按钮和一个文本字段,我想将它们添加到我的单一 JPanel 中。我希望文本字段位于按钮下方并居中。有没有一种方法可以用普通布局轻松做到这一点,我该怎么做?

代码在这里

public class FarkleWindow extends JFrame{
public FarkleWindow()
{
    this.setTitle("Farkle!");
    this.setSize(windowWidth,windowHeight);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inititalizeWindow();
    addButtonListener();

    this.setVisible(true);
}

private void inititalizeWindow() {

    gameBoard = new FarkleGameBoard();
    game = new FarkleGame();

    roll = new JButton("Roll First Time");
    reroll = new JButton("Roll Again!");
    endTurn = new JButton("Let The Other Player Roll!");

    gameBoard.add(roll, BorderLayout.CENTER);
    gameBoard.add(reroll, BorderLayout.CENTER);
    gameBoard.add(endTurn, BorderLayout.CENTER);

    playerTurn = new JTextField();
    if(game.isPlayer1Turn())
        playerTurn.setText(game.getP1() + "\'s Score Turn Score Is " + game.getTurnScore());
    else
        playerTurn.setText(game.getP2() + "\'s Score Turn Score Is " + game.getTurnScore());

    playerTurn.setEditable(false);

    gameBoard.add(playerTurn, BorderLayout.SOUTH);

    this.add(gameBoard, BorderLayout.CENTER);   
    repaint();
}
}

public class FarkleGameBoard extends JPanel{
    public void paintComponent(Graphics g)
{   
    g.setColor(mainBackGround);
    g.fillRect(0,0,2*playerDiceWidth+tossingAreaWidth+2*player1diceX, 
                playerDiceHeight+2*player1diceY);

    g.setColor(diceArea);
    g.fillRect(player1diceX,player1diceY,playerDiceWidth, playerDiceHeight);
    g.fillRect(player1diceX+tossingAreaWidth,player1diceY,playerDiceWidth, playerDiceHeight);

    g.setColor(scoreBoard);
    g.fillRect(player1diceX+playerDiceWidth+currentScoreX, player1diceY+currentScoreY,
        currentScoreWidth,currentScoreHeight);
    g.fillRect(player1diceX+playerDiceWidth+runScoreX, player1diceY+runScoreY,
        runScoreWidth,runScoreHeight);
}
}

在阅读了一些 API 之后,我通常只是对布局管理器及其工作原理感到困惑。任何帮助都会很棒。

为按钮创建一个面板:

JPanel buttonPanel = new JPanel();
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);

为文本字段创建另一个面板:

JPanel textPanel = new JPanel( FlowLayout.CENTER );
textPanel.add( textField );

现在可以使用 BoxLayout 垂直显示面板并将此面板添加到框架中:

Box main = Box.createVerticalBox();
main.add(buttonPanel);
main.add(textPanel);
add(main, BorderLayout.SOUTH);

永远不要认为您必须使用一个面板。只需按逻辑分解布局即可。

当然有很多方法可以做到这一点。这只是一种方法。从 Layout Managers

上的 Swing 教程学习每个布局管理器的基础知识

您可以通过多种不同的方式实现这一目标,您使用哪种方式取决于您想要的结果,例如,您可以简单地使用 GridBagLayout

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Roll First Time"), gbc);
gbc.gridx++;
panel.add(new JButton("Roll Again"), gbc);
gbc.gridx++;
panel.add(new JButton("Let the other player roll"), gbc);

gbc.gridx = 0;
gbc.gridy++;
// You could also use a EmptyBorder
gbc.insets = new Insets(100, 0, 0, 0);
gbc.gridwidth = 3;
panel.add(new JTextField(10), gbc);

或者您可以使用复合方法,使用多个布局管理器来实现您之后的结果...

JPanel buttons = new JPanel(new FlowLayout());
buttons.add(new JButton("Roll First Time"));
buttons.add(new JButton("Roll Again"));
buttons.add(new JButton("Let the other player roll"));

JPanel field = new JPanel(new GridBagLayout());
field.add(new JTextField(10));

JPanel main = new JPanel(new BorderLayout());
main.add(buttons, BorderLayout.NORTH);
main.add(field);

这可能会稍微好一点,因为该字段将始终位于父容器可用 space 的中心...