问题
I am trying to add components to SOUTH of a BorderLayout I need it to look like this example.
------------------------------------
| |
| |
| |
| |
| |
| |
|_TxtField|Button_____________Label|
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();
}
}
回答1:
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);
Read the section from the Swing tutorial on How to Use BoxLayout for more information on the glue
and working examples.
来源:https://stackoverflow.com/questions/36827259/java-aligning-components-in-panels