如何创建具有多种布局样式的布局

时间:2017-10-25 19:00:35

标签: java swing layout-manager

我正在尝试创建一个游戏启动器,为此我会有几个不同类型的布局样式的JPanel,此时我只是不知道要使用什么,因为有很多布局样式他们都有各自的利弊。 这就是我现在所拥有的: enter image description here

我就是这样做的: 这是主框架,添加了几个面板。

public Frame() {
    main = new Background();
    menuPane = new MenuPane();

    main.setLayout(new BorderLayout());

    add(main);

    main.add(menuPane, BorderLayout.NORTH);

    setTitle("DelusionX Launcher");
    setSize(700, 500);
    setUndecorated(true);
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

这是背景面板

public Background() {
    try {
        backgroundImg = ImageIO.read(new File("./Images/Background.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(backgroundImg, (getWidth() - backgroundImg.getWidth(null)) / 2, 0, null);
}

这是菜单面板

public MenuPane() {
    this.setBackground(new Color(0, 0, 0, 0.5f));
    this.setLayout(new BorderLayout());
    minimizeBtn = new Button();
    minimizeBtn.addActionListener(this);
    minimizeBtn.setIcon(minimizeImg);
    this.add(minimizeBtn, BorderLayout.EAST);
    closeBtn = new Button();
    closeBtn.addActionListener(this);
    closeBtn.setIcon(closeImg);
    this.add(closeBtn, BorderLayout.EAST);
}

这是按钮对象

public Button() {
    setBorderPainted(false);
    setBorder(null);
    setMargin(new Insets(0, 0, 0, 0));
    setContentAreaFilled(false);
}

正如你所看到的,我也添加了一个最小化按钮,但由于某种原因,它是在关闭按钮后面,我想。 这就是我想要实现的目标: enter image description here 这就是我认为面板应该如何,但我不确定这是否是最好/最简单的方法。

enter image description here 简而言之:我如何创建一个看起来像第二张图像的JFrame?

1 个答案:

答案 0 :(得分:2)

GridBagLayout是默认API中最灵活的布局管理器之一,如果您愿意尝试,它可以帮助您摆脱一些惊人的事情

有关详细信息,请参阅How to Use GridBagLayout

GridBagLayout

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(100, 0, 0, 0);
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(new ProxyPane(400, 300, Color.RED), gbc);

            gbc.insets = new Insets(101, 1, 0, 1);
            gbc.weighty = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.anchor = GridBagConstraints.NORTH;
            add(new ProxyPane(400, 50, Color.YELLOW), gbc);

            gbc.insets = new Insets(0, 0, 0, 0);
            gbc.weightx = 0;
            gbc.weighty = 0;
            gbc.fill = GridBagConstraints.NONE;
            add(new ProxyPane(300, 200, Color.GREEN), gbc);
        }

    }

    public class ProxyPane extends JPanel {

        private Dimension size;

        public ProxyPane(int width, int height, Color borderColor) {
            size = new Dimension(width, height);
            setBorder(new LineBorder(borderColor));
            setOpaque(false);
        }

        @Override
        public Dimension getPreferredSize() {
            return size;
        }

    }

}