gpt4 book ai didi

java - Java Swing 中的 GridBagLayout

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

我遇到以下问题。我有一个带有 GridBagLayout() 设置的 JPanel 。我使用相同的布局管理器向其中添加另一个 JPanel。我里面有几个不同的物体。问题是我无法将它们设置为使标有 2 号的对象位于 1 号对象的前面。也就是说,2 号对象应该是从左侧第一个。

代码下方:

public MainPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
SecondPanel secondPanel = new SecondPanel();
gbc.insets = new Insets(10, 20, 10, 20);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.LINE_END;
gbc.weightx = 0.1;
add(secondPanel , gbc);
}

class SecondPanel extends JPanel {
public SecondPanel () {
setLayout(new GridBagLayout());
JPanel main = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.LINE_START;
main.add(ope, gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(opeV, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
main.add(am, gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(amV, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
main.add(cry, gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(cryV, gbc);

gbc.anchor = GridBagConstraints.BOTH;
main.add(addButton);

BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.drawLine(0, 0, 320, 240);
g2d.drawLine(320, 0, 0, 240);
g2d.dispose();

mainL.setIcon(new ImageIcon(img));
mainL.setBorder(new LineBorder(Color.RED));

gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
add(mainL, gbc);

add(main);

setVisible(true);
}
}

屏幕截图:

Screenshot of mainPanel

编辑:我解决了问题,因为此代码中的第一个错误是 add(mainL, gbc); 应该是 main.add(mainL,gbc);。其次,我分别从 SecondPanelMainPanel 制作两个不同的面板。

最佳答案

停下来,去读一下 How to Use GridBagLayout并确保保留JavaDocs for GridBagLayoutGridBagConstraints手头上有。

GridBagLayout 令人惊讶的是,它是一个基于“网格”的布局,具有灵 active 。

如果您想将一个组件放置在另一个组件之上,那么您必须告诉布局这两个组件共享同一个单元格。这就是 gridxgridy 很重要的地方

所以当你做类似的事情时......

add(mainL, gbc);
add(main);

您允许布局管理器决定如何最好地布局main,这不是您想要的,相反,您需要为两个组件提供相同的 gridx/gridy 约束,例如...

Overlapped

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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 SecondPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

class SecondPanel extends JPanel {

public SecondPanel() {
setLayout(new GridBagLayout());
JPanel main = new JPanel(new GridBagLayout());
main.setBorder(new LineBorder(Color.GRAY));

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.LINE_START;
main.add(new JLabel("Ope"), gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(new JLabel("opeV"), gbc);
gbc.anchor = GridBagConstraints.LINE_START;
main.add(new JLabel("am"), gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(new JLabel("amV"), gbc);
gbc.anchor = GridBagConstraints.LINE_START;
main.add(new JLabel("cry"), gbc);
gbc.anchor = GridBagConstraints.LINE_END;
main.add(new JLabel("cryV"), gbc);

gbc.anchor = GridBagConstraints.CENTER;
main.add(new JButton("Add"), gbc);

BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.drawLine(0, 0, 320, 240);
g2d.drawLine(320, 0, 0, 240);
g2d.dispose();

JLabel mainL = new JLabel();
mainL.setIcon(new ImageIcon(img));
mainL.setBorder(new LineBorder(Color.RED));

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(main, gbc);
add(mainL, gbc);
}
}
}

我保留了 main opaque 并添加了边框,以便您可以看到它。另请注意,组件按 FILO(反向)顺序显示

GridBagLayout 是一种艺术形式,你只需仔细研究即可掌握

But this dont resolve my problem

你的意思是......

something like

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(main, gbc);
gbc.gridx++;
add(mainL, gbc);

关于java - Java Swing 中的 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51019274/

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