I use a BorderLayout combined with a FlowLayout, which works perfectly when I set up the BorderLayout.NORTH. However, in the CENTER area I would like to add a JTextArea (to print out console), but when I create a panel, add JTextArea, after adding a panel to BorderLayout.CENTER nothing appears and became grey. I have tried several combinations and tricks, also I have checked several forum posts without luck. Here is my very simplified code (should run flawlessly, I have commented the problematic part if you remove the comment the bug present):
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(700, 500);
frame.setLayout(new BorderLayout()); //a frame BorderLayout elrendezésü
JPanel panel1 = new JPanel();
//panel.setBounds(0,0,700,500);
panel1.setBackground(new java.awt.Color(255,255,255));
panel1.setVisible(true);
panel1.setSize(new Dimension(140, 30));
JPanel panel2 = new JPanel();
//panel.setBounds(0,0,700,500);
panel2.setBackground(new java.awt.Color(255,255,255));
panel2.setVisible(true);
panel2.setSize(new Dimension(140, 30));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
btNewEntry.setPreferredSize(new Dimension(100,30));
btModifyEntry.setPreferredSize(new Dimension(100,30));
btDeleteEntry.setPreferredSize(new Dimension(100,30));
btShowEntries.setPreferredSize(new Dimension(100,30));
btExit.setPreferredSize(new Dimension(100,30));
lbFile.setPreferredSize(new Dimension(100,30));
lbFilePath.setPreferredSize(new Dimension(310,30));
btFileOpen.setPreferredSize(new Dimension(100,30));
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.setPreferredSize(new Dimension(500, 95));
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
/*Something wrong here! From this point, if this uncommented ruins the gui.*/
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
//panel.setBounds(0,0,700,500);
panel3.setBackground(new java.awt.Color(255,255,255));
//panel3.setVisible(true);
//panel3.setPreferredSize(new Dimension(30, 30));
JTextArea textArea = new JTextArea("Welcome to...!");
//textArea.setPreferredSize(new Dimension(50,50));
textArea.setBackground(new java.awt.Color(255,0,0));
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
Hi Andrew Thompson and user16320675, Thanks for the quick help, this solved my issue. How can I further improve the quality the code? I had to put only one setPrefferedSize() at the JTextArea, all other places I removed (without this the window shrinks to zero)
I reduced my code
image: https://ibb.co/Xj7tYKf
I have removed allsetPrefferedSize()
I put frame.setVisible(true) at the end, and also frame.pack() at the end
Here is my code:
package bloggui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Start {
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setBackground(new java.awt.Color(255,255,255));
JPanel panel2 = new JPanel();
panel2.setBackground(new java.awt.Color(255,255,255));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
frame.pack();
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setBackground(new java.awt.Color(135,206,250));
JTextArea textArea = new JTextArea("Welcome to...!");
textArea.setBackground(new java.awt.Color(255,255,255));
textArea.setPreferredSize(new Dimension(620, 400)); //Must not use setPreferredSize() [I must use it only once here!]
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
frame.pack(); //Must be at the end
frame.setVisible(true); //Must be at the end
}
}
Related
This is the picture I am trying to replicate
This is what I have (didn't add icon images yet)
I can't seem to find a solution, been staring at it for quite some time.
I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing. Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.
This is what I have, how can I go about it?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
The easiest way to do it would be to add an empty border to your JPanel (see this post on empty borders):
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));
Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column. Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (see How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,2,10,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
Here's a little demonstration I whipped up.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
You don't set the size of the JFrame and try and make the Swing components fit. You let the JFrame pack with all the Swing components.
You create a GridLayout JPanel inside of a FlowLayout JPanel. The FlowLayout JPanel uses an empty border of the appropriate size.
I used the image the OP provided to get the icons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155, 113, 110, 90);
images[1] = image.getSubimage(276, 113, 110, 90);
images[2] = image.getSubimage(155, 217, 110, 90);
images[3] = image.getSubimage(276, 217, 110, 90);
images[4] = image.getSubimage(155, 321, 110, 90);
images[5] = image.getSubimage(276, 321, 110, 90);
return images;
}
#Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));
JPanel innerPanel = new JPanel(new GridLayout(0, 2, 10, 10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
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.
public ATMgui1()
{
setTitle("ATM Transactions");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.PINK);
setContentPane(contentPane);
contentPane.setOpaque(false);
JLabel pinLabel = new JLabel("Enter your Pin:");
pinLabel.setOpaque(false);
pinTextField = new JTextField();
JButton pinButton = new JButton( "EnterPin OK");
pinButton.setOpaque(false);
JLabel pinChangeLabel = new JLabel("Enter your new Pin:");
JTextField pinChangeTextField = new JTextField();
JButton pinChangeButton = new JButton( "Change Pin");
JButton exitButton = new JButton("EXIT");
exitButton.addActionListener(e -> this.dispose());
pinButton.addActionListener(this);
JPanel pinPanel = new JPanel();
pinPanel.setLayout(new GridLayout(3, 3));
pinPanel.add(pinLabel);
pinPanel.add(pinTextField);
pinPanel.add(pinButton);
pinPanel.add(pinChangeLabel);
pinPanel.add(pinChangeTextField);
pinPanel.add(pinChangeButton);
pinPanel.add(exitButton);
contentPane.add(pinPanel, BorderLayout.CENTER);
I tried changing the background but it's not completely changing, the code is kinda all over the place and I think it's only in one section that's actually what I'm struggling with.
What you're seeing:
The JPanel that hold your components, your JTextFields, labels and buttons should be made non-opaque. Call .setOpaque(false) on it, and you should see the color underneath it.
For example:
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class PinkBackground {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JTextField(10));
panel.add(Box.createVerticalStrut(15));
panel.add(new JTextField(10));
panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
// panel.setOpaque(false); // ******* uncomment this! **********
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.PINK);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Make your pinPanel non-opaque. The contentPane, the component that you're setting to pink should remain opaque:
public ATMgui1() {
setTitle("ATM Transactions");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.PINK);
setContentPane(contentPane);
JLabel pinLabel = new JLabel("Enter your Pin:");
// pinLabel.setOpaque(false);
pinTextField = new JTextField();
JButton pinButton = new JButton("EnterPin OK");
// pinButton.setOpaque(false);
JLabel pinChangeLabel = new JLabel("Enter your new Pin:");
JTextField pinChangeTextField = new JTextField();
JButton pinChangeButton = new JButton("Change Pin");
JButton exitButton = new JButton("EXIT");
exitButton.addActionListener(e -> this.dispose());
pinButton.addActionListener(this);
JPanel pinPanel = new JPanel();
pinPanel.setOpaque(false); // !!
pinPanel.setLayout(new GridLayout(3, 3));
pinPanel.add(pinLabel);
pinPanel.add(pinTextField);
pinPanel.add(pinButton);
pinPanel.add(pinChangeLabel);
pinPanel.add(pinChangeTextField);
pinPanel.add(pinChangeButton);
pinPanel.add(exitButton);
contentPane.add(pinPanel, BorderLayout.CENTER);
}
I am having a problem with BorderLayout, that was set to the green JPanel side. It does not display elements on the EAST in a row order. Do I have to combine this with GridBagLayout ? Could someone advice me how should I tackle this problem?
Basically the problem is of displaying objects inside green area below when I am using
Current layout:
My aim is to achieve this layout:
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel.setLayout(layout);
panel2.setLayout(layout);
panel.add(BorderLayout.CENTER,object_btn);
panel.add(BorderLayout.WEST,oriented_btn);
panel.add(BorderLayout.WEST,programming_btn);
panel.add(BorderLayout.WEST,and_btn);
panel.add(BorderLayout.WEST,java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.getContentPane().add(panel);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
don't share same layout for multiple component and use box layout for left panel to positioning buttons
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel2.setLayout(layout);
panel.setLayout( new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(object_btn);
panel.add(oriented_btn);
panel.add(programming_btn);
panel.add(and_btn);
panel.add(java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.setLayout(new BorderLayout());
jf.getContentPane().add(panel ,BorderLayout.WEST);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
The first problem is that you're having your panels share a layout. You must use a new BorderLayout for each of them.
The second problem is that a BorderLayout can only have one component in each constraint position. When you try to add multiple components to the WEST position, each one replaces the one that was previously in that position:
panel.add(BorderLayout.WEST,oriented_btn);
// Implicitly removes oriented_btn from panel
panel.add(BorderLayout.WEST,programming_btn);
// Implicitly removes programming_btn from panel
panel.add(BorderLayout.WEST,and_btn);
// Implicitly removes and_btn from panel
panel.add(BorderLayout.WEST,java_btn);
The solution is to put them in their own container, such as a Box or a JPanel with a GridLayout:
Box box = Box.createVerticalBox();
// Or:
//JComponent box = new JPanel(new GridLayout(0, 1));
box.add(oriented_btn);
box.add(programming_btn);
box.add(and_btn);
box.add(java_btn);
panel.add(BorderLayout.WEST, box);
How can I make buttons like this?
JPanel jp = new JPanel();
JPanel jpB1 = new JPanel();
JPanel jpB2 = new JPanel();
JPanel jpB3 = new JPanel();
JButton jb1 = new JButton("button1");
JButton jb2 = new JButton("button2");
JButton jb3 = new JButton("button3");
...
JLabel jl = new JLabel("label");
...
jp.setLayout(new BorderLayout());
...
jpB1.add(jb1);
jpB2.add(jb2);
jpB3.add(jb3);
...
jp.add(jpB1, BorderLayout.NORTH);
jp.add(jpB2, BorderLayout.CENTER);
jp.add(jpB3, BorderLayout.SOUTH);
...
I tried this code on creating 3 panels and add them to the main panel. It shows two buttons on north and one button on south! Can someone help me?
Note, the default layout of JPanel is FlowLayout; a GridBagLayout with default constraints is centered in the frame's BorderLayout.CENTER. Also, pack() the enclosing Window and display it last. Using Initial Threads left as an exercise.
Addendum: A useful trick for solving layout problems is setting the background color of an enclosing container to a contrasting color, for example
jpB2.setBackground(Color.blue);
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Bouton2 {
public static void main(String[] args) {
final int LARGEUR = 400;
final int HAUTEUR = 300;
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JPanel jpB1 = new JPanel();
JPanel jpB2 = new JPanel(new GridBagLayout());
JPanel jpB3 = new JPanel();
JButton jb1 = new JButton("Cliquez ici");
JButton jb2 = new JButton("Je compte");
JButton jb3 = new JButton("J'agrandis");
JLabel jl = new JLabel("0 clic");
jp.setLayout(new BorderLayout());
jpB1.add(jb1);
jpB2.add(jb2);
jpB3.add(jb3);
jp.add(jpB1, BorderLayout.NORTH);
jp.add(jpB2, BorderLayout.CENTER);
jp.add(jpB3, BorderLayout.SOUTH);
jf.setTitle("Fenêtre Bouton2");
jf.setContentPane(jp);
jf.pack();
jf.setSize(LARGEUR, HAUTEUR);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}