swing layout question

Posted on 2003-12-06
Last Modified: 2010-03-31
I am a little confused about defining the layout using swing.heres what I want to do....

I have a background image say background.gif
Then I have 5 images, say c1.gif-c5.gif
I also have three buttons and a slider and I want the page to be like this(approximately) with the background image in the back...

|                      slider   |      So basically the slider is near the top right with the 3 buttons
|                    b1 b2 b3|     lined up under it. Also could you please give me the code that
|                                 |   would let me display the images and also how to remove them...
|  c3 c4 c5                   |
|                                 |
|                    c1   c2   |
Question by:Akindo
  • 2

Author Comment

ID: 9889744
btw, I forgot to add I also want a menu bar....

Expert Comment

ID: 9889753

I think you should have a look at the class JDesktopPane and play around with the LayoutManagers , and LayeredPanes.

Maybe this thread could help you

Thats what I could help.

LVL 15

Accepted Solution

jimmack earned 500 total points
ID: 9890232
Here's most of it for you.  You'll need to do some work to get the images laid out the way you want, but this should be enough to get you started.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Slider
    private JFrame mainFrame;
    private JMenuBar createMenuBar()
        JMenuItem fileNew = new JMenuItem("New");
        JMenuItem fileExit = new JMenuItem("Exit");
        fileExit.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent e)
        JMenu fileMenu = new JMenu("File");
        JMenuBar menuBar = new JMenuBar();
        return (menuBar);
    private void init()
        JSlider slider = new JSlider(0, 100);
        JButton button1 = new JButton("Button1");
        JButton button2 = new JButton("Button2");
        JButton button3 = new JButton("Button3");
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JPanel sliderAndButtons = new JPanel(new BorderLayout());
        sliderAndButtons.add(slider, BorderLayout.NORTH);
        sliderAndButtons.add(buttonPanel, BorderLayout.SOUTH);
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JPanel centerImagePanel = new JPanel(new GridLayout(1, 3));
        centerImagePanel.add(new JLabel("c3"));
        centerImagePanel.add(new JLabel("c4"));
        centerImagePanel.add(new JLabel("c5"));
        JPanel bottomImagePanel = new JPanel(new GridLayout(1, 2));
        bottomImagePanel.add(new JLabel("c1"));
        bottomImagePanel.add(new JLabel("c2"));
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(centerImagePanel, BorderLayout.CENTER);
        mainPanel.add(bottomImagePanel, BorderLayout.SOUTH);
        mainFrame = new JFrame("Slider Frame");
        mainFrame.setSize(new Dimension(640, 480));;
    public static void main(String[] args)
        new Slider().init();
LVL 15

Expert Comment

ID: 9890292

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 63
egit plugin on eclipse 8 64
HashTable highest marks enumeration alternative 9 39
Which non-HTML GUI front end to use with Java? 3 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question