gpt4 book ai didi

java - 如何将 jPanel 中的所有 jPanel 向左对齐?

转载 作者:行者123 更新时间:2023-12-02 14:00:56 25 4
gpt4 key购买 nike

嘿,我正在尝试将一个面板中的所有面板与较大面板的左侧对齐。

这是我目前面临的情况的图片:

Box panel layout

对于主面板(包含所有其他面板的面板 - 我将称其为主面板!)我在创建它时使用以下代码:

    JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

对于其中的每个面板,我也使用 BoxLayout,但我在每个面板上尝试了 [jpanel].setAlignmentX(Component.LEFT_ALIGNMENT) 之类的方法,但这似乎不起作用。

如有任何帮助,我们将不胜感激!

:)

编辑:对于“盒子尺寸(米):”标签,是否有任何方法可以将其与其所包含的面板顶部对齐?它位于自己的面板中。

编辑:修复后:

JPanel problem once fixed

解决方案如下,感谢大家的帮助:)

最佳答案

您有多种可能性,具体取决于您的所有需求。

我在这里所做的只是使用复合面板并使用 GridBagLayout 来调整布局

enter image description here

public class BadLayout03 {

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

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

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MasterPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MasterPane extends JPanel {

public MasterPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.WEST;
add(new DimensionsPane(), gbc);
gbc.gridy++;
add(new ColorPane(), gbc);
gbc.gridy++;
add(new ReinforementPane(), gbc);
gbc.gridy++;
add(new SealableTopPane(), gbc);
gbc.gridy++;
add(new CardGradePane(), gbc);
gbc.gridy++;
add(new QuantityPane(), gbc);
gbc.gridy++;
add(new OrderPricePane(), gbc);
}

}

public class DimensionsPane extends JPanel {

public DimensionsPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Dimensions of box (m):"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("Length: 33.0"), gbc);
gbc.gridy++;
add(new JLabel("Width: 3.0"), gbc);
gbc.gridy++;
add(new JLabel("Height: 3.0"), gbc);
}

}

public class ColorPane extends JPanel {

public ColorPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Colour :"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("0"), gbc);
}

}

public class ReinforementPane extends JPanel {

public ReinforementPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Reinforcement :"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("None"), gbc);
}

}

public class SealableTopPane extends JPanel {

public SealableTopPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Selable top :"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("No"), gbc);
}

}

public class CardGradePane extends JPanel {

public CardGradePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Grade of card:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("1"), gbc);
}

}

public class QuantityPane extends JPanel {

public QuantityPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Quantity:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("1"), gbc);
}

}

public class OrderPricePane extends JPanel {

public OrderPricePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Order price:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("$558.9"), gbc);
}

}

}

关于java - 如何将 jPanel 中的所有 jPanel 向左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13445621/

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