JPanel not shown properly - java

So, I'm starting with the basic visual Java components, and have trouble to use them accurately.
Here is my new problem: I'm trying to implement a 3 parts panel: east, center, west, but can't have a proper display of the center panel. Here is my piece of code:
Basically 'panelUpMiddle' isn't visible, so I wonder why?...
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}

Try this code. I updated the naming of the panels and the labels so it is more clear.
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel centerPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JPanel westPanel = new JPanel(new BorderLayout());
window.add(centerPanel, BorderLayout.CENTER);
window.add(eastPanel, BorderLayout.EAST);
window.add(westPanel, BorderLayout.WEST);
JLabel centerLabel = new JLabel("Center");
JLabel eastLabel = new JLabel("East");
JLabel westLabel = new JLabel("West");
eastPanel.add(eastLabel);
westPanel.add(westLabel);
centerPanel.add(centerLabel);
centerPanel.setPreferredSize(new Dimension(200, 400));
eastPanel.setPreferredSize(new Dimension(100, 400));
westPanel.setPreferredSize(new Dimension(100, 400));
eastPanel.setBackground(new Color(200, 240, 200));
westPanel.setBackground(new Color(200, 240, 0));
window.setVisible(true);

A third panel is never created and nested within the other panel.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
JPanel panelUpRight = new JPanel(); //Create third panel
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpRight, BorderLayout.EAST); //Add Third Panel
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUpRight.add(label1); //Add label for third panel
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}

Actually, you don't need the innerPanel:
public class TestCode_Web
{
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.setSize(400, 400);
JPanel panelUp = new JPanel();
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(panelUp, BorderLayout.NORTH);
window.add(panelUpLeft, BorderLayout.WEST);
window.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
NiceCow gave the answer why yours is not working. This should work fine.

Related

How do I specifically position JLabels within a JPanel

I made a JPanel, which has 6 string JLabels, and one image JLabel. What I need is to be able to position the Jlabel strings somewhere inside of my image JLabel's bounds. I'm pretty sure my problem would be solved if I knew how to resize JLabels.
Some Code:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("rainbow seizure");
frame.setResizable(false);
frame.setSize(500, 500);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.SOUTH);
JButton newCardButton = new JButton("New Card");
newCardButton.addActionListener(actionEvent -> {
JPanel panel2 = new JPanel();
panel2.setLayout(new OverlayLayout(panel2));
JLabel jImage = new JLabel(new ImageIcon("C:\\Users\\unkno\\java\\Card1.PNG"));
Dimension dimension = jImage.getPreferredSize(); // dimensions for me are 72x122
System.out.println(dimension.width);
System.out.println(dimension.height);
JLabel label1 = new JLabel("name");
label1.setBounds(new Rectangle(dimension.width, dimension.height)); // doesn't do anything :(
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label1.setAlignmentY(Component.TOP_ALIGNMENT);
JLabel label2 = new JLabel("cost");
JLabel label3 = new JLabel ("atk");
JLabel label4 = new JLabel("type");
JLabel label5 = new JLabel ("hp");
JLabel label6 = new JLabel("disc");
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(jImage);
panel.add(panel2);
frame.revalidate();
});
frame.add(newCardButton, BorderLayout.NORTH);
}
}

How to align JLabels and JPanels to the left inside BoxLayout?

