JFrame use one component multiple times [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a fixture program. I have a page with the button CREATE on. When the user hits create it adds a new button onto the next page. How could I get it so that when the user hits CREATE it adds a new button and the previous buttons move down by say 50 pixels(eg).
createFixtures.setLayout(null); //Create a new fixture panel components
createPanelLabel.setBounds(160, 30, 500, 50);
createPanelLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
createPanelLabel.setForeground(Color.WHITE);
southBar4.setBounds(0, 730, 500, 1);
northBar4.setBounds(0, 100, 500, 1);
backButton2.setBounds(20, 750, 150, 60);
createButton2.setBounds(320, 750, 150, 60);
sportLabel.setBounds(30, 150, 500, 40);
sportLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
sportLabel.setForeground(Color.WHITE);
descriptionLabel.setBounds(30, 300, 500, 40);
descriptionLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
descriptionLabel.setForeground(Color.WHITE);
dateLabel.setBounds(30, 450, 500, 40);
dateLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
dateLabel.setForeground(Color.WHITE);
resultLabel.setBounds(30, 600, 500, 40);
resultLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
resultLabel.setForeground(Color.WHITE);
sportDropDown.setBounds(250, 150, 200, 40);
descriptionField.setBounds(250, 300, 200, 100);
descriptionField.setFont(new Font("TimesRoman", Font.PLAIN, 20));
descriptionField.setLineWrap(true);
descriptionField.setWrapStyleWord(true);
createFixtures.add(southBar4);
createFixtures.add(northBar4);
createFixtures.add(createPanelLabel);
createFixtures.add(backButton2);
createFixtures.add(createButton2);
createFixtures.add(sportLabel);
createFixtures.add(descriptionLabel);
createFixtures.add(dateLabel);
createFixtures.add(resultLabel);
createFixtures.add(sportDropDown);
createFixtures.add(descriptionField);
container.add(fixtures, "2"); //Labels each panel with a number allowing me to call the number when switching panels also adds panels to main container
container.add(loginPanel, "3");
container.add(createFixtures, "4");
container.add(editFixtures, "5");
container.add(adminFixturesFootball, "6");
container.add(adminFixturesSwimming, "7");
container.add(adminFixturesTennis, "8");
createButton2.addActionListener(new ActionListener() { //Back button listener, switches back to ADMIN fixtures panel
#Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(container, "6");
String descriptionText = descriptionField.getText();
fixtureDescButton.setText( descriptionText );
fixtureDescButton2.setText( descriptionText );
}
});

