gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:32:07 26 4
gpt4 key购买 nike

我正在尝试创建一个游戏启动器,为此我将有几个具有不同类型布局样式的 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?

最佳答案

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

}

}

关于java - 如何创建具有多种布局样式的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com