I want to align Labels and a Panel (containing Buttons) to the left inside a vertical BoxLayout.
As long as I don't add the panel to the BoxLayout everything is aligned to the left perfectly, but adding it screws everything up.
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel five = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Font testFont = new Font("Arial", Font.BOLD, 20);
JLabel label1 = new JLabel("Label1");
label1.setFont(testFont);
JLabel label2 = new JLabel("Label2");
label2.setFont(testFont);
five.setLayout(new BoxLayout(five, BoxLayout.X_AXIS));
plus.setMinimumSize(new Dimension(30, 30));
plus.setMaximumSize(new Dimension(30, 30));
minus.setMinimumSize(new Dimension(30, 30));
minus.setMaximumSize(new Dimension(30, 30));
five.add(plus);
five.add(minus);
panel.add(label1);
panel.add(five);
panel.add(label2);
panel.setOpaque(true);
panel.setBackground(Color.red);
frame.setMinimumSize(new Dimension(200, 200));
frame.getContentPane().add(panel, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Your components need to have the same "x alignment":
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label2.setAlignmentX(Component.LEFT_ALIGNMENT);
five.setAlignmentX(Component.LEFT_ALIGNMENT);
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
You can use invisible components as fillers.
private static Box leftAlignedInHorizontalBox(Component component) {
Box box = Box.createHorizontalBox();
box.add(component);
box.add(Box.createHorizontalGlue());
return box;
}
and then:
panel.add(leftAlignedInHorizontalBox(label1));

Java Swing Scroll-able JPanel with Multiple Panels on Frame

I have a JFrame and it has two JPanels. First is the header and then below it is the main content panel. I want to make my second content panel to be scroll-able but even though the scrollbar appears on the panel it doesn't seem to expand to its true height. In my second JPanel called p2, I am adding 20 small JPanels which should cause it to expand in height but it doesn't do that. Below is my Code:
public class DisplayItems3{
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Home Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//Top Panel
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.LIGHT_GRAY);
p1.setPreferredSize(new Dimension(950, 100));
JLabel l1 = new JLabel("All Library Items");
l1.setForeground(Color.BLACK);
l1.setPreferredSize(new Dimension(900, 50));
l1.setFont(l1.getFont().deriveFont(30.0f));
p1.add(l1);
//Content Panel
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.setBackground(Color.LIGHT_GRAY);
p2.setPreferredSize(new Dimension(950, 800));
p2.setAutoscrolls(true);
JScrollPane scrollPane = new JScrollPane(p2);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0, 0, 950, 800);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(950, 800));
contentPane.add(scrollPane);
for(int i = 0; i < 20; i++) {
JPanel sp1 = new JPanel();
sp1.setLayout(new FlowLayout());
sp1.setBackground(Color.WHITE);
sp1.setPreferredSize(new Dimension(900, 180));
p2.add(sp1);
}
//contentPane.add(p2);
frame.add(p1);
//frame.add(p2);
//frame.setContentPane(contentPane);
frame.add(contentPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Update using JList
It didn't work!
public class JListTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Home Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//Top Panel
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.LIGHT_GRAY);
p1.setPreferredSize(new Dimension(950, 100));
JLabel l1 = new JLabel("All Library Items");
l1.setForeground(Color.BLACK);
l1.setPreferredSize(new Dimension(900, 50));
l1.setFont(l1.getFont().deriveFont(30.0f));
p1.add(l1);
JList ll1 = new JList();
ll1.setLayout(new FlowLayout());
ll1.setBackground(Color.LIGHT_GRAY);
ll1.setPreferredSize(new Dimension(950, 800));
ll1.setAutoscrolls(true);
JScrollPane scrollPane = new JScrollPane(ll1);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(0, 0, 950, 800);
for(int i = 0; i < 20; i++) {
JPanel sp1 = new JPanel();
sp1.setLayout(new FlowLayout());
sp1.setBackground(Color.WHITE);
sp1.setPreferredSize(new Dimension(900, 180));
ll1.add(sp1);
}
//contentPane.add(p2);
frame.add(p1);
frame.add(scrollPane);
//frame.setContentPane(contentPane);
//frame.add(contentPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Problem Solved: Working Code
public class scrollTest2 {
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Home Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JLabel m1 = new JLabel("safsd");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setAutoscrolls(true);
frame.add(panel,BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 800, 800);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(900, 900));
contentPane.add(scrollPane);
for(int i = 0; i < 30; i++) {
JPanel sp1 = new JPanel();
sp1.setLayout(new FlowLayout());
sp1.setBackground(Color.WHITE);
sp1.setPreferredSize(new Dimension(900, 180));
JPanel ssp1 = new JPanel();
ssp1.setLayout(new FlowLayout());
ssp1.setBackground(Color.WHITE);
ssp1.setPreferredSize(new Dimension(500, 170));
JPanel ssp2 = new JPanel();
ssp2.setLayout(new FlowLayout());
ssp2.setBackground(Color.WHITE);
ssp2.setPreferredSize(new Dimension(350, 170));
JLabel l3 = new JLabel("Title: ");
l3.setForeground(Color.BLACK);
l3.setPreferredSize(new Dimension(100, 20));
JTextField t1 = new JTextField("Electronic Basics");
t1.setPreferredSize(new Dimension(320, 20));
JLabel l4 = new JLabel("Type: ");
l4.setForeground(Color.BLACK);
l4.setPreferredSize(new Dimension(100, 20));
JTextField t2 = new JTextField("Book");
t2.setPreferredSize(new Dimension(320, 20));
JLabel l5 = new JLabel("Authors: ");
l5.setForeground(Color.BLACK);
l5.setPreferredSize(new Dimension(100, 20));
JTextField t3 = new JTextField("Bob the Builder");
t3.setPreferredSize(new Dimension(320, 20));
JLabel l6 = new JLabel("Publisher: ");
l6.setForeground(Color.BLACK);
l6.setPreferredSize(new Dimension(100, 20));
JTextField t4 = new JTextField("ABC Company");
t4.setPreferredSize(new Dimension(320, 20));
JLabel l7 = new JLabel("Location: ");
l7.setForeground(Color.BLACK);
l7.setPreferredSize(new Dimension(100, 20));
JTextField t5 = new JTextField("Shelf 1 Row 3");
t5.setPreferredSize(new Dimension(320, 20));
JLabel l8 = new JLabel("Status: ");
l8.setForeground(Color.BLACK);
l8.setPreferredSize(new Dimension(100, 20));
JTextField t6 = new JTextField("Available");
t6.setPreferredSize(new Dimension(320, 20));
JButton btnLoanHistory = new JButton("Loan History");
btnLoanHistory.setPreferredSize(new Dimension(300, 20));
JButton btnLoanItem = new JButton("Loan Item");
btnLoanItem.setPreferredSize(new Dimension(300, 20));
JButton btnProcessReturn = new JButton("Process Return");
btnProcessReturn.setPreferredSize(new Dimension(300, 20));
ssp1.add(l3);
ssp1.add(t1);
ssp1.add(l4);
ssp1.add(t2);
ssp1.add(l5);
ssp1.add(t3);
ssp1.add(l6);
ssp1.add(t4);
ssp1.add(l7);
ssp1.add(t5);
ssp1.add(l8);
ssp1.add(t6);
ssp2.add(btnLoanHistory);
ssp2.add(btnLoanItem);
ssp2.add(btnProcessReturn);
sp1.add(ssp1);
sp1.add(ssp2);
panel.add(sp1);
}
frame.add(m1, BorderLayout.NORTH);
frame.add(contentPane, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Add p1 into contentPane, not into frame
The example above is good.
Have a nice day.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
public class DisplayItems3 {
public static void main(final String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Home Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// Top Panel
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.LIGHT_GRAY);
p1.setPreferredSize(new Dimension(950, 100));
JLabel l1 = new JLabel("All Library Items");
l1.setForeground(Color.BLACK);
l1.setPreferredSize(new Dimension(900, 50));
l1.setFont(l1.getFont().deriveFont(30.0f));
p1.add(l1);
// Content Panel
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(-1, 1));
p2.setBackground(Color.LIGHT_GRAY);
p2.setPreferredSize(new Dimension(950, 800));
p2.setAutoscrolls(true);
JScrollPane scrollPane = new JScrollPane(p2);
scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0, 0, 950, 800);
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setPreferredSize(new Dimension(950, 800));
contentPane.add(scrollPane, BorderLayout.CENTER);
for (int i = 0; i < 20; i++) {
JPanel sp1 = new JPanel();
sp1.setLayout(new FlowLayout());
sp1.setBackground(Color.WHITE);
sp1.setPreferredSize(new Dimension(900, 180));
sp1.setBorder(new LineBorder(Color.RED));
p2.add(sp1);
}
// contentPane.add(p2);
contentPane.add(p1, BorderLayout.NORTH);
// frame.add(p2);
// frame.setContentPane(contentPane);
frame.add(contentPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}

