I need to make a program that ends up looking like this:
My code so far is what's posted and I can't seem to wrap my head around how to make this look exactly like the image I provided. I just don't really know what else I can do, I've tried looking around for solutions, but I can't manage to combine that information with this to make it all work together.
So, I hope somebody else can help me here, it'd be greatly appreciated! :D
import java.awt.*; // Needed for BorderLayout class
import javax.swing.*; // Needed for Swing classes
/**
This class demonstrates how JPanels can be nested
inside each region of a content pane governed by
a BorderLayout manager.
*/
public class BorderPanelWindow extends JFrame
{
/**
Constructor
*/
public BorderPanelWindow()
{
// Set the title bar text.
setTitle("Border Layout");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a BorderLayout manager to the content pane.
setLayout(new BorderLayout());
// Create five panels.
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(3,3));
JPanel panel5 = new JPanel();
panel5.setLayout(new GridLayout(3,3));
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
// Create five buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
JButton button7 = new JButton("Button 7");
JButton button8 = new JButton("Button 8");
JButton button9 = new JButton("Button 9");
JButton button10 = new JButton("Button 10");
//Add buttons to panel4
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
panel4.add(button5);
panel4.add(button6);
// Add the buttons to the panels.
panel7.add(button7);
panel8.add(button8);
panel9.add(button9);
panel10.add(button10);
// Add the five panels to the content pane.
add(panel7, BorderLayout.NORTH);
add(panel8, BorderLayout.SOUTH);
add(panel9, BorderLayout.EAST);
add(panel4, BorderLayout.WEST);
//add(panel10, BorderLayout.WEST);
add(panel5, BorderLayout.CENTER);
// Pack and display the window.
pack();
setVisible(true);
}
/**
The main method creates an instance of the
BorderPanelWindow class, causing it to display
its window.
*/
public static void main(String[] args)
{
new BorderPanelWindow();
}
}
Update
So this is what I have so far in what I think an answer suggested me to do. Like I said I can't see my panel4 when I have button 10 on there as well, I'm sure it's something I'm just not seeing but still.
import java.awt.*; // Needed for BorderLayout class
import javax.swing.*; // Needed for Swing classes
/**
This class demonstrates how JPanels can be nested
inside each region of a content pane governed by
a BorderLayout manager.
*/
public class BorderPanelWindow extends JFrame
{
/**
Constructor
*/
public BorderPanelWindow()
{
// Set the title bar text.
setTitle("Border Layout");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a BorderLayout manager to the content pane.
setLayout(new BorderLayout());
// Create five panels.
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(3,3));
JPanel panel5 = new JPanel();
panel5.setLayout(new GridLayout(3,3));
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
// Create five buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
JButton button7 = new JButton("Button 7");
JButton button8 = new JButton("Button 8");
JButton button9 = new JButton("Button 9");
JButton button10 = new JButton("Button 10");
JButton button11 = new JButton("Button 11");
JButton button12 = new JButton("Button 12");
JButton button13 = new JButton("Button 13");
JButton button14 = new JButton("Button 14");
JButton button15 = new JButton("Button 15");
JButton button16 = new JButton("Button 16");
JButton button17 = new JButton("Button 17");
JButton button18 = new JButton("Button 18");
//Add buttons to panel4
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
panel4.add(button5);
panel4.add(button6);
//Add buttons to panel5
panel5.add(button11);
panel5.add(button12);
panel5.add(button13);
panel5.add(button14);
panel5.add(button15);
panel5.add(button16);
panel5.add(button17);
panel5.add(button18);
// Add the buttons to the panels.
panel7.add(button7);
panel8.add(button8);
panel9.add(button9);
panel10.add(button10);
// Add the five panels to the content pane.
add(panel7, BorderLayout.NORTH);
add(panel8, BorderLayout.SOUTH);
add(panel9, BorderLayout.EAST);
add(panel4, BorderLayout.WEST);
add(panel10, BorderLayout.WEST);
add(panel5, BorderLayout.CENTER);
// Pack and display the window.
pack();
setVisible(true);
}
/**
The main method creates an instance of the
BorderPanelWindow class, causing it to display
its window.
*/
public static void main(String[] args)
{
new BorderPanelWindow();
}
}
Update2
So now I polished it up a little bit and tried to do what you said but now it looks even worse so there's gotta be something I'm just not getting entirely lol. Hopefully you can see my mistake.
import java.awt.*; // Needed for BorderLayout class
import javax.swing.*; // Needed for Swing classes
/**
This class demonstrates how JPanels can be nested
inside each region of a content pane governed by
a BorderLayout manager.
*/
public class BorderPanelWindow extends JFrame
{
/**
Constructor
*/
public BorderPanelWindow()
{
// Set the title bar text.
setTitle("Border Layout");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a BorderLayout manager to the content pane.
setLayout(new BorderLayout());
// Create five panels.
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(3,3));
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(3,3));
// Create five buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
JButton button7 = new JButton("Button 7");
JButton button8 = new JButton("Button 8");
JButton button9 = new JButton("Button 9");
JButton button10 = new JButton("Button 10");
JButton button11 = new JButton("Button 11");
JButton button12 = new JButton("Button 12");
JButton button13 = new JButton("Button 13");
JButton button14 = new JButton("Button 14");
JButton button15 = new JButton("Button 15");
JButton button16 = new JButton("Button 16");
JButton button17 = new JButton("Button 17");
JButton button18 = new JButton("Button 18");
//Add buttons to panel1
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
//Add buttons to panel2
//Add the buttons to panel3
panel3.add(button7);
panel3.add(button8);
panel3.add(button9);
panel3.add(button10);
//Add the buttons to panel4
panel4.add(button11);
panel4.add(button12);
panel4.add(button13);
panel4.add(button14);
panel4.add(button15);
panel4.add(button16);
panel4.add(button17);
panel4.add(button18);
// Add the five panels to the content pane.
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.EAST);
add(panel4, BorderLayout.CENTER);
// Pack and display the window.
pack();
setVisible(true);
}
/**
The main method creates an instance of the
BorderPanelWindow class, causing it to display
its window.
*/
public static void main(String[] args)
{
new BorderPanelWindow();
}
}
Update3
Okay, I'm a little further now the big issue is that buttons 11-18 aren't coming up in the middle of buttons 7-10.
import java.awt.*; // Needed for BorderLayout class
import javax.swing.*; // Needed for Swing classes
/**
This class demonstrates how JPanels can be nested
inside each region of a content pane governed by
a BorderLayout manager.
*/
public class BorderPanelWindow extends JFrame
{
/**
Constructor
*/
public BorderPanelWindow()
{
// Set the title bar text.
setTitle("Border Layout");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a BorderLayout manager to the content pane.
setLayout(new BorderLayout());
// Create five panels.
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,3));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,2));
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(1,2));
// Create five buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
JButton button7 = new JButton("Button 7");
JButton button8 = new JButton("Button 8");
JButton button9 = new JButton("Button 9");
JButton button10 = new JButton("Button 10");
JButton button11 = new JButton("Button 11");
JButton button12 = new JButton("Button 12");
JButton button13 = new JButton("Button 13");
JButton button14 = new JButton("Button 14");
JButton button15 = new JButton("Button 15");
JButton button16 = new JButton("Button 16");
JButton button17 = new JButton("Button 17");
JButton button18 = new JButton("Button 18");
//Add buttons to panel1
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
//Add buttons to panel2
//Add the buttons to panel3
panel3.add(button7, BorderLayout.NORTH);
panel3.add(button8, BorderLayout.SOUTH);
panel3.add(button9, BorderLayout.EAST);
panel3.add(button10, BorderLayout.WEST);
//Add the buttons to panel4
panel4.add(button11);
panel4.add(button12);
panel4.add(button13);
panel4.add(button14);
panel4.add(button15);
panel4.add(button16);
panel4.add(button17);
panel4.add(button18);
// Add the five panels to the content pane.
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.EAST);
add(panel4, BorderLayout.CENTER);
// Pack and display the window.
pack();
setVisible(true);
}
/**
The main method creates an instance of the
BorderPanelWindow class, causing it to display
its window.
*/
public static void main(String[] args)
{
new BorderPanelWindow();
}
}
I'd make that with 3 grid layouts and a border layout. E.G.
GridLayout for left/right sections (each section a panel)
GridLayout for buttons 1-6
BorderLayout for buttons 7-10 + panel in CENTER for..
GridLayout for buttons 11-18
Update
Your edit 3 was very close, note extra comments and changes in code to see this:
import java.awt.*; // Needed for BorderLayout class
import javax.swing.*; // Needed for Swing classes
/**
This class demonstrates how JPanels can be nested
inside each region of a content pane governed by
a BorderLayout manager.
*/
public class BorderPanelWindow extends JFrame
{
/**
Constructor
*/
public BorderPanelWindow()
{
// Set the title bar text.
setTitle("Border Layout");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // better
// Add a BorderLayout manager to the content pane.
setLayout(new BorderLayout());
// Create five panels.
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,3));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,2));
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
JPanel panel4 = new JPanel();
// we need as many rows as needed (0) in 2 columns (2)
panel4.setLayout(new GridLayout(0,2));
// Create five buttons.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
JButton button7 = new JButton("Button 7");
JButton button8 = new JButton("Button 8");
JButton button9 = new JButton("Button 9");
JButton button10 = new JButton("Button 10");
JButton button11 = new JButton("Button 11");
JButton button12 = new JButton("Button 12");
JButton button13 = new JButton("Button 13");
JButton button14 = new JButton("Button 14");
JButton button15 = new JButton("Button 15");
JButton button16 = new JButton("Button 16");
JButton button17 = new JButton("Button 17");
JButton button18 = new JButton("Button 18");
//Add buttons to panel1
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
//Add buttons to panel2
//Add the buttons to panel3
panel3.add(button7, BorderLayout.PAGE_START);
panel3.add(button8, BorderLayout.PAGE_END);
panel3.add(button9, BorderLayout.LINE_END);
panel3.add(button10, BorderLayout.LINE_START);
//Add the buttons to panel4
panel4.add(button11);
panel4.add(button12);
panel4.add(button13);
panel4.add(button14);
panel4.add(button15);
panel4.add(button16);
panel4.add(button17);
panel4.add(button18);
// Add panel4 to the CENTER of panel3
panel3.add(panel4, BorderLayout.CENTER);
// Add the five panels to the content pane.
add(panel1, BorderLayout.LINE_START);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.LINE_END);
//add(panel4, BorderLayout.CENTER); // AND DON'T ADD IT HERE!
// Pack and display the window.
pack();
setVisible(true);
}
/**
The main method creates an instance of the
BorderPanelWindow class, causing it to display
its window.
*/
public static void main(String[] args)
{
new BorderPanelWindow();
}
}
Related
I am setting up a UI for a blackjack helper program and while I know its not the most beautiful way to do things, it makes sense.
The layering for what seems to be the upper layers is not working properly. Any suggestions?
The left should have four layers, as should the middle and the right side should have two layers between the keypad and the enter buttons. Image is attached below.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
// main method, runs the program
class BlackjackHelper
{
public static void main(String\[\] args)
{
Frame21 game = new Frame21();
//pop window with rules of game
}
}
// JFrame Construction
class Frame21 extends JFrame
{
// create needed components of program
JLabel questionDisplay = new JLabel("What is your first card?");
JLabel actionDisplay = new JLabel("Enter your first card");
JLabel dealerCardText = new JLabel("Dealer's Card:");
JLabel dealerCardDisplay = new JLabel("N/A");
JLabel handOneText = new JLabel("Hand One:");
JLabel handOneDisplay = new JLabel("N/A");
JLabel handTwoText = new JLabel("Hand Two:");
JLabel handTwoDisplay = new JLabel("N/A");
JLabel statsText = new JLabel("Win %:");
JLabel statsDisplay = new JLabel("N/A");
JButton aceButton = new JButton("A");
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton tenButton = new JButton("10");
JButton faceButton = new JButton("F");
JButton clearButton = new JButton("C");
JButton standButton = new JButton("Stand");
JButton hitButton = new JButton("Hit");
JButton doubleButton = new JButton("Double");
JButton splitButton = new JButton("Split");
JButton winButton = new JButton("Win");
JButton loseButton = new JButton("Lose");
JButton resetButton = new JButton("Reset All");
JButton enterButton = new JButton("Enter");
public Frame21()
{
// JFrame - the main area of the program
JFrame frame = new JFrame("Blackjack Helper");
// JPanel right - the rightside of the program
JPanel rightSide = new JPanel();
JPanel rightNorthSide = new JPanel();
rightNorthSide.setLayout(new GridLayout(3,4));
rightNorthSide.add(aceButton);
rightNorthSide.add(twoButton);
rightNorthSide.add(threeButton);
rightNorthSide.add(fourButton);
rightNorthSide.add(fiveButton);
rightNorthSide.add(sixButton);
rightNorthSide.add(sevenButton);
rightNorthSide.add(eightButton);
rightNorthSide.add(nineButton);
rightNorthSide.add(tenButton);
rightNorthSide.add(faceButton);
rightNorthSide.add(clearButton);
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
rightSide.add(rightNorthSide, BorderLayout.NORTH);
rightSide.add(rightSouthSide, BorderLayout.SOUTH);
frame.add(rightSide, BorderLayout.EAST);
// JPanel Center - the center of the program
JPanel center = new JPanel();
JPanel centerNorth = new JPanel();
centerNorth.add(questionDisplay, BorderLayout.NORTH);
centerNorth.add(actionDisplay, BorderLayout.SOUTH);
JPanel centerSouth = new JPanel();
JPanel centerSouthNorth = new JPanel();
centerSouthNorth.add(dealerCardText, BorderLayout.WEST);
centerSouthNorth.add(dealerCardDisplay, BorderLayout.EAST);
JPanel centerSouthSouth = new JPanel();
JPanel centerSouthSouthWest = new JPanel();
centerSouthSouthWest.add(handOneText, BorderLayout.NORTH);
centerSouthSouthWest.add(handOneDisplay, BorderLayout.SOUTH);
JPanel centerSouthSouthEast = new JPanel();
centerSouthSouthEast.add(handTwoText, BorderLayout.NORTH);
centerSouthSouthEast.add(handTwoDisplay, BorderLayout.SOUTH);
centerSouthSouth.add(centerSouthSouthWest, BorderLayout.WEST);
centerSouthSouth.add(centerSouthSouthEast, BorderLayout.EAST);
centerSouth.add(centerSouthNorth, BorderLayout.NORTH);
centerSouth.add(centerSouthSouth, BorderLayout.SOUTH);
center.add(centerNorth, BorderLayout.NORTH);
center.add(centerSouth, BorderLayout.SOUTH);
frame.add(center, BorderLayout.CENTER);
// JPanel left - the center of the program
JPanel left = new JPanel();
JPanel leftNorth = new JPanel();
JPanel leftNorthNorth = new JPanel();
JPanel leftNorthSouth = new JPanel();
JPanel leftSouth = new JPanel();
JPanel leftSouthNorth = new JPanel();
JPanel leftSouthSouth = new JPanel();
leftNorthNorth.add(standButton, BorderLayout.WEST);
leftNorthNorth.add(hitButton, BorderLayout.EAST);
leftNorthSouth.add(doubleButton, BorderLayout.WEST);
leftNorthSouth.add(splitButton, BorderLayout.EAST);
leftNorth.add(leftNorthNorth, BorderLayout.NORTH);
leftNorth.add(leftNorthSouth, BorderLayout.SOUTH);
leftSouthNorth.add(statsText, BorderLayout.WEST);
leftSouthNorth.add(statsDisplay, BorderLayout.EAST);
leftSouthSouth.add(winButton, BorderLayout.WEST);
leftSouthSouth.add(loseButton, BorderLayout.EAST);
leftSouth.add(leftSouthNorth, BorderLayout.NORTH);
leftSouth.add(leftSouthSouth, BorderLayout.SOUTH);
left.add(leftNorth, BorderLayout.NORTH);
left.add(leftSouth, BorderLayout.SOUTH);
frame.add(left, BorderLayout.WEST);
frame.setSize(1600, 200);
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The layering for what seems to be the upper layers is not working properly. Any suggestions? The left should have four layers.
A BorderLayout does not "layer".
Only a single component can be added to each of the 5 areas of the BorderLayout.
So, yes, you can create a JPanel and add multiple components to that panel, and then add the panel to an area of the BorderLayout.
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
However, the above code is incorrect. The default layout manager of a JPanel is the FlowLayout. So specifying BorderLayout constraints does nothing (and is very confusing).
If by "layers" you mean "rows", then you need to use a panel with a different layout manage. Maybe you can use a BoxLayout to add rows of panels.
Overall, your approach to creating panels with different components is valid, the problem is you also need to use the appropriate layout manager for each of your child panels.
Read the Swing tutorial on Layout Managers for more information about how each of the layout managers work.
I have panel with 2 buttons in BoxLayout. What i want is to add vertical space between the buttons.
Here is my code:
frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel panel = new JPanel();
panel.setBounds(new Rectangle(0, 5, 0, 0));
panel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
frame.getContentPane().add(panel, BorderLayout.WEST);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);
Add an invisible vertical component to the panel in between the two buttons:
JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);
panel.add(Box.createVerticalStrut(50));
JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);
I'm writing a program that does the following:
Create a frame and set its layout to FlowLayout.
* Create two panels and add them to the frame.
* Each panel contains three buttons. The panel uses FlowLayout. Here is my code:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
class Flow{
public static void main(String[] args){
JFrame f = new JFrame("Testing out these JPanels");
f.setSize(400, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setLayout(new FlowLayout());
JButton b = new JButton("button 1");
JButton butt = new JButton("button 2");
JButton bug = new JButton("button 3");
JButton button = new JButton("button 4");
JButton button5 = new JButton("button 5");
JButton button6 = new JButton("button 6");
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.setVisible(true);
p.setSize(200, 100);
JPanel pnl = new JPanel();
pnl.setLayout(new FlowLayout());
pnl.setSize(200,100);
p.add(b);
p.add(butt);
p.add(bug);
pnl.add(button5);
pnl.add(button);
pnl.add(button6);
f.add(button5);
}
}
When I run the program, the frame shows up but not my buttons or panels. Can someone please explain what I'm doing wrong?
You should add the JPanel p and pnl to JFrame
// f.add(button5); Comment this line button5 is already added to pnl
// Now add the two panel with JFrame.
f.add(p);
f.add(pnl);
And, for proper layout manager, go through this tutorial.
You need to add you JPanel to your JFrame
f.getcontentpane().add(p);
f.getcontentpane().add(pnl);
and then call setVisible() on the frame.
f.setVisible(true);
It is not showing because you set the visibility of frame to true before you add the buttons, therefore it only rendered the frame without buttons and by the time you re-size the JFrame it will show the buttons because it is rendered again.
solution:
set the visibility of the frame after you add the buttons
JFrame f = new JFrame("Testing out these JPanels");
f.setSize(400, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setLayout(new FlowLayout());
JButton b = new JButton("button 1");
JButton butt = new JButton("button 2");
JButton bug = new JButton("button 3");
JButton button = new JButton("button 4");
JButton button5 = new JButton("button 5");
JButton button6 = new JButton("button 6");
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.setVisible(true);
p.setSize(200, 100);
JPanel pnl = new JPanel();
pnl.setLayout(new FlowLayout());
pnl.setSize(200,100);
p.add(b);
p.add(butt);
p.add(bug);
pnl.add(button5);
pnl.add(button);
pnl.add(button6);
f.add(button5);
f.setVisible(true); //relocated here
You have created the panels, but have forgot to add the panels in that frame, exactly the same way you have added the "button 5"
try including this in code:
f.add(p);
f.add(pnl);
I need help with design and implement a basic GUI with multiple action components. The objective is to create two panels and six buttons. Each panel has three buttons.
So far, I coded in Java Virtual Machine. My codes seem pretty right but the output does not show buttons. Can you help to find why the buttons don't show?
By the way, I have a picture. I want my design like this one.
http://i1199.photobucket.com/albums/aa467/Jordan_Sanjaya/Picture1.png
My code:
import javax.swing.*;
import java.awt.*;
public class FlowlayoutExperiment extends JFrame {
FlowLayout experimentLayout = new FlowLayout();
private JButton firstButton = new JButton("Button 1");
private JButton secondButton = new JButton("Button 2");
private JButton thirdButton = new JButton("Button 3");
private JButton fourthButton = new JButton("Button 4");
private JButton fifthButton = new JButton("Button 5");
private JButton sixthButton = new JButton("Button 6");
public FlowlayoutExperiment ()
{
JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
}
public static void main(String[] args)
{
FlowlayoutExperiment frame = new FlowlayoutExperiment();
frame.setTitle("Button Panel Example");
frame.setSize(600, 75);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You seem to have forgetten to add the groups to anything
public FlowlayoutExperiment ()
{
JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
// This is important ;)
add(group1);
add(group2);
}
You're also setting the layout of the frame and not the groups
You might want to change...
JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));
//...
JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));
To
JPanel group1 = new JPanel();
group1.setLayout(new GridLayout(3,1));
//...
JPanel group2 = new JPanel();
group2.setLayout(new GridLayout(3,1));
The buttons are not displaying. It is because you do not add the 2 JPanels to Frame.
you can add 2 lines codes to constructor FlowlayoutExperiment. And it will make the buttons be shown.
this.getContentPane().add(group1);
this.getContentPane().add(group2);
public FlowlayoutExperiment() {
JPanel group1 = new JPanel();
setLayout(new GridLayout(3, 1));
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new GridLayout(3, 1));
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
this.getContentPane().add(group1);
this.getContentPane().add(group2);
}
I want to align JButton instances vertically. Can you please suggest a way to do so?
Jbutton jb= new Jbutton();
Jbutton jb1= new Jbutton();
JButton jb3= new JBUtton();
Refer the code below:
private void prepareUI() {
this.setUndecorated(true);
//Preparing control buttons
JButton jb = new MyButton("-");
jb.setActionCommand("Min");
jb.addActionListener(this);
jb1 = new MyButton("[]");//delete this line
jb1.setActionCommand("Max");
jb1.addActionListener(this);
JButton jb2 = new MyButton("X");
jb2.setActionCommand("Close");
jb2.addActionListener(this);
//Preparing panel
JPanel buttonPanel = new JPanel();
BoxLayout boxLayout1 = new BoxLayout(buttonPanel, BoxLayout.Y_AXIS);
buttonPanel.setLayout(boxLayout1);
buttonPanel.add(Box.createVerticalGlue());
buttonPanel.add(jb);
buttonPanel.add(jb1);
buttonPanel.add(jb2);
jb.setFocusable(false);
jb1.setFocusable(false);
jb2.setFocusable(false);
JPanel panel=new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.setLayout(boxLayout);
panel.add(Box.createHorizontalGlue())