Java Swing - 使用LayeredPane实现布局

时间:2013-04-30 10:03:09

标签: java swing

我有关于制作特定布局的问题,首先我会展示示例,然后我会添加一些额外的说明。

朋友和消息关闭时的布局: enter image description here

打开好友和消息时的布局: enter image description here

我打算用Java Swing进行这种布局。

我的目的是首先将框架划分为三个区域,即顶部菜单行,主面板和底部菜单行。 我在想这个部分使用BorderLayout。

然后,朋友和消息按钮应该是切换按钮,并且在切换时,他们应该在主面板(或其他任何东西)的顶部显示一个覆盖图,其中包含一个朋友列表和一个消息区域。我意识到我需要以某种方式使用LayeredPane。

另一个重要的部分是布局应该可以以任何大小查看,也就是说用户可以调整应用程序的大小,并且它将用于各种分辨率,所以我真的不想要任何具有固定宽度和高度。

但我真的迷失了如何将它结合起来,因此我请求你的帮助。

希望我对这种情况有足够的解释。

问候。

2 个答案:

答案 0 :(得分:2)

这可能是关于叠加的,因为JPanel可以包含其他JComponents

  • 使用基于JXLayer(Java6)的JLayer(Java7),

  • 将GlassPane与JComponents layed to rellative to....

  • 一起使用
  • 最简单的可能是使用JDialog(未修饰)铺设到Point(setLocation(int,int)),setVisible()必须包装到invokeLater

答案 1 :(得分:1)

我将使用gridBagLayout。

这是一个小例子,包括隐藏黄色面板的按钮:

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();
            }
        });
    }
}