BorderLayout setSize issue - java

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();
}
});
}
}

Related

JPanel inside JPanel in JAVA

public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...

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);
}
}

JPanel not shown properly

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.

Can't get panels to display in JFrame

I am trying to design a calculator shell (non-functioning calculator) using Java but for some reason I cannot get the program to display my buttons. What am I doing wrong here?
import java.awt.*;
import javax.swing.*;
import java.awt.color.*;
public class Calculator extends JFrame {
public Calculator() {
JPanel P1 = new JPanel();
P1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i = 1; i <= 9; i++) {
P1.add(new JButton("" + i));
}
P1.add(new JButton("" + 0));
P1.add(new JButton("."));
P1.add(new JButton("*"));
P1.add(new JButton("/"));
P1.add(new JButton("+"));
P1.add(new JButton("-"));
P1.add(new JButton("="));
P1.setBackground(Color.cyan);
P1.setForeground(new Color(100, 1, 1));
//Content panel
JPanel P2 = new JPanel();
P2.setLayout(new BorderLayout());
P2.add(new JTextField("Hello world"), BorderLayout.NORTH);
P2.add(P1, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator frame = new Calculator();
frame.setTitle("Simple Calculator");
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
OK here is what I have now...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
Calculator()
{
super("Wk 3 Calculator"); setBounds(100,100,300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(p1);
con.add(p2);
p1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i =1; i <=9; i++){
p1.add(new JButton ("" + i));
}
p1.add(new JButton (""+0));
p1.add(new JButton ("."));
p1.add(new JButton ("*"));
p1.add(new JButton ("/"));
p1.add(new JButton ("+"));
p1.add(new JButton ("-"));
p1.add(new JButton ("="));
//Content panel
p2.setLayout(new BorderLayout());
p2.add (new JTextField("Hello world"),BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
//Frame specs
Calculator frame = new Calculator();
frame.setSize(250,250);
frame.setTitle("Simple Calculator");
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){new Calculator();}
}
It is still not working :(
You're not adding anything to the frame...
Try something along the lines of
add(P1, BorderLayout.NORTH);
add(P2, BorderLayout.SOUTH);
For starters...
You might like to have a look at
Creating a UI with Swing
Initial Threads
Code Conventions for the Java Programming Language
You are not adding the panels to the Frame. Put add(p1, BorderLayout.NORTH) and add(p2, BorderLayout.SOUTH) at the end of your constructor.
public Calculator()
{
//rest of the construtor
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
}
And rename them so they start with a small letter. Capital letters are for class names.
add your P2 inside JFrame
this.add(P2);
this.pack();
this.add(P2);
this.pack();
this.repain();
Well:
1). Call Swing-related code on the EDT (Event Dispatch Thread), not on the main thread. See here for more details.
2). You're not adding the P1 and P2 panels to the JFrame, so when you display the frame, it doesn't contain anything. Try calling add (P1) and add (P2).
3). You should call frame.pack() before you call frame.setVisible (true).
EDIT:
I rewrote your code (you still have a loong way into making a functional application, but it's a start):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
private JFrame frame;
public Calculator()
{
frame = new JFrame ("Simple Calculator");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout (new BorderLayout ());
JPanel panel = new JPanel(new GridLayout(4, 4));
JPanel p2 = new JPanel(new BorderLayout());
for (int i = 1; i <= 9; i++)
{
panel.add (new JButton ("" + i));
}
panel.add(new JButton (""+0));
panel.add(new JButton ("."));
panel.add(new JButton ("*"));
panel.add(new JButton ("/"));
panel.add(new JButton ("+"));
panel.add(new JButton ("-"));
panel.add(new JButton ("="));
frame.add(panel, BorderLayout.CENTER);
frame.add (new JTextField("Hello world"), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
#Override
public void run ()
{
new Calculator();
}
});
}
}

too much empty space inside jpanel with gridlayout

I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png
Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Categories