Reputation: 349
I'm newbie in Java programming and can't deal with my Swing problem. I can't get JButtons and JLabels Centred in JPanels(BoxLayout).
Here are some photos :
Code:
PlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
PlayerLabel.setVerticalTextPosition(JLabel.BOTTOM);
PlayerLabel.setText("Player Level: " + CarMain.main[5]);
AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);
JButton jbtnSellAccessoriesLv1 = new JButton("Sell");
jbtnSellAccessoriesLv1.addActionListener(this);
And this is where i make Jlabel :
//Upgrades Panel
GridLayout UpgradesLayout = new GridLayout(3,3);
JPanel UpgradesPanel = new JPanel();
UpgradesPanel.setLayout(UpgradesLayout);
JPanel UpgradesPanelSub = new JPanel();
UpgradesPanelSub.setLayout(new BoxLayout(UpgradesPanelSub, BoxLayout.PAGE_AXIS));
UpgradesPanelSub.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub2 = new JPanel();
UpgradesPanelSub2.setLayout(new BoxLayout(UpgradesPanelSub2, BoxLayout.PAGE_AXIS));
UpgradesPanelSub2.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub3 = new JPanel();
UpgradesPanelSub3.setLayout(new BoxLayout(UpgradesPanelSub3, BoxLayout.PAGE_AXIS));
UpgradesPanelSub3.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
UpgradesPanelSub.add(Labels.PlayerLabel);
UpgradesPanelSub.add(jbtnUpgradeLevel);
UpgradesPanelSub2.add(Labels.GarageLabel);
UpgradesPanelSub2.add(jbtnUpgradeGarageLevel);
UpgradesPanelSub3.add(Labels.BoxesLVLLabel);
UpgradesPanelSub3.add(jbtnUpgradeBoxesLevel);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(UpgradesPanelSub);
UpgradesPanel.add(UpgradesPanelSub2);
UpgradesPanel.add(UpgradesPanelSub3);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
Upvotes: 0
Views: 151
Reputation: 88757
As for the layout problem: there's a tutorial on how to use box layout (look for the components' alignment to center them): docs.oracle.com/javase/tutorial/uiswing/layout/box.html.
Alternatively you could use one of the external layout managers like MigLayout which are easier to use IMO.
Upvotes: 1