1

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

关闭好友和消息时的布局: 在此处输入图像描述

打开好友和消息时的布局: 在此处输入图像描述

我打算用 Java Swing 来做这个布局。

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

然后朋友和消息按钮应该是切换按钮,并且在切换时它们应该在主面板(或那里的任何东西)顶部显示一个覆盖,包含一个朋友列表和一个消息区域。我意识到我需要为此使用 LayeredPane。

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

但是我真的很迷茫如何结合它,所以我请求你的帮助。

希望我已经对这种情况进行了足够的解释。

问候。

4

2 回答 2

2

这可能与覆盖有关,因为 JPanel 可以包含其他 JComponent

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

  • 使用 GlassPane 和JComponents 相对于....

  • 最简单的方法是使用 JDialog(undecorated) 放置到 Point (setLocation(int, int)),setVisible() 必须包装到 invokeLater

于 2013-04-30T10:06:44.490 回答
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();
            }
        });
    }
}
于 2013-04-30T13:51:36.670 回答