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);
}
}
Related
Below is a picture of the basic interface I want created. Everything works fine except for the following:-
The JScrollPane isn't appearing when I try inserting lots of text
in the InformationDisplay JTextArea and I don't get why
The buttons I inserted in the unitButtonPanel in the availableUnits JTextArea isn't appearing at all
This is the part in my code that tackles point #1
InfoPanel with informationDisplay TextArea and JScrollPane
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
This is the part in my code that tackles point #2
Adding a textPanel 3 rows, 1 column to encapsulate 3 TextAreas
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
TextArea1 "availableUnits" 2 rows and 2 columns to encapsulate 4 buttons
JTextArea availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
JPanel unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
JButton ambulanceUnit = new JButton("Ambulance");
JButton diseaseControlUnit = new JButton ("diseaseControlUnit");
JButton gasControlUnit = new JButton("gasControlUnit");
JButton fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
This is the full code ready for compilation:-
import java.awt.*;
import javax.swing.*;
public class GameView extends JFrame {
private JPanel buttonPanel;
private JPanel infoPanel;
private JTextArea informationDisplay;
private JTextArea availableUnits;
private JPanel unitButtonPanel;
private JPanel disasterPanel;
private JTextArea disasterDisplay;
private JButton ambulanceUnit;
private JButton diseaseControlUnit;
private JButton gasControlUnit;
private JButton fireTruck;
public static void main(String[] args) {
new GameView();
}
public GameView() {
setSize(1000, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width/2) - (this.getWidth()/2);
int yPos = (dim.height/2) - (this.getHeight()/2);
this.setLocation(xPos,yPos);
JPanel title = new JPanel();
JLabel label1 = new JLabel("Invaded City");
title.add(label1);
add(title, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
for (int i = 0; i < 100; i++) {
JButton mapButton = new JButton();
addButton(mapButton);
}
add(buttonPanel, BorderLayout.CENTER);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
ambulanceUnit = new JButton("Ambulance");
diseaseControlUnit = new JButton ("diseaseControlUnit");
gasControlUnit = new JButton("gasControlUnit");
fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
JTextArea respondingUnits = new JTextArea(2,2);
//respondingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(respondingUnits);
JTextArea treatingUnits = new JTextArea(2,2);
//treatingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(treatingUnits);
add(textPanel, BorderLayout.EAST);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
disasterPanel = new JPanel();
disasterPanel.setLayout(new BorderLayout());
disasterDisplay = new JTextArea();
disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));
disasterPanel.add(disasterDisplay);
add(disasterPanel, BorderLayout.WEST);
pack();
setVisible(true);
}
public void addButton(JButton b) {
buttonPanel.add(b);
}
}
Output:-
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);
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.
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();
}
});
}
}
There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?