First time working with Java swing and I'm not understanding where I'm going wrong.
I'm creating a JFrame
, and then creating two JPanels
to add to it. Each JPanel
has a few JLabels
added to them. The JFrame
and JPanels
are specified as FlowLayouts
, which (as I understand it) means that things are just displayed in the order they are added and added to the next line if there isn't room for them on the current line.
Here's my problem: If I run my current code and only add my first panel (infoPanel
), I get a correct result that looks like this: .
But, if I try to add both panels, the first one gets overridden and the JLabels
of the first are not fully displayed (There should be much more then "Na..." on each, this is only the first part of a multi-line string starting with "Name". I specify the minimum size as 100x100, and yet they are still only a few characters. I've tried making them 400x400 and other sizes, but nothing changes. It looks like this:
My questions are:
(1) Am I doing something with my panels incorrectly? Should the JFrame
with a FlowLayout
not be able to add both panels without overwriting the first one that was added?
(2) Why aren't my full JLabels
displaying? I don't understand where I'm going wrong, as they're specified the exact same as the JLabels
in my infoPanel
JPanel
and not appearing the same...
Any help is appreciated, my code is below:
private void createView()
{
// Create the frame
frame = new JFrame("Shared Resources ");
frame.setSize(850, 750);
frame.setResizable(true);
frame.setLocation(0,0);;
frame.setVisible(true);
// Set the content
Container content = frame.getContentPane();
content.setBackground(Color.WHITE);
// Create our panels
JPanel infoPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel playerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
// Empty border template
Border emptyBorder = BorderFactory.createEmptyBorder();
// Shared resources information
// Palace Cards
palaceCards = new JLabel("Player Palace Cards");
/* TODO */
palaceCards.setHorizontalTextPosition(SwingConstants.CENTER);
palaceCards.setVerticalTextPosition(SwingConstants.BOTTOM);
palaceCards.setVerticalAlignment(SwingConstants.BOTTOM);
palaceCards.setBorder(emptyBorder);
palaceCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
palaceCards.setPreferredSize(new Dimension(350,25));
palaceCards.setMinimumSize(palaceCards.getPreferredSize());
infoPanel.add(palaceCards);
festivalCards = new JLabel("Festival Cards");
festivalCards.setBorder(emptyBorder);
festivalCards.setPreferredSize(new Dimension(350,25));
festivalCards.setMinimumSize(festivalCards.getPreferredSize());
festivalCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(festivalCards);
numThreeBlocks = newJLabel("Three Blocks: ");
numThreeBlocks.setText("");
numThreeBlocks.setPreferredSize(new Dimension(350,25));
numThreeBlocks.setHorizontalTextPosition(SwingConstants.CENTER);
numThreeBlocks.setVerticalTextPosition(SwingConstants.BOTTOM);
numThreeBlocks.setVerticalAlignment(SwingConstants.BOTTOM);
numThreeBlocks.setMinimumSize(numThreeBlocks.getPreferredSize());
numThreeBlocks.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numThreeBlocks);
numIrrigationTiles = newJLabel("Irrigation Tiles");
numIrrigationTiles.setPreferredSize(new Dimension(350,25));
numIrrigationTiles.setMinimumSize(numIrrigationTiles.getPreferredSize());
numIrrigationTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numIrrigationTiles);
numTwoPalaceTiles = newJLabel("Two Palace Tiles: ");
numTwoPalaceTiles.setPreferredSize(new Dimension(350,25));
numTwoPalaceTiles.setMinimumSize(numTwoPalaceTiles.getPreferredSize());
numTwoPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numTwoPalaceTiles);
numFourPalaceTiles = newJLabel("Four Palace Tiles: ");
numFourPalaceTiles.setPreferredSize(new Dimension(350,25));
numFourPalaceTiles.setMinimumSize(numFourPalaceTiles.getPreferredSize());
numFourPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numFourPalaceTiles);
numSixPalaceTiles = newJLabel("Six Palace Tiles: ");
numSixPalaceTiles.setPreferredSize(new Dimension(350,25));
numSixPalaceTiles.setMinimumSize(numSixPalaceTiles.getPreferredSize());
numSixPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numSixPalaceTiles);
numEightPalaceTiles = newJLabel("Eight Palace Tiles: ");
numEightPalaceTiles.setPreferredSize(new Dimension(350,25));
numEightPalaceTiles.setMinimumSize(numEightPalaceTiles.getPreferredSize());
numEightPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numEightPalaceTiles);
numTenPalaceTiles = newJLabel("Ten Palace Tiles: ");
numTenPalaceTiles.setPreferredSize(new Dimension(350,25));
numTenPalaceTiles.setMinimumSize(numTenPalaceTiles.getPreferredSize());
numTenPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numTenPalaceTiles);
player1 = newJLabel("Player 1");
player1.setMinimumSize(new Dimension(400,400));
player1.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player1);
player2 = newJLabel("PLayer 2");
player2.setMinimumSize(new Dimension(400,400));
player2.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player2);
player3 = newJLabel("Player 3");
player3.setMinimumSize(new Dimension(400,400));
player3.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player3);
player4 = newJLabel("Player 4");
player4.setMinimumSize(new Dimension(100,100));
player4.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player4);
// Add panels
content.add(infoPanel);
content.add(playerPanel);
}