Two labels in the middle of center

I want to place two labels in the middle of the center of a window. I get it working with 1 label and the following code:
Screenshot: http://abload.de/img/scr1g6u0f.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("center1");
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Now I want another label next to the first label. I tried to use a flowlabel, but they are placed at the top of the BorderLayout.CENTER
Screenshot: http://abload.de/img/scr2a3u26.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Thanks!
Use a GridBagLayout without constraints:
JPanel centerPanel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel);

BorderLayout setSize issue

Before you come with GridBagLayout suggestions, I've tried that but I couldn't get it to work.
I want a frame with the size 800 x 800 and with a center panel of 600x600. Right now, when I run it the center panel is 600x578. Can someone tell me where it goes wrong? It's just 22 pixels.
public void createPlayground()
{
JFrame frame = new JFrame("ForFun Maze");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,1));
buttonPanel.setPreferredSize(new Dimension(100,600));
buttonPanel.setMaximumSize(new Dimension(100,600));
buttonPanel.setBackground(SIDEBAR);
JButton reset = new JButton();
reset.setText("Reset");
reset.addActionListener(new RestartListener());
reset.setSize(100,180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100,180);
pause.addActionListener(new PauseListener());
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100,180);
quit.addActionListener(new QuitListener());
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
if(level == 1)
{
array = glevel1;
}
CenterPanel centerPanel = new CenterPanel(array);
centerPanel.setPreferredSize(new Dimension(600,600));
centerPanel.setMinimumSize(new Dimension(600,600));
centerPanel.setBackground(BACKGROUND);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(SIDEBAR);
leftPanel.setPreferredSize(new Dimension(100,600));
JPanel northPanel = new JPanel();
northPanel.setBackground(SIDEBAR);
northPanel.setPreferredSize(new Dimension(800,100));
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(SIDEBAR);
bottomPanel.setPreferredSize(new Dimension(800,100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Size of centerpane"+centerPanel.getWidth()+"x"+centerPanel.getHeight());
}
Use pack() over setSize() (because you don't take the JFrame insets into account
Use setLocationRelativeTo(null) (after calling pack()) to center the frame.
Your calls to setSize() on JComponent's are useless because the LayoutManager's will override them (same goes for setBounds and setLocation).
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.xml.stream.Location;
public class Example {
public void createPlayground() {
JFrame frame = new JFrame("ForFun Maze");
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.setPreferredSize(new Dimension(100, 600));
buttonPanel.setMaximumSize(new Dimension(100, 600));
JButton reset = new JButton();
reset.setText("Reset");
reset.setSize(100, 180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100, 180);
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100, 180);
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
JPanel centerPanel = new JPanel();
centerPanel.setPreferredSize(new Dimension(600, 600));
centerPanel.setMinimumSize(new Dimension(600, 600));
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100, 600));
JPanel northPanel = new JPanel();
northPanel.setPreferredSize(new Dimension(800, 100));
JPanel bottomPanel = new JPanel();
bottomPanel.setPreferredSize(new Dimension(800, 100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Size of centerpane" + centerPanel.getWidth() + "x" + centerPanel.getHeight());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example().createPlayground();
}
});
}
}

Categories