Again, you should create a factory method that creates your JPanel.
i.e.,
public JPanel createPanel(String buttonText, String dataOnPanel) {
JPanel panel = new JPanel();
JButton myButton = new JButton(buttonText);
myButton.addActionListener(new ButtonListener());
panel.add(myButton);
// add jtextarea if need be with the dataOnPanel String,...
return panel;
}
Edit
You ask
What difference does this make?
You can't add a single component to the multiple containers, but you can create similar components and add them to multiple containers. Using a factory method allows you to do this. For more details on your problem, consider creating and posting a minimal, compilable, runnable example program.
As an aside you appear to be using a null layout and calling setBounds(...) on your components. While this may seem to a newbie the better way to create complex GUI's, it's a fallacy, and more you create Swing GUI's the more you learn to respect and use the layout managers and see that these creatures help immensely in creating flexible, beautiful and if need be, complex GUI's.
Edit 2
Note: if the component has enough significant unique behavior, then I agree with nachokk, create a class to encapsulate it, and then create instances of this class rather than a simple factory method.
Edit 3
The key is that while you can't add a component more than once to a container, you can add new components that use the same model more than once, and that's almost the same thing. Your component's components can even share the same model, for instance your JButtons can share the same Action, your JTextField the same Document if desired.
Edit 4
You state:
A way to increment a button down every time the user hits CREATE. So it adds another button each time, but they move down each instead off spawning on the same position.
Here the layout manager is key. The container holding your JPanels with your buttons will need to use a Layout manager that will accept a new JPanel and place it in the right location. Consider:
The container that accepts the new JPanel can use a GridLayout(0, 1) -- meaning a grid with one column and variable number of rows.
Place this GridLayout using JPanel into the BorderLayout.NORTH position of a BorderLayout using JPanel. This will compress your grid panel to its smallest reasonable size so the JPanels don't expand.
Place the BorderLayout using JPanel into a JScrollPane.
When you add a new JPanel, add it to the GridLayout using JPanel, and then call revalidate() and repaint() on the GridLayout using JPanel so that the new JPanel will be positioned well.
Again, for more details on your problem, consider creating and posting a minimal, compilable, runnable example program.
Edit 5
For example, my MCVE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class GridButtons extends JPanel {
private JPanel gridPanel = new JPanel(new GridLayout(0, 1));
public GridButtons() {
JPanel topPanel = new JPanel();
topPanel.add(new JButton(new AddRowAction("Add Row")));
JPanel middlePanel = new JPanel(new BorderLayout());
middlePanel.add(gridPanel, BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(middlePanel);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setPreferredSize(new Dimension(400, 400));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(scrollpane, BorderLayout.CENTER);
}
private class AddRowAction extends AbstractAction {
public AddRowAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
// my factory method here
JPanel panel = new JPanel();
panel.add(new JButton("Button"));
panel.add(new JTextField(20));
gridPanel.add(panel);
gridPanel.revalidate();
gridPanel.repaint();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GridButtons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GridButtons());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Related

Java Swing Pixels Being Inaccurate

I am designing a Java app with Swing, and I have trouble designing the GUI without a layout.
My purpose is to design a GUI with one JPanel and four JButtons. I've done the math to set buttons and panel on the right place and coded like the following:
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
public MainFrame() {
this.setTitle("Example Frame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
JPanel randomPanel = new JPanel();
randomPanel.setOpaque(true);
randomPanel.setBackground(Color.RED);
randomPanel.setBounds(10, 10, 430, 530);
JButton addButton = new JButton("Add");
addButton.setBounds(10, 550, 100, 40);
addButton.setBackground(Color.GRAY);
JButton deleteButton = new JButton("Delete");
deleteButton.setBounds(120, 550, 100, 40);
deleteButton.setBackground(Color.GRAY);
JButton refreshButton = new JButton("Refresh");
refreshButton.setBounds(230, 550, 100, 40);
refreshButton.setBackground(Color.GRAY);
JButton devButton = new JButton("Developer");
devButton.setBounds(340, 550, 100, 40);
devButton.setBackground(Color.GRAY);
this.add(randomPanel);
this.add(addButton);
this.add(deleteButton);
this.add(refreshButton);
this.add(devButton);
this.setSize(900, 600);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
Following to the code, the components are expected to be placed as following:
However, the actual form was displayed as following:
The components exceed the form, which does not match with the expected look.
What is the problem of this and what should be done for an accurate placement of components?
There are two main problems...
setLayout(null)
setSize
What you've not taken into account is the fact that the amount of space available to the content of the window, is the size of the window MINUS the frame decorations.
Pixel perfect layouts are an illusion in modern UI development and are best avoided.
You could have a look at:
What's wrong with the Null Layout in Java?
Why is it frowned upon to use a null layout in Swing?
Why null layout and absolute positions are bad practice in Java Swing?
for more details.
A better solution is to make use one or more available layout managers. The example below simply makes use of BorderLayout and GridLayout with the help of EmptyBorder to provide some padding
See Laying Out Components Within a Container for more details
Benefits
Adaptable layout:
The example uses pack to "pack" the window around the content, automatically, without you having to adapt your code to the currently running OS (or frame decorations provided by different look and feels)
The user can change the size of the window and the content will resize automatically - bonus to the user.
The layout will adapt to the user's system settings, so if they are using a font larger then you've designed for, it won't completely blow up in your face
Want to add more buttons? No worries, knock yourself out, just add more buttons, the layout will adapt automatically, no need to "pixel push" ever component on the screen
Runnable example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));
add(new SizablePane(430, 530));
JPanel buttonPane = new JPanel(new GridLayout(1, 3, 20, 0));
buttonPane.setBorder(new EmptyBorder(10, 0, 0, 0));
buttonPane.add(new JButton("Add"));
buttonPane.add(new JButton("Delete"));
buttonPane.add(new JButton("Refresh"));
buttonPane.add(new JButton("Developer"));
add(buttonPane, BorderLayout.SOUTH);
}
}
public class SizablePane extends JPanel {
private Dimension size;
public SizablePane(int width, int height) {
size = new Dimension(width, height);
setBackground(Color.RED);
}
#Override
public Dimension getPreferredSize() {
return size;
}
}
}
Need to add more buttons? Easy...
JPanel buttonPane = new JPanel(new GridLayout(1, 0, 20, 0));
buttonPane.setBorder(new EmptyBorder(10, 0, 0, 0));
buttonPane.add(new JButton("Add"));
buttonPane.add(new JButton("Delete"));
buttonPane.add(new JButton("Refresh"));
buttonPane.add(new JButton("Developer"));
buttonPane.add(new JButton("Some"));
buttonPane.add(new JButton("More"));
buttonPane.add(new JButton("Buttons"));
I'm quite late, I don't think this will be helpful to OP anymore... But to anyone else in the same situation.
As others mentioned, when you setSize on a JFrame, that includes the title bar and borders. There's a way to get the size values for those, but... If you want to lay things out manually in your content pane, why not prepare a content pane first, then add it to the JFrame?
class MainPanel extends JPanel {
public MainPanel() {
setLayout(null);
setPreferredSize(new Dimension(900, 600));
// JFrame will have some layouting going on,
// it won't listen to setSize
JPanel randomPanel = new JPanel();
randomPanel.setOpaque(true);
randomPanel.setBackground(Color.RED);
randomPanel.setBounds(10, 10, 430, 530);
JButton addButton = new JButton("Add");
addButton.setBounds(10, 550, 100, 40);
addButton.setBackground(Color.GRAY);
JButton deleteButton = new JButton("Delete");
deleteButton.setBounds(120, 550, 100, 40);
deleteButton.setBackground(Color.GRAY);
JButton refreshButton = new JButton("Refresh");
refreshButton.setBounds(230, 550, 100, 40);
refreshButton.setBackground(Color.GRAY);
JButton devButton = new JButton("Developer");
devButton.setBounds(340, 550, 100, 40);
devButton.setBackground(Color.GRAY);
this.add(randomPanel);
this.add(addButton);
this.add(deleteButton);
this.add(refreshButton);
this.add(devButton);
}
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setTitle("Example Frame");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setContentPane(new MainPanel());
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
}
If you mess with JFrame directly you're sort of bypassing the component system. Whereas this way, you're doing components just fine! Now, you have a JFrame fit to a single child panel, which has some things laid out manually.
This is how I normally do things, in such a situation.
P.S. "Don't lay things out manually, just use layout managers" is not something you can apply everywhere. You may need custom components sometimes, especially for something like a video game, where you have a game screen that you're custom rendering. Inside the game screen, you would be doing manual layout. They can coexist just fine, as long as you know which is which.
You need to override the getInsets() method of the underlying JFrame.
#Override
public Insets getInsets() {
return new Insets(0, 0, 0, 0);
}
Take a look at this question for more information.

