Here's what I actually want to put on a panel:
First logical block:
radio button 1 text field icon button
radio button 2 text field icon button
check box
Second logical block:
Label Spinner
Button
My first decision is to make Vertical Box Layout and put there two Horizontal Box Layouts - for each logical block. But the problem is with these blocks, what layouts to choose to describe this structure? I dislike GridBagLayout - it is very composite and difficult to understand, especially when code isn't yours. For the moment I see that Flow Layout and Grid Layout can be used. But Grid Layout, for example, stretches buttons to the width of a cell and if a button is with icon only it, it looks very strange then.
Hope you can advise me something.
For the first case you can use a simple GridLayout on the JPanel with 3 Rows each having a separate JPanel with FlowLayout having constraints, FLowLayout.LEFT. Have a look at this code example :
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
JTextField tfield1 = new JTextField(10);
JButton button1 = new JButton("Button 1");
topPanel.add(rbut1);
topPanel.add(tfield1);
topPanel.add(button1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut2 = new JRadioButton("RadioButton 2", false);
JTextField tfield2 = new JTextField(10);
JButton button2 = new JButton("Button 2");
middlePanel.add(rbut2);
middlePanel.add(tfield2);
middlePanel.add(button2);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JCheckBox cbox = new JCheckBox("CheckBox 1", false);
bottomPanel.add(cbox);
contentPane.add(topPanel);
contentPane.add(middlePanel);
contentPane.add(bottomPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}
OUTPUT :
And for the Second case, simply add first two components to the JPanel having default Layout. And for the third components, simply add components on to a JPanel having GridBagLayout, with no constraints.
EDIT #1 :
Or you can use this approach, for your second block.
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
//topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JLabel label1 = new JLabel("Label 1", JLabel.CENTER);
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
topPanel.add(label1);
topPanel.add(rbut1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridBagLayout());
JButton button1 = new JButton("Button 1");
middlePanel.add(button1);
basePanel.add(topPanel);
basePanel.add(middlePanel);
contentPane.add(basePanel, BorderLayout.PAGE_START);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}
Related
I'm currently building GUI with Java Swing.
My current code produces this.
The JTextArea of Product List makes the GUI looks awkward, how can I make the JTextArea looks like this, where it seems to have an extra row:
The GroupLayout code I'm using is:
gr.setVerticalGroup(gr.createSequentialGroup()
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(productName).addComponent(productText).addComponent(productList))
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(amount).addComponent(amountText).addComponent(prodScroll))
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(description).addComponent(desScroll))
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(addButton).addComponent(remButton)));
gr.setHorizontalGroup(gr.createSequentialGroup()
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(productName).addComponent(amount).addComponent(description).addComponent(addButton))
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(productText).addComponent(amountText).addComponent(desScroll).addComponent(remButton))
.addGroup(gr.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(productList).addComponent(prodScroll)));
I think the minority of people would choose to use GridBagLayout. However, I dislike it (among with GroupLayout) since it is "hard to use". I use nested panels instead with various Layout Managers. Using only BorderLayout and GridLayout you can achieve something like the following example, which is totally resizable, giving emphasis to "interaction" components (I mean, there is no reason to resize a constant-texted JLabel, right?)
I did not add any comments in purpose, so you can experiment with constants (and layout constraints) and see their reason of existence while having the documentations opened.
Code:
public class NestedLayoutManagersExample extends JFrame {
private static final long serialVersionUID = -7042997375941726246L;
private static final int labelsWidth = 80;
private static final int textFieldColumns = 15;
private static final int spaceBetweenAllComponents = 10;
public NestedLayoutManagersExample() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new GridLayout(1, 2, 50, 50));
contentPane.setBorder(BorderFactory.createEmptyBorder(spaceBetweenAllComponents, spaceBetweenAllComponents,
spaceBetweenAllComponents, spaceBetweenAllComponents));
setContentPane(contentPane);
add(createLeftPanel());
add(createRightPanel());
setLocationByPlatform(true);
pack();
}
private Component createRightPanel() {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel productListLabel = new JLabel("Product list");
mainPanel.add(productListLabel, BorderLayout.PAGE_START);
JList<String> productList = new JList<>();
DefaultListModel<String> listModel = new DefaultListModel<>();
Arrays.asList("Small Chair", "Big Chair", "Flying Chair").forEach(listModel::addElement);
productList.setModel(listModel);
JScrollPane listScrollPane = new JScrollPane(productList);
mainPanel.add(listScrollPane, BorderLayout.CENTER);
return mainPanel;
}
private Component createLeftPanel() {
JPanel mainPanel = new JPanel(new BorderLayout(spaceBetweenAllComponents, spaceBetweenAllComponents));
JPanel topPanel = new JPanel(new GridLayout(2, 1, spaceBetweenAllComponents, spaceBetweenAllComponents));
topPanel.add(createStraightPanel("Product Name"));
topPanel.add(createStraightPanel("Amount"));
mainPanel.add(topPanel, BorderLayout.PAGE_START);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("<html><p style='width:" + labelsWidth + "px';> Description");
label.setVerticalAlignment(JLabel.TOP);
centerPanel.add(label, BorderLayout.LINE_START);
centerPanel.add(createTextAreaPanel());
mainPanel.add(centerPanel, BorderLayout.CENTER);
return mainPanel;
}
private JPanel createTextAreaPanel() {
JPanel mainPanel = new JPanel(new BorderLayout(spaceBetweenAllComponents, spaceBetweenAllComponents));
JTextArea textArea = new JTextArea(1, textFieldColumns);
JScrollPane textAreaScrollPane = new JScrollPane(textArea);
mainPanel.add(textAreaScrollPane, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(new BorderLayout());
JButton addButton = new JButton("Add");
buttonsPanel.add(addButton, BorderLayout.LINE_START);
JButton removeButton = new JButton("Remove");
buttonsPanel.add(removeButton, BorderLayout.LINE_END);
mainPanel.add(buttonsPanel, BorderLayout.PAGE_END);
return mainPanel;
}
private Component createStraightPanel(String labelText) {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("<html><p style='width:" + labelsWidth + "px';>" + labelText);
mainPanel.add(label, BorderLayout.LINE_START);
JTextField textField = new JTextField(textFieldColumns);
mainPanel.add(textField, BorderLayout.CENTER);
return mainPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new NestedLayoutManagersExample().setVisible(true));
}
}
Preview:
Hello I am trying to add a border or a line break separating the north section from the rest of the JPanel. Basically using set border I have a border around the whole window but then want a line from one section of the border straight across horizontally to the other side. when i add a border to a JPanel that is added to BorderLayout.NORTH it puts a whole border inside the section. not the outline of the section. hope you know what i mean.
Attached I have a section of my code that is holding all my JPanel code in it so far. any help I would love, thanks.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class GamePanel extends JPanel {
private JTextPane playertext;
private JTextField wealthstring, currentwealth;
public GamePanel() {
super();
setLayout(new BorderLayout());
setBackground(Game.getBackgroundColor());
Border raised = BorderFactory.createRaisedBevelBorder();
Border lowered = BorderFactory.createLoweredBevelBorder();
setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(4, 4, 4, 4), (BorderFactory.createCompoundBorder(raised, lowered))));
add(northpanel(), BorderLayout.NORTH);
add(eastpanel(), BorderLayout.EAST);
}
private JPanel northpanel() {
Font northfont = new Font("Engravers MT", Font.BOLD, 12);
playertext = new JTextPane();
playertext.setFont(northfont);
playertext.setEditable(false);
playertext.setText("Player: \n" + Game.getName());
playertext.setBackground(Game.getBackgroundColor());
playertext.setBorder(new EmptyBorder(10,10,10,10));
wealthstring = new JTextField("Money: ");
wealthstring.setFont(northfont);
wealthstring.setEditable(false);
wealthstring.setHorizontalAlignment(wealthstring.RIGHT);
wealthstring.setBorder(null);
wealthstring.setBackground(Game.getBackgroundColor());
currentwealth = new JTextField();
currentwealth.setFont(northfont);
currentwealth.setEditable(false);
currentwealth.setHorizontalAlignment(wealthstring.RIGHT);
currentwealth.setBackground(Game.getBackgroundColor());
currentwealth.setBorder(null);
String wealthrounded = String.format("%.2f", Game.getMoney());
currentwealth.setText(wealthrounded);
JPanel wealthtext = new JPanel();
wealthtext.setLayout(new GridLayout(2, 1));
wealthtext.setBackground(Game.getBackgroundColor());
wealthtext.setBorder(new EmptyBorder(10,10,10,10));
wealthtext.add(wealthstring);
wealthtext.add(currentwealth);
JPanel northpanel = new JPanel();
northpanel.setLayout(new BorderLayout());
northpanel.setBackground(Game.getBackgroundColor());
northpanel.add(playertext, BorderLayout.WEST);
northpanel.add(wealthtext, BorderLayout.EAST);
return northpanel;
}
private JPanel eastpanel() {
JButton tab1 = new JButton("Tab 1");
JButton tab2 = new JButton("Tab 2");
JButton tab3 = new JButton("Tab 3");
JPanel easttabs = new JPanel();
easttabs.setLayout(new GridLayout(1, 3));
easttabs.add(tab1);
easttabs.add(tab2);
easttabs.add(tab3);
JPanel eastpanels = new JPanel();
eastpanels.setLayout(new BorderLayout());
eastpanels.add(easttabs, BorderLayout.NORTH);
return eastpanels;
}
}
Like this?
For that we would use a JSeparator.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class UnderlinePageStart {
private JComponent ui = null;
UnderlinePageStart() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JPanel pageStart = new JPanel(new BorderLayout(2,2));
ui.add(pageStart, BorderLayout.PAGE_START);
pageStart.add(new JLabel("Page Start", SwingConstants.CENTER));
// add a 'horizontal line'
pageStart.add(new JSeparator(), BorderLayout.PAGE_END);
ui.add(new JScrollPane(new JTextArea(5, 25)));
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
UnderlinePageStart o = new UnderlinePageStart();
JFrame f = new JFrame("Underline Page Start");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
public class Gridlayout extends JFrame {
public Gridlayout()
{
setTitle("xxx");
setSize(540,635);
this.setResizable(false);
this.setLocation(500,50);
initComponents();
setDefaultCloseOperation(3);
}
JLabel etykieta = new JLabel("Poziom trudności: ");
JPanel top = new JPanel(new GridLayout(2, 2));
JPanel mid = new JPanel(new GridLayout(0, 1));
JPanel bot = new JPanel(new GridLayout(1, 0));
JButton start= new JButton("Start");
JButton zakoncz = new JButton("Zakoncz");
JRadioButton latwy = new JRadioButton("Łatwy");
JRadioButton sredni = new JRadioButton("Średni");
JRadioButton trudny = new JRadioButton("Trudny");
ButtonGroup poziomTrudnosci= new ButtonGroup();
public void initComponents() {
this.getContentPane().add(top, BorderLayout.NORTH);
this.getContentPane().add(mid, BorderLayout.CENTER);
this.getContentPane().add(bot, BorderLayout.SOUTH);
start.setPreferredSize(new Dimension(1, 40));
zakoncz.setPreferredSize(new Dimension(1, 40));
mid.add(etykieta);
mid.add(latwy);
mid.add(sredni);
mid.add(trudny);
bot.add(start);
poziomTrudnosci.add(latwy);
poziomTrudnosci.add(sredni);
poziomTrudnosci.add(trudny);
}
public static void main(String[] args) {
new Gridlayout().setVisible(true);
}
}
How can I move this button from the left to the center of the window? They should stay close. I was trying everything but they still are on the left side.
If I understand your question correctly, you want to horizontally center the JRadioButtons in the JFrame, yes? Try the setHorizontalAlignment() method. Add the following lines to your initComponents() function:
latwy.setHorizontalAlignment(JRadioButton.CENTER);
sredni.setHorizontalAlignment(JRadioButton.CENTER);
trudny.setHorizontalAlignment(JRadioButton.CENTER);
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'm facing a problem with using BoxLayout.
In my example, I try to decrease the height of the text field and change the width of the buttons (as shown in green marker in the picture at the bottom). I know about the techniques setPreferredSize() and setMaximumSize(), but it did not work as it should. The line add(Box.createHorizontalGlue()) also did not help.
Thanks for any ideas.
public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
add(textField);
button = new JButton("Button 4");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
//add(Box.createHorizontalGlue());
}
}
First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.
For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...
Documentation says:
BoxLayout pays attention
to a component's requested minimum, preferred, and maximum sizes.
While you are fine-tuning the layout, you might need to adjust these
sizes. ... For example, a button's maximum size is generally the
same as its preferred size. If you want the button to be drawn wider
when additional space is available, then you need to change its
maximum size.
Then set components maximum size: c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height)); (c means button, label and textField in your example)
Edit 1:
Here is working source code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
label.setMaximumSize(new Dimension(Integer.MAX_VALUE, label.getMinimumSize().height));
add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, textField.getMinimumSize().height));
add(textField);
button = new JButton("Button 4");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
// add(Box.createVerticalGlue());
}
}
Edit 2:
If you want laid out Button 4 at the bottom of right column add this line add(Box.createVerticalGlue()); between add(textField); and button = new JButton("Button 4");.
As a quick remedy, you can use nested layouts, in the sense, that on the right side, create a JPanel with BorderLayout, put a JPanel(say compPanel) at the CENTER and a JPanel(say buttonPanel) at PAGE_END location. Now use a new JPanel(say panel) with GridLayout and put all the components on it, and place this compPanel inside centerPanel. Place JButton(button4) inside buttonPanel as is.
BoxLayout on the contrary, respects the preferred size of a given JComponent, which is usually calculated based on the content the JComponent holds or given explicity, hence components do not tend to align well with respect to other given components.
Here is the working example :
import java.awt.*;
import javax.swing.*;
public class Testy extends JPanel {
private JPanel panel;
private JPanel buttonPanel;
public Testy() {
setLayout(new BorderLayout(5, 5));
JPanel compPanel = new JPanel();
panel = new JPanel(new GridLayout(6, 1, 5, 5));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
panel.add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
panel.add(textField);
compPanel.add(panel);
buttonPanel = new JPanel();
button = new JButton("Button 4");
buttonPanel.add(button);
add(compPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel centerPanel = new JPanel();
frame.getContentPane().setLayout(new BorderLayout(5, 5));
centerPanel.setBackground(Color.DARK_GRAY);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(this, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Testy().constructGUI();
}
});
}
}
OUTPUT :
This should get close, based on your draw, just need to work on that component
below the JLabel (using setPreferredSize()):
JPanel main = new JPanel(new GridLayout(1, 2));
JPanel left = new JPanel();
//left.setPreferredSize(some size);
JPanel right = new JPanel(new GridLayout(6, 1));
//right.setPreferredSize(some size);
right.add(new JButton("Button 1"));
//...
right.add(new JButton("Button 4"));
main.add(left);
main.add(right);