I am trying to set the layout of a JFrame to be a grid bag layout. I want to to essentially look like 4 grids of equal size, but with the bottom 2 merged into one panel. I am adding JPanels to each. However, I get 3 small grids at the middle of the JFrame, not properly sized. It looks like this instead.
My code is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hangman extends JPanel{
private String word;
private JPanel hA, gL, letters;
public void setupLayout(JFrame window){
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
hA = new JPanel();
hA.setBackground(Color.blue);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
window.add(hA, c);
gL = new JPanel();
gL.setBackground(Color.green);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
window.add(gL, c);
letters = new JPanel();
letters.setBackground(Color.black);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
window.add(letters, c);
}
public void startWindow(){
JFrame window = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = (int) screenSize.getHeight();
int windowHeight = (int) ((screenHeight / 4) * 3);
window.setSize(windowHeight, windowHeight);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupLayout(window);
window.setVisible(true);
}
public void startGame(){
Prep prepare = new Prep();
word = prepare.findWord(true);
startWindow();
}
public static void main(String[] args){
Hangman start = new Hangman();
start.startGame();
}
}
It's not very important what Prep does. It just takes a random word from a text file. I couldn't see how it would affect the layout. To recap, I need to make it so each of the 4 grid spaces span one fourth of the window, and then make the bottom 2 cells merged with a JPanel added to each of them. Any help is appreciated. Thank you in advance.
EDIT:
I needed to set the weight of all of them to be 1. It is solved.
GridBagLayout displays each component at its preferred size. Since you didn't add any components to the panels you just see a small panel.
If you want the panels to fill the space available then you need to play with the constraints.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information and working examples. You will want to look at the "fill" and "weightx/weighty" constraints.
and then make the bottom 2 cells merged
you will also need to look at the "gridwidth/gridheight" constraints.
Related
I am trying to position components on JPanel using GridBagLayout but the output I am getting is completely off from what I expect. Hope to get some clarity with brilliant-minded ppl in stackoverflow:).
Below I have provided a piece of code and screentshot of the program. My questions are:
Why the JLabel Choose measure system to convert is not on Y-axis = 1? As to my knowledge, c.gridy=1 is one pixel downward, but the label is stuck on the top leaving no space from the Frame title. And also, why is it positioned so weird, i.e., not really in the center, nor in the start?
Why is there such a big space between ComboBoxes From... and To..., but there is no space between ComboBox To... and TextField Enter value here...?
Here is the code:
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
getContentPane().add(container, BorderLayout.NORTH);
TitledBorder outputCenter;
GridBagConstraints c = new GridBagConstraints();
label = new JLabel("Choose measure system to convert");
label.setFont(new Font("Times New Roman", Font.PLAIN, 20));
c.gridx = 0;
c.gridy = 1;
container.add(label, c);
fromList = new JComboBox<String>(convertFrom);
c.gridx = 0;
c.gridy = 2;
container.add(fromList, c);
toList = new JComboBox<String>(convertTo);
c.gridx = 1;
c.gridy = 2;
container.add(toList, c);
//Field where user enters the value to be converted
input = new JTextField("Enter value here...");
input.setPreferredSize(new Dimension(150,30));;
input.setEditable(true);
input.setBackground(Color.WHITE);
input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
input.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
input.setText("");}});
c.gridx = 2;
c.gridy = 2;
container.add(input, c);
And here is the screentshot:
EDIT: If I change the code from:
label = new JLabel("Choose measure system to convert");
label.setFont(new Font("Times New Roman", Font.PLAIN, 20));
c.gridx = 0;
c.gridy = 1;
container.add(label, c);
label = new JLabel("Choose measure system to convert");
label.setFont(new Font("Times New Roman", Font.PLAIN, 20));
c.gridx = 1; // changed this line
c.gridy = 1;
container.add(label, c);
The outcome is like this:
This is very confusing me as why changing the position of one component effects everything?
GridBagConstraints sets up the frame into effectively a grid. The width and height of the cells in the grid are determined by the size of the data in the cell by default unless otherwise specified. So if you want to add some space in-between cells I suggest ipadx and ipady. You can also utilize anchor for adjusting your data in the cell. I also suggest weightx and weighty for adjusting the actual cell size.
So imagine something like this as your current set up:
EDIT: Example of what your new GBC looks like. The numbers are (gridx,gridy)
Why the JLabel Choose measure system to convert is not on Y-axis = 1? As to my knowledge, c.gridy=1 is one pixel downward
You're confusing yourself, c.gridy = 1 is not positioning it 1 pixel downward but rather on the next row, but as there's no previous row, then it takes the first row. For reference see: GridBagConstraints#gridy which says the following:
Specifies the cell at the top of the component's display area, where the topmost cell has gridy=0. The value RELATIVE specifies that the component be placed just below the component that was added to the container just before this component was added.
Next question:
And also, why is it positioned so weird, i.e., not really in the center, nor in the start?
It is centered in its own cell, if you want to center it on the JFrame, then you might need to create it on its own gridx = 1 and the rest of components on the other ones (0 and 2) or make it span 2 or more columns based on you want it to look like...
Why is there such a big space between ComboBoxes From... and To..., but there is no space between ComboBox To... and TextField Enter value here...?
It is because your program is giving it all the extra space because of the large text on the first cell...
You can have something like this:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutExample {
private JFrame frame;
private JPanel pane;
private JLabel label;
private JTextField field;
private JComboBox<String> box1;
private JComboBox<String> box2;
private GridBagConstraints gbc;
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutExample()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
label = new JLabel("Choose measure system to convert");
box1 = new JComboBox<>(new String[] {"From..."});
box2 = new JComboBox<>(new String[] {"To..."});
field = new JTextField(10);
gbc.insets = new Insets(5, 5, 5, 5); //We add extra space at top, left, bottom, right of each component
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3; //We make our text to span 3 cells
pane.add(label, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1; //We return the spanning to 1 single cell
pane.add(box1, gbc);
gbc.gridx = 1;
pane.add(box2, gbc);
gbc.gridx = 2;
pane.add(field, gbc);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Which produces the following output:
I'm trying to create a console using Java Swing (GridBagLayout).
I don't know why, but as you can see at the left margin, grids don't have the correct size.
It's supposed to be shown this way:
Where light blue is the list, green the image, orange the text panel and yellow the text field.
I don't know how to make the list bigger and the image smaller. Too, the text field's grid is binded to the list one, even tough the list is on y 1 and the text field on y 2.
Here's some code.
// Command List
DefaultListModel<String> listInput = new DefaultListModel<String>();
JList<String> list = new JList<String>(listInput);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list);
list.setBackground(new Color(160, 160, 160));
list.setSelectionBackground(new Color(150, 150, 150));
scrollPane.setPreferredSize(new Dimension(20, 20));
manager.setCommandList(listInput);
c.insets = new Insets(2, 2, 2, 2);
c.ipady = 0;
c.ipadx = 100;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 2;
c.weightx = 0.1;
c.weighty = 0.6;
c.fill = GridBagConstraints.BOTH;
console.add(scrollPane, c);
// Image Displayer
JLabel image = new JLabel(new ImageIcon());
manager.setImageField(image);
c.insets = new Insets(0, 0, 0, 0);
c.ipady = 0;
c.ipadx = 0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.1;
c.weighty = 0.3;
c.fill = GridBagConstraints.BOTH;
console.add(image, c);
where 'c' is a grid bag constraint and console the main JPanel.
As you can see, the list has a grid height of 2 and weight of 0.6, and the image a grid height of 1 and weight of 0.9, so not sure why the list is 4 times smaller than the image.
Another issue, I've added a listener to the JLabel holding the image (on resize), anyways, it isn't called. Should I add the listener to the main panel? as the image is only being resized by the layout manager.
Thanks ^^
EDIT:
SSCCE:
package co.relieved.jelly.application.display.swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
#SuppressWarnings("serial")
public class Test extends JPanel {
static JLabel image;
public static void main(String[] args) {
display();
try {
BufferedImage buffer = ImageIO
.read(new File("/home/juanco/Pictures/Screenshot from 2016-02-08 22-43-22.png"));
image.setIcon(new ImageIcon(
buffer.getScaledInstance(image.getWidth(), image.getHeight(), BufferedImage.SCALE_SMOOTH)));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public Test() {
super(new GridLayout(1, 1));
JTabbedPane tabs = new JTabbedPane();
/*** >>> Console Pane <<< ***/
JPanel console = new JPanel();
console.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 0;
c.ipady = 0;
c.gridwidth = 1;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
// Console Screen
JTextPane screen = new JTextPane();
screen.setEditable(false);
c.gridx = 1;
c.gridy = 0;
c.gridheight = 2;
c.weightx = 0.8;
c.weighty = 1;
console.add(screen, c);
// Console Input
JTextField input = new JTextField();
c.insets = new Insets(2, 0, 2, 0);
c.ipady = 3;
c.gridy = 2;
c.gridheight = 1;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
console.add(input, c);
// Command List
DefaultListModel<String> listInput = new DefaultListModel<String>();
listInput.setSize(1);
JList<String> list = new JList<String>(listInput);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list);
c.insets = new Insets(2, 2, 2, 2);
c.ipady = 0;
c.ipadx = 100;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.weightx = 0.1;
c.weighty = 0.6;
c.fill = GridBagConstraints.BOTH;
console.add(scrollPane, c);
// Image Displayer
image = new JLabel(new ImageIcon());
c.insets = new Insets(0, 0, 0, 0);
c.ipadx = 0;
c.gridy = 0;
c.gridheight = 1;
c.weighty = 0.3;
console.add(image, c);
// General
tabs.addTab("Console", console);
/*** >>> Logs Pane <<< ***/
JPanel logs = new JPanel();
tabs.addTab("Logs", logs);
// Setup
tabs.setSelectedIndex(0);
add(tabs);
}
static void display() {
JFrame frame = new JFrame("Relieved Console");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 500));
frame.add(new Test(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Here's some code.
Which doesn't help. The grid can only be completed with the entire code. That is we need to know the gridwidth and gridheight for all components in order to determine the allocation of space to each component in the grid.
the text field's grid is binded to the list one, even tough the list is on y 1 and the text field on y 2.
You can't just randomly assign a component to a grid. The component will only go to grid 2 if the component above it has a grid height of 2. So basically each of your columns needs to have components with a total grid height of 3.
I don't know how to make the list bigger
Setting a preferred size of (20, 20) doesn't help. Anyway you should not be using the setPreferredSize() method.
Instead you should be using:
list.setVisibleRowCount(...);
to specify the visible rows. Then the JList can determine its own preferred size.
Another layout option is to use nested panels which can simplify the layout.
So you could start with a "west" panel that uses a BorderLayout. Then you add the label to "PAGE_START" and the list to "CENTER".
Then you create a "center" panel. Add the main component to the "CENTER" and the text field to the "PAGE_START".
Then you add the two panels to the frame:
frame.add(westPanel, BorderLayout.LINE_START);
frame.add(centerPanel, BorderLayout.CENTER);
Edit:
Sorry, I take back my comment about making each column have a grid height of 3. You can't just specify a total grid height of 3 because you only have 2 components in each column, so each component can only have a height of 1.
Check out my answer in this posting: Why does this GridBagLayout not appear as planned? for a hack that allows you to manipulate gridHeight/Weight by using invisible components in a row/column.
However, I don't recommend that approach. it will be far easier to use my suggestion of nested panels using a BorderLayout (or some other layout manager on the nested panels).
It seems to me that you would need to also specify the gridwidth and/or gridheight for the text field as well.
GridBagLayout is like a glorified GridLayout. It will attempt to align all of the components with the nearest grid spaces around it. Oracle's Tutorials also describe this behavior.
Much of the time, issues with laying out components using GridBagLayout come from the other components being added to the layout instead of the apparent problem child.
I fixed it splitting the "console" JPanel in two, a border layout panel to the left and a grid bag layout panel to the right. As #camickr suggested
getting a bit confused with GridBagLayout.
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
public class MihatteFrame extends JFrame {
private JTextArea area;
private JTextField textField;
private Button b;
final static boolean shouldFill = true;
public MihatteFrame() {
setTitle("見張ってしながら...");
setSize(500,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
area = new JTextArea(5, 15);
area.setEditable(false);
area.setCursor(null);
area.setOpaque(false);
area.setFocusable(false);
area.setLineWrap(true);
area.setWrapStyleWord(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
this.add(area, c);
textField = new JTextField(20);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 2;
c.gridy = 0;
c.anchor = GridBagConstraints.CENTER;
this.add(textField);
//textField.addActionListener(this);
b = new Button("Proceed");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 2;
c.gridy = 0;
this.add(b);
setVisible(true);
}
public void displayText(String text) {
area.setText(text);
//textField.setText(text)
}
}
I understand the basics and its working well, it`s sorting out the resizing (specifically downsizing) issue.
When the text areas weight is 0 and the text fields weight is 1, there is no problem with resizing, but the text area is in the centre and not right up against the left-hand side, despite the anchoring.
When the text areas weight is anything above 0 and the text fields weight correlates (eg. 0.2/0.8), everything looks snug as a bug, and upsizing is fine, but downsizing, the text field is reduced to 1 character wide.
I dont mind the text area resizing with the frame, but I want it to return to its original size when the frame is made smaller, not shove the textfield against the proceed button.
I have read that I may need to put this in a JPanel border layout - am not sure what this would look like in code, so if anyone could tell me what goes where if that is the case, would be grateful.
So questions:
Is there a way to make the text area stay anchored to the West AND
not have the textfield reduced to a small size when resized?
Why are my anchors not working?
Do I need to put the text area in a JPanel with relation to another layout, and if so, where does the code go?
Why does everyone fear GridBagLayout? The amount of "I avoid this at all costs" I`ve seen...
Thank you!
(Ps. am halfway through implementing actionlistener, so if it looks half-complete, that`s why).
Use ipadx for the JTextField. I would suggest also to place the JTextArea in a JScrollPane.
You use c.gridx = 2 twice for the JTextField and JButton.
It's better to post SSCCE to let us check what's wrong.
I`m trying to create a Java Applet, I was looking around on the Java website and I found an nice example which I edited.
In this example there are 3 buttons, One that will be like a header, one that will be like a footer
and one that will be like the body. My problem is that, the middle (body) button will not expand fully.
If you run this code, you will see what I mean.
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class main {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("hi");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipady = 40;
pane.add(button, c);
button = new JButton("Hello, i will not expand");
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0; //request any extra vertical space
c.weightx=1.0;
c.anchor = GridBagConstraints.CENTER; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridy = 1; //third row
pane.add(button, c);
button = new JButton("fr");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("main");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Looks like it's not the middle button's fault, but the bottom button that is not expanding, if you set the bottom button as so c.fill = GridBagConstraints.BOTH; then it takes up the empty space below.
Do you want the middle to be bigger? You might want to change the weight of the bottom button to .5
c.weighty = .5; //request any extra vertical space
and see if that is what you are looking for?
If you want the middle to be much bigger then the top and bottom, you may need to make more grid spaces, so rather then 1,1,1, try 1,3,1 etc
I was looking around on the Java website and I found an nice example which I edited.
So I take it you don't have to use a GridBagLayout?
Use a BorderLayout, make the expandable button at position BorderLayout.CENTER.
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
Here is a tough one for you guys :)
Basically I have a GridBagLayout with 2 columns : a list wrapped in a scrollpane in each one. The scrollpanes are stretched in BOTH direction.
If I progressively reduce the height of this panel (by dragging the window's edge), I see "random" resizing happening on the scrollpanes:
second one's width gets reduced when first's Hscroll bar appears
then, second one's width gets shrinked again for no reason...
If i don't wrap my components, don't get this behavior.
And if you replace the right hand list with a tree, it will behaves differently:
shrinking the window's height bellow 380-ich px, the tree gets resized...
If i don't wrap my components, the tree gets resized anyway if you keep resizing the window !
Do you guys have any idea what's going on ???
PS: the actual layout i am trying to build is more complex than this example. In the mean time i use SpringLayout to do what i want but it requires to much (not so beautiful) things to setup
protected static ListModel newListModel(int n) {
DefaultListModel lm = new DefaultListModel();
for (int i = 0; i < n; ++i)
lm.addElement("AAA");
return lm;
}
protected static JComponent createContentPane() {
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
gbc.gridx = 0;
pane.add(new JScrollPane(new JList(newListModel(12))), gbc);
++gbc.gridx;
pane.add(new JScrollPane(new JList(newListModel(4))), gbc);
return pane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(createContentPane());
f.setSize(800, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
I have an application with several ScrollPanes in a GridBagLayout, and they also exhibit sudden resizes when I resize the window. It seems that a jump occurs when the actual size of the ScrollPane steps over its "preferred size". I have found a workaround: set the preferred size to 1x1 pixels. If the component has positive weight and stretches BOTH, it will still occupy the whole cell, but will not jump. If you need several cells to resize in different proportions, you can set preferred size of another one to, say, 2x1 pixels.
this is basic principles of GridBagLayout, you forgot for define anchor, then you can be able to fix placed JComponent to the rellative Point, and I think that GridBagLayout complicated your GUI, this LayoutManager is better use for placing lots of JComponents to the one container,(without using nested layout)
for example
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderPanels extends JFrame {
private static final long serialVersionUID = 1L;
public BorderPanels() {
setLayout(new GridBagLayout());// set LayoutManager
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 20;
add(panel1, gbc); // add compoenet to the COntentPane
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "60pct"));
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 60;
//gbc.insets = new Insets(2, 2, 2, 2);
add(panel2, gbc); // add component to the COntentPane
JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
gbc.gridy = 2;
gbc.weightx = gbc.weighty = 20;
gbc.insets = new Insets(2, 2, 2, 2);
add(panel3, gbc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
pack();
setVisible(true); // important
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
public void run() {
BorderPanels borderPanels = new BorderPanels();
}
});
}
}
to avoiding against to complicating simple things
use GridLayout (all JComponents have got same Dimmension on the screen)
use BoxLayout or BorderLayout