How to arrange components using BorderLayout?

I'm trying to get my GUI to appear as such:
Grocery Cart [Refill]
(TextArea)
I am currently using BorderLayout and I would like to stick with it. How can I get the text area underneath the JLabel and the JButton whilst being in the same JPanel? Here is my code for the specific area:
How do I add the text box underneath the two side by side? Whenever I add it, it just goes next to them.
JPanel newPanel = new JPanel();
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
newPanel.add(label);
contentPane.add(newPanel, BorderLayout.WEST) ;
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
newPanel.add(btnNewButton);
If my understanding is correct, here's what you need to do:
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JTextArea area = new JTextArea("Test content");
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(label, BorderLayout.WEST);
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
eastPanel.add(btnNewButton, BorderLayout.WEST);
eastPanel.add(area, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.CENTER);
contentPane.add(mainPanel, BorderLayout.NORTH);
The main idea is that in order to construct complex layouts with simple layout types such as border and flow, you have to use container hierarchy and get creative with a combination of flow and border layouts.
In my example the label and button aren't resizable and always have their widths equal to their preferred widths. The text area, however is resizable and takes up its container's remaining width.
Note, that all components added to mainPanel are resizable vertically. In order to keep mainPanel to its preferred height you place it to the contentPane's BorderLayout.NORTH or SOUTH for that matter.

JPanel not showing when added to JFrame

