所以,自从上一个问题以来,我已经找到了一些方法,并改变了我现在所做的一切。
我目前的问题是:我有三个按钮和一个文本字段,我想添加到我的单一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后,我通常只对布局管理器以及它们的工作原理感到困惑。任何帮助都会很棒。
答案 0 :(得分:1)
为按钮创建一个面板:
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教程了解每个布局管理器的基础知识答案 1 :(得分:0)
您可以通过多种不同方式实现此目标,您使用的方法取决于您所需的结果,例如,您只需使用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);
这可能稍好一些,因为该字段将始终位于父容器的可用空间的中心...