2

ねえ、パネル内のすべてのパネルを大きなパネルの左側に揃えようとしています。

ここに私が現在直面しているものの写真があります:

ボックス パネル レイアウト

マスター パネル (他のすべてのパネルを含むパネル - 私はそれをマスターと呼びます!) の場合、作成時に次のコードを使用しています。

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

その中のパネルごとに BoxLayout も使用していますが、各パネルで [jpanel].setAlignmentX(Component.LEFT_ALIGNMENT) などを試しましたが、うまくいかないようです。

どんな助けでも大歓迎です!

:)

編集:「ボックスの寸法(m):」ラベルについて、それが含まれているパネルの上部に揃える方法はありますか? それはそれ自身のパネルにあります。

編集:修正後:

JPanelの問題が修正されると

以下の解決策、あなたの助けに感謝します:)

4

2 に答える 2

5

すべてのニーズに応じて、多くの可能性があります。

ここで行ったのは、複合パネルを使用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);
        }

    }

}
于 2012-11-19T00:16:51.487 に答える
3

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

}

そして、あなたは次のようなものを得るでしょう:

ここに画像の説明を入力

私は最近、ここで同様の質問に答えました

幸運を!:)

于 2012-11-18T23:38:44.407 に答える