Java Swing-使用LayeredPane实现布局

Java Swing-使用LayeredPane实现布局,java,swing,Java,Swing,我有一个关于制作特定布局的问题,首先我会展示一些例子,然后我会添加一些额外的说明 关闭好友和邮件时的布局: 打开好友和邮件时的布局: 我打算用JavaSwing制作这个布局 我的意图是首先将框架划分为三个区域:顶部菜单行、主面板和底部菜单行。 我正在考虑在这个部分使用边框布局 然后,好友和消息按钮应该是切换按钮,在切换时,它们应该在主面板(或其他任何地方)的顶部显示一个覆盖,其中包含好友列表和消息区域。我意识到我需要以某种方式使用分层窗格 另一个重要的部分是,布局应该是任何大小都可以查看的,

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

关闭好友和邮件时的布局:

打开好友和邮件时的布局:

我打算用JavaSwing制作这个布局

我的意图是首先将框架划分为三个区域:顶部菜单行、主面板和底部菜单行。 我正在考虑在这个部分使用边框布局

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

另一个重要的部分是,布局应该是任何大小都可以查看的,也就是说,用户可以调整应用程序的大小,并且可以在不同的分辨率上使用,所以我并不真正想要任何具有固定宽度和高度的内容

但我真的不知道如何结合这一点,所以我请求您的帮助

希望我已经解释了足够的情况


注意。

这可能是关于覆盖的,因为JPanel可以包含其他JC组件

  • 在JXLayer(Java6)的基础上使用JLayer(Java7)

  • 使用玻璃材质窗格玻璃

  • 最简单的方法是使用分层到点(setLocation(int,int))的JDialog(未修饰),setVisible()必须包装到invokeLater中


我将使用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();
            }
        });
    }
}

主面板中的哪些内容如此无关,以至于您可以用两个“浮动”面板来掩盖它?听起来像是“又一个无法使用的图形用户界面”正在制作中。它是为2D回合游戏设计的。如果要查看邮件,请单击“邮件”按钮(或者最好使用快捷方式),当不想再查看邮件时,请再次将其隐藏。所有你的选择,信息是绝对不需要看到时,玩游戏虽然。但它也可以用于调试反馈,例如,我将在稍后对此进行研究。但是您是否考虑到我希望其他面板(朋友列表和消息区域)与框架而不是主面板耦合?这样,如果有必要,我仍然可以更换材料,当然,如果这是唯一的方法,那么我将不得不进行调整。@skiwi主要布局可以通过使用BorderLayout实现,利用框架的玻璃窗格或层平面,您可以覆盖其他组件。查看更多详细信息,您可以选择将任何JComponent放在JPanel中,更改(运行时)维度,屏幕坐标不要担心,在使用LayoutManager之前,使用GlassPane直到不需要透明度,否则请查看JLayer,未修饰的JDialog(尽可能简单)啊,我明白了。我认为在GlassPane上使用朋友列表和消息区域的BorderLayout是一个很好的建议。或者最好把它们放在主面板上,我想我得到了一些应该有用的东西。非常感谢:)