[英]How to align all jPanels within a jPanel to the left?
Hey I'm attempting to align all panels within a panel to the left of the larger panel. 嘿,我试图将面板内的所有面板对齐到较大面板的左侧。
here is a picture of what I'm currently faced with: 这是我目前面临的情况:
For the master panel (the one that contains all other panels - I shall call it the master!) I'm using the following code when creating it: 对于主面板(包含所有其他面板的面板 - 我将其称为主面板!)我在创建它时使用以下代码:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
For each of the panels within it I'm also using the BoxLayout, but I've tried things like [jpanel].setAlignmentX(Component.LEFT_ALIGNMENT) on each panel but that doesn't seem to work. 对于其中的每个面板我也使用BoxLayout,但我在每个面板上尝试了[jpanel] .setAlignmentX(Component.LEFT_ALIGNMENT)之类的东西,但这似乎不起作用。
Any help would be appreciated! 任何帮助,将不胜感激!
:) :)
edit: for the "Dimensions of box(m):" label, is there any way of aligning that to the top of the panel it's contained in? 编辑:对于“盒子尺寸(m):”标签,有没有办法将它对齐到它所包含的面板顶部? It's in a panel of its own.
它在自己的一个小组中。
edit: Once fixed: 编辑:修复后:
Solution below, thanks all for your help :) 以下解决方案,谢谢大家的帮助:)
You have a number of possibilities, depending on you over all needs. 您有多种可能性,具体取决于您的所有需求。
All I've done here is used compound panels and using GridBagLayout
to adjust the layouts 我在这里所做的只是使用复合面板并使用
GridBagLayout
来调整布局
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);
}
}
}
You can use MigLayout : 您可以使用MigLayout :
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class PanelAlignment extends JFrame {
JPanel contentPane = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
JPanel thirdPanel = new JPanel();
JLabel dimOfBox, rein, seaTop;
public PanelAlignment() {
contentPane.setLayout(new MigLayout());
firstPanel.add(dimOfBox = new JLabel("Dimensions of box (m): "));
firstPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
contentPane.add(firstPanel,"wrap");
secondPanel.add(rein = new JLabel("Reinforcement: "));
secondPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
contentPane.add(secondPanel,"wrap");
thirdPanel.add(dimOfBox = new JLabel("Sealable top: "));
thirdPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
contentPane.add(thirdPanel,"wrap");
add(contentPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
PanelAlignment pa = new PanelAlignment();
pa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pa.pack();
pa.setVisible(true);
}
});
}
}
And you will get something like this: 你会得到这样的东西:
I recently answered on similar question here 我最近在这里回答了类似的问题
Good luck! 祝好运! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.