I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being displayed. My code is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.getContentPane().add(panel);
}
}
Could someone please advise as to why this is?
Thanks very much :)
Edit
From a few tweaks to the code, it appears this is the desired layout (in a non-resizable GUI).
I think you used null to get a "place it wherever fits"? Then use a FlowLayout
frame.setLayout(new FlowLayout());
That should fix it :)
Could someone please advise as to why this is?
Using null layouts and not calling pack().
The image I edited into the question was obtained as a screenshot of the GUI after I had commented out the call to setUndecorated(true) and dragged it a little bigger. Doing so causes the JRE to validate the component structure (what pack() would do) and thereby make the components appear.
As I mentioned in a comment:
..a better question would be "How to layout this GUI?" (so long as you provide an attempt)
And that leads me to my first comment. (Now in longer form)
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
So coming back to:
(so long as you provide an attempt)
Look over those two examples to see how they work, then attempt to combine some layouts and padding to create a frame that can then be packed to reduce to the natural size.
And a tip the the JTextArea. Suggest a size in columns x rows combined with the Font size.
1: You should never call setLayout(null).
2: Try frame.validate() to layout the components with your layout.
Replace
frame.getContentPane().setLayout(null);
with
frame.getContentPane().setLayout(new BorderLayout());
Good luck.
Edit: For future reference, to decide which LayoutManager should be used in your case, you should refer to this Visual Guide to LayoutManagers.
Just remove/comment this line from the above code at line number 46.
// frame.getContentPane().setLayout(null);
It should work fine..
Maybe you shoul replace :
frame.getContentPane().add(panel);
by
frame.setContentPane(panel);
Hope it helped

How to place an object in a specific location (x,y) on a JFrame?

How can I place an object in a specific location (x,y) on a JFrame?
Here find the Absolute Positioning Tutorials. Please do read carefully, as to why this approach is discouraged over using LayoutManagers
To add say a JButton to your JPanel, you can use this :
JButton button = new JButton("Click Me");
button.setBounds(5, 5, 50, 30);
panel.add(button);
Here try this example program :
import java.awt.*;
import javax.swing.*;
public class AbsoluteLayoutExample
{
private void displayGUI()
{
JFrame frame = new JFrame("Absolute Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
JLabel label = new JLabel(
"This JPanel uses Absolute Positioning"
, JLabel.CENTER);
label.setSize(300, 30);
label.setLocation(5, 5);
JButton button = new JButton("USELESS");
button.setSize(100, 30);
button.setLocation(95, 45);
contentPane.add(label);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.setSize(310, 125);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AbsoluteLayoutExample().displayGUI();
}
});
}
}
Try these 2... in combination with each other...
setLocation() and setBounds()
Its even better to use GroupLayout, developed by NetBeans team in 2005. WindowsBuilder Pro is a good tool for Building Gui in java
Check out this absolute layout code sample:
Absolute Layout demo
In the class inheriting the frame:
setLayout(null);
In your component:
setLocation(x,y);

My whole code is complete but I am not able to add a second button

Please take a look at my code it is working fine the way I want but the only issue is that I want to add another button opposite my current button and I am not able to do so can any body please help me.
import java.awt.event.*;
import javax.swing.*;
public class Example2 extends JFrame {
public Example2() {
initUI();
}
public final void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
panel.setToolTipText("A Panel container");
JButton button = new JButton("Even");
button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");
JButton button2 = new JButton("Odd");
button2.setBounds(100, 60, 100, 30);
button2.setToolTipText("A button component");
//Add action listener to button
button.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System .out.println("You clicked the button");
int sum=0;
for(int i=1;i<=100;i++){
if(i%2==0){
System.out.println(i);
sum+=i;
}
}
System.out.println("Sum of even numbers: "+sum);
}
});
panel.add(button);
panel.add(button2);
setTitle("Tooltip");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Example2 ex = new Example2();
ex.setVisible(true);
}
}
panel.setLayout(null);
That is where it starts to go wrong.
Use layouts. See Laying Out Components Within a Container & Effective Layout Management: Short Course for more details.
Use:
The appropriate layouts.
Possibly nested inside one another.
With appropriate layout padding and component border/insets for white space.
As an aside.
...
button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");
JButton button2 = new JButton("Odd");
button2.setBounds(100, 60, 100, 30);
...
Did you notice how the bounds of the two buttons were identical? What do you think happens when you put two components of the same size in the same place?
You have to change panel.setLayout(null) to layout you need. For example:
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
or
panel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));
Andrew Thompson +1 ,
Here are some usefull links :
A Visual Guide to Layout Managers
Using Layout Managers
Adding space between components

Categories

Resources