2

I have a question regarding making a specific Layout, first I'll show examples then I will add some extra clarification.

Layout when Friends and Messages are closed: enter image description here

Layout when Friends and Messages are opened: enter image description here

I intend to make this layout with Java Swing.

My intention is to firstly have the Frame divided in three areas, the top menu row, the main panel and the bottom menu row. I was thinking of using a BorderLayout for this part.

Then the Friends and Messages buttons should be toggle button's, and on toggle they should show an overlay on top of the mainpanel (or whatever is there), containing a friend list and a message area. I realised I need to use a LayeredPane somehow for this.

Another important part is that the Layout should be viewable in any size, that is the user may resize the application and it will be used on a various amount of resolutions, so I don't really want anything with a fixed width and height.

But I am really lost as how to combine this, so therefore I ask your help.

Hopefully I have explained enough about the situation.

Regards.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • What is in the main panel that is so irrelevant that you can afford to obscure it with the two 'floating' panels? Sounds like 'yet another unusable GUI' in the making. – Andrew Thompson Apr 30 '13 at 10:08
  • It is intended for a 2D turn based game. If you want to see your messages, then you click the Messages button (or use a shortcut preferably), and when you do not want to see them anymore, then you hide it again. All your choice, the messages are definately not needed to be seen when playing the game though. But it can also be used for debugging feedback for example. – skiwi Apr 30 '13 at 10:20

2 Answers2

2

this could be about overlay, because JPanel can contains other JComponents

  • use JLayer (Java7) based on JXLayer(Java6),

  • use GlassPane with JComponents layed to rellative to....

  • easiest could be to use JDialog(undecorated) layed to Point (setLocation(int, int)), setVisible() must be wrapped into invokeLater

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I'll look into this in a second. But did you take into account that I want the other panels (Friend List & Message Area) coupled with the frame and not with the Main Panel? Such that I can still replace stuff if neccessary, of course if this is the only way then I will have to adapt. – skiwi Apr 30 '13 at 10:10
  • @skiwi The main layout can be achieved through the use of BorderLayout, utilising the GlassPane or LayerPane of the frame, you can overlay the other components. Take a look at [JRootPane](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) for more details – MadProgrammer Apr 30 '13 at 10:14
  • to every of choices you can to put any JComponent layed in JPanel, change (on runtime) Dimension, screen coordinates don't worry doesn't matter, untill LayoutManager is used, use GlassPane until transparency is not necessary, otherwise look at JLayer, undecorated JDialog (easiest as is possible) – mKorbel Apr 30 '13 at 10:17
  • Ah I see. I think using a BorderLayout with the Friend List & Message Area on the GlassPane is a nice suggestion. Or maybe even better lay them on the Main Panel, I think I got something that should work. thanks a lot :) – skiwi Apr 30 '13 at 10:22
1

I will use gridBagLayout.

Here is small example including button which hide your yellow panels:

package Core;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBagLayoutDemo {

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new GridBagLayout());

        add1row(pane);
        addmainRow(pane);
        addLastRow(pane);
    }

    private static void addLastRow(Container pane) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        c.anchor = GridBagConstraints.PAGE_END;
        JPanel bottonPanel = new JPanel();
        bottonPanel.setBackground(Color.BLUE);
        bottonPanel.setLayout(new GridBagLayout());
        pane.add(bottonPanel, c);

        JPanel messagePanel = new JPanel();
        messagePanel.setBackground(Color.GRAY);
        messagePanel.add(new JLabel("MESSAGES"));
        c.fill = GridBagConstraints.VERTICAL;
        c.anchor = GridBagConstraints.LINE_END;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        bottonPanel.add(messagePanel, c);
    }

    private static void addmainRow(Container pane) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = GridBagConstraints.CENTER;
        JPanel mainManel = new JPanel();
        mainManel.setBackground(Color.GREEN);
        mainManel.setLayout(new GridBagLayout());
        pane.add(mainManel, c);

        final JPanel friendListPanel = new JPanel();
        friendListPanel.setBackground(Color.YELLOW);
        friendListPanel.add(new JLabel("FRIEND LIST"));
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = GridBagConstraints.FIRST_LINE_END;
        mainManel.add(friendListPanel, c);

        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 0;
        c.weighty = 0;
        c.anchor = GridBagConstraints.CENTER;
        JButton button = new JButton("On/Off");
        mainManel.add(button, c);

        final JPanel messageAreaPanel = new JPanel();
        messageAreaPanel.setBackground(Color.YELLOW);
        messageAreaPanel.add(new JLabel("MESSAGE PANEL"));
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        mainManel.add(messageAreaPanel, c);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                friendListPanel.setVisible(!friendListPanel.isVisible());
                messageAreaPanel.setVisible(!messageAreaPanel.isVisible());
            }
        });

    }

    private static void add1row(Container pane) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.PAGE_START;
        Panel headerPanel = new Panel();
        headerPanel.setLayout(new GridBagLayout());
        headerPanel.setBackground(Color.BLUE);
        pane.add(headerPanel, c);

        JPanel quitPanel = new JPanel();
        quitPanel.setBackground(Color.GRAY);
        quitPanel.add(new JLabel("QUIT"));
        c.fill = GridBagConstraints.VERTICAL;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        headerPanel.add(quitPanel, c);

        JPanel friendsPanel = new JPanel();
        friendsPanel.setBackground(Color.GRAY);
        friendsPanel.add(new JLabel("FRIENDS"));
        c.fill = GridBagConstraints.VERTICAL;
        c.anchor = GridBagConstraints.LINE_END;
        c.gridx = 1;
        c.gridy = 0;
        headerPanel.add(friendsPanel, c);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addComponentsToPane(frame);
        // uncoment to use full screen
        // frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
hudi
  • 15,555
  • 47
  • 142
  • 246