Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

swing layout question

Posted on 2003-12-06
4
Medium Priority
?
323 Views
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   |
|___________________|
0
Comment
Question by:Akindo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:Akindo
ID: 9889744
btw, I forgot to add I also want a menu bar....
0
 
LVL 7

Expert Comment

by:gnurl
ID: 9889753
Hi,

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

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20748381.html

Thats what I could help.

CU
0
 
LVL 15

Accepted Solution

by:
jimmack earned 2000 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)
            {
                System.exit(0);
            }
        });
       
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(fileNew);
        fileMenu.addSeparator();
        fileMenu.add(fileExit);
       
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
       
        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());
        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);
       
        JPanel sliderAndButtons = new JPanel(new BorderLayout());
        sliderAndButtons.add(slider, BorderLayout.NORTH);
        sliderAndButtons.add(buttonPanel, BorderLayout.SOUTH);
       
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        topPanel.add(sliderAndButtons);
       
        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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(mainPanel);
        mainFrame.setJMenuBar(createMenuBar());
        mainFrame.setSize(new Dimension(640, 480));
        mainFrame.show();
    }
   
    public static void main(String[] args)
    {
        new Slider().init();
    }
}
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9890292
;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

618 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