0

パネルに GridBagLayout を使用しています。ここに下の図のようにコンポーネントを追加したいのですが、Intern というラベルの下に Medication perscription というラベルを付けることができません。 誰でも方法を知っていますか?

GridBagLayout gridbaglayout = new GridBagLayout();
        TGridBagConstraints constraints = new TGridBagConstraints();
        northPanel = new WebPanel(gridbaglayout);
        northPanel.setBorder(new EmptyBorder(5,5,5,5));


// *** PackComboBox row
        constraints.setGxGyGwGhWxWyFillAnchor(0, 0, 1, 1, 0.0, 0.0, TGridBagConstraints.HORIZONTAL, TGridBagConstraints.NORTHWEST);
        gridbaglayout.setConstraints(PackComboBox, constraints);
        northPanel.add(FerPackComboBox);

        constraints.setGxGyGwGhWxWyFillAnchor(1, 0, 1, 1, 0.0, 0.0, TGridBagConstraints.HORIZONTAL, TGridBagConstraints.NORTHWEST);
        gridbaglayout.setConstraints(approveButton, constraints);
        northPanel.add(approveButton);

        constraints.setGxGyGwGhWxWyFillAnchor(2, 0, 1, 1, 0.0, 0.0, TGridBagConstraints.HORIZONTAL, TGridBagConstraints.NORTHWEST);
        WebLabel internLabel = PrescriptionSummaryUtil.createCustomLabel("Intern",new Font("SansSerif",Font.BOLD,20),Color.decode("#ABB202").darker() );
        gridbaglayout.setConstraints(internLabel, constraints);
        northPanel.add(internLabel);

        constraints.setGxGyGwGhWxWyFillAnchor(5, 0, 1, 1, 0.0, 0.0, TGridBagConstraints.HORIZONTAL, TGridBagConstraints.NORTHWEST);
        gridbaglayout.setConstraints(patientInfoBox, constraints);
        northPanel.add(patientInfoBox);

        // *** Medication PerscriptionLabel row
        constraints.setGxGyGwGhWxWyFillAnchor(1, 1, 2, 0, 0.0, 0.0, TGridBagConstraints.NORTHEAST, TGridBagConstraints.NORTHEAST);
        WebLabel medicationPerscriptionLabel = PrescriptionSummaryUtil.createCustomLabel("Medication perscription",new Font("SansSerif",Font.BOLD,20),Color.decode("#ABB202").darker() );
        gridbaglayout.setConstraints(medicationPerscriptionLabel, constraints);
        northPanel.add(medicationPerscriptionLabel);

ここに画像の説明を入力

4

1 に答える 1

1

制約を生成するために使用している API に詳しくないため、要件を満たすコードを作成するのは簡単ではありません...

ただし、次のコードは生成されます...

ここに画像の説明を入力

public class BadLayout {

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

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

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

    protected class LayoutPane extends JPanel {

        public LayoutPane() {
            setLayout(new GridBagLayout());

            JLabel intern = new JLabel("Intern");
            JLabel med = new JLabel("Medication perscription");

            JPanel box = new JPanel(new GridBagLayout());
            box.setBorder(new LineBorder(Color.BLACK));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(40, 40, 40, 40);
            box.add(new JLabel("I'm a box"), gbc);

            gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            add(intern, gbc);

            gbc.gridy++;
            gbc.weighty = 1;
            add(med, gbc);

            gbc.weighty = 0;
            gbc.weightx = 0;
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(box, gbc);                
        }                        
    }        
}
于 2012-10-30T11:10:24.370 に答える