3

我将一个 2 jpanel (CENTER AND PAGE_END) 添加到另一个 Jpanel 中,该 Jpanel 位于 JFrame 中。我想消除的两个面板(panneauDateDebut 和 panneauDateFin)之间存在巨大差距。我尝试将它们设置为不同的配置(开始/中心、开始/结束、中心/结束),但没有运气。如何才能做到这一点 ?

编辑以获得工作代码

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CreerModificationAbsence extends JDialog {

    private JPanel modificationAbsence1, modificationAbsence2,
            modificationAbsence3, panneauDateDebut, panneauDateFin;
    private JButton modifier, annuler;
    private JLabel raison, prenomNomEmpl, prenomNomChef;
    private JComboBox<String> raisonC, heureDebutC, heureFinC, minuteDebutC,
            minuteFinC;
    private JTextField prenomNomEmplT, prenomnomChefT;
    private final String[] raisonAbsence = { "Malade" };

    private JLabel dateDebut, dateFin;
    private JTextField dateDebutT, dateFinT;

    private final String[] heures = { "00" };
    private final String[] minutes = { "00", "15", "30", "45" };

    private BorderLayout gestionnaireComposant;
    private GridLayout gridGestionnaireComposant;
    private FlowLayout panneauMilieuLayout;
    final FlowLayout gestionnaireComposantBas;

    public CreerModificationAbsence() {
        super((Frame) null, "Modification - Absence d'employé", true);

        setPreferredSize(new Dimension(600, 250));
        setAlwaysOnTop(true);
        setResizable(false);
        setLocation(400, 200);
        setAlwaysOnTop(true);
        gestionnaireComposant = new BorderLayout();
        this.getContentPane().setLayout(gestionnaireComposant);

        // Modification Panneau Haut

        modificationAbsence1 = new JPanel();

        gridGestionnaireComposant = new GridLayout(3, 2, 2, 2);
        modificationAbsence1.setLayout(gridGestionnaireComposant);

        raison = new JLabel("Raison : ");
        raisonC = new JComboBox<>(raisonAbsence);
        raisonC.setEditable(true);
        prenomNomEmpl = new JLabel("Prénom et Nom de l'employé : ");
        prenomNomEmplT = new JTextField();
        prenomNomChef = new JLabel("Prénom et Nom du chef d'équipe : ");
        prenomnomChefT = new JTextField();

        modificationAbsence1.add(raison);
        modificationAbsence1.add(raisonC);
        modificationAbsence1.add(prenomNomEmpl);
        modificationAbsence1.add(prenomNomEmplT);
        modificationAbsence1.add(prenomNomChef);
        modificationAbsence1.add(prenomnomChefT);

        // Modification Panneau Milieu

        modificationAbsence2 = new JPanel();
        panneauDateDebut = new JPanel();
        panneauDateFin = new JPanel();

        panneauMilieuLayout = new FlowLayout();

        panneauDateDebut.setLayout(panneauMilieuLayout);
        panneauDateDebut.setPreferredSize(new Dimension(600, 0));
                   panneauDateDebut.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        panneauDateFin.setLayout(panneauMilieuLayout);
        panneauDateFin.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        panneauDateFin.setPreferredSize(new Dimension(600, 113));

        modificationAbsence2.setLayout(new BorderLayout(0,0));

        dateDebutT = new JTextField(12);
        heureDebutC = new JComboBox<>(heures);
        minuteDebutC = new JComboBox<>(minutes);
        dateFinT = new JTextField(12);
        heureFinC = new JComboBox<>(heures);
        minuteFinC = new JComboBox<>(minutes);

        dateDebut = new JLabel("Date de début :");
        dateFin = new JLabel("Date de fin :");

        dateDebutT.setPreferredSize(new Dimension(125, 20));
        dateFinT.setPreferredSize(new Dimension(125, 20));
        dateDebut.setPreferredSize(new Dimension(125, 20));
        dateFin.setPreferredSize(new Dimension(125, 20));
        heureDebutC.setPreferredSize(new Dimension(130, 20));
        minuteDebutC.setPreferredSize(new Dimension(130, 20));
        heureFinC.setPreferredSize(new Dimension(130, 20));
        minuteFinC.setPreferredSize(new Dimension(130, 20));

        panneauDateDebut.add(dateDebut);
        panneauDateDebut.add(dateDebutT);
        panneauDateDebut.add(heureDebutC);
        panneauDateDebut.add(minuteDebutC);
        panneauDateFin.add(dateFin);
        panneauDateFin.add(dateFinT);
        panneauDateFin.add(heureFinC);
        panneauDateFin.add(minuteFinC);
        modificationAbsence2.add(panneauDateDebut, BorderLayout.CENTER);
        modificationAbsence2.add(panneauDateFin, BorderLayout.PAGE_END);

        // Modification Panneau Bas

        modificationAbsence3 = new JPanel();
        gestionnaireComposantBas = new FlowLayout(FlowLayout.RIGHT);
        modificationAbsence3.setLayout(gestionnaireComposantBas);

        modifier = new JButton("Modifier");
        annuler = new JButton("Annuler");

        modificationAbsence3.add(modifier);
        modificationAbsence3.add(annuler);

        this.add(modificationAbsence1, BorderLayout.NORTH);
        this.add(modificationAbsence2, BorderLayout.CENTER);
        this.add(modificationAbsence3, BorderLayout.SOUTH);
        /*this.*/setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.pack();
            /*this.*/setVisible(true);
    }

 public static void main(String s[]) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            CreerModificationAbsence textf = new CreerModificationAbsence();
        }
    });
}


}
4

2 回答 2

3

对于初学者来说很好(对于结束者来说,不知道这是否是英语):不要打电话setPreferredSize()!这就是导致你所有问题的原因。停止使用它(永远在你的生活中〜糟糕的幽默感,不要苛刻),你会解决你所有的问题。

试试这个:

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CreerModificationAbsence extends JDialog {

    private JPanel modificationAbsence1, modificationAbsence2, modificationAbsence3, panneauDateDebut, panneauDateFin;
    private JButton modifier, annuler;
    private JLabel raison, prenomNomEmpl, prenomNomChef;
    private JComboBox<String> raisonC, heureDebutC, heureFinC, minuteDebutC, minuteFinC;
    private JTextField prenomNomEmplT, prenomnomChefT;
    private final String[] raisonAbsence = { "Malade" };

    private JLabel dateDebut, dateFin;
    private JTextField dateDebutT, dateFinT;

    private final String[] heures = { "00" };
    private final String[] minutes = { "00", "15", "30", "45" };

    private BorderLayout gestionnaireComposant;
    private GridLayout gridGestionnaireComposant;
    private FlowLayout panneauMilieuLayout;
    final FlowLayout gestionnaireComposantBas;

    public CreerModificationAbsence() {
        super((Frame) null, "Modification - Absence d'employé", true);

        // setPreferredSize(new Dimension(600, 250));
        setAlwaysOnTop(true);
        setResizable(false);
        setAlwaysOnTop(true);
        gestionnaireComposant = new BorderLayout();
        this.getContentPane().setLayout(gestionnaireComposant);

        // Modification Panneau Haut

        modificationAbsence1 = new JPanel();

        gridGestionnaireComposant = new GridLayout(3, 2, 2, 2);
        modificationAbsence1.setLayout(gridGestionnaireComposant);

        raison = new JLabel("Raison : ");
        raisonC = new JComboBox(raisonAbsence);
        raisonC.setEditable(true);
        prenomNomEmpl = new JLabel("Prénom et Nom de l'employé : ");
        prenomNomEmplT = new JTextField();
        prenomNomChef = new JLabel("Prénom et Nom du chef d'équipe : ");
        prenomnomChefT = new JTextField();

        modificationAbsence1.add(raison);
        modificationAbsence1.add(raisonC);
        modificationAbsence1.add(prenomNomEmpl);
        modificationAbsence1.add(prenomNomEmplT);
        modificationAbsence1.add(prenomNomChef);
        modificationAbsence1.add(prenomnomChefT);

        // Modification Panneau Milieu

        modificationAbsence2 = new JPanel();
        panneauDateDebut = new JPanel();
        panneauDateFin = new JPanel();

        panneauMilieuLayout = new FlowLayout();

        panneauDateDebut.setLayout(panneauMilieuLayout);
        panneauDateDebut.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        panneauDateFin.setLayout(panneauMilieuLayout);
        panneauDateFin.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        dateDebutT = new JTextField(12);
        heureDebutC = new JComboBox(heures);
        minuteDebutC = new JComboBox(minutes);
        dateFinT = new JTextField(12);
        heureFinC = new JComboBox(heures);
        minuteFinC = new JComboBox(minutes);

        dateDebut = new JLabel("Date de début :");
        dateFin = new JLabel("Date de fin :");

        panneauDateDebut.add(dateDebut);
        panneauDateDebut.add(dateDebutT);
        panneauDateDebut.add(heureDebutC);
        panneauDateDebut.add(minuteDebutC);
        panneauDateFin.add(dateFin);
        panneauDateFin.add(dateFinT);
        panneauDateFin.add(heureFinC);
        panneauDateFin.add(minuteFinC);
        modificationAbsence2.add(panneauDateDebut, BorderLayout.CENTER);
        modificationAbsence2.add(panneauDateFin, BorderLayout.PAGE_END);

        // Modification Panneau Bas

        modificationAbsence3 = new JPanel();
        gestionnaireComposantBas = new FlowLayout(FlowLayout.RIGHT);
        modificationAbsence3.setLayout(gestionnaireComposantBas);

        modifier = new JButton("Modifier");
        annuler = new JButton("Annuler");

        modificationAbsence3.add(modifier);
        modificationAbsence3.add(annuler);

        this.add(modificationAbsence1, BorderLayout.NORTH);
        this.add(modificationAbsence2, BorderLayout.CENTER);
        this.add(modificationAbsence3, BorderLayout.SOUTH);
        /*this.*/setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.pack();
        setLocationRelativeTo(null);
        /*this.*/setVisible(true);
    }

    public static void main(String s[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CreerModificationAbsence textf = new CreerModificationAbsence();
            }
        });
    }

}
于 2012-12-19T20:43:10.300 回答
3

您遇到的问题是因为您setPreferredSizeJDialog. 所以发生的情况是 Dialog 必须是给定的大小。因为它现在必须是那个大小,所以必须给予一些东西。这就是 LayoutManagers 接管的地方。在 BorderLayout 中,Center 中的任何内容都将始终拉伸。

如果您设置有问题的两个面板的背景,您可以看到发生了什么:

    panneauDateDebut = new JPanel();
    panneauDateDebut.setOpaque(true);
    panneauDateDebut.setBackground(Color.blue);
    panneauDateFin = new JPanel();
    panneauDateFin.setOpaque(true);
    panneauDateFin.setBackground(Color.green);

解决方案(纪尧姆在我写作时指出)是停止使用setPreferredSize. 因此,如果您的构造函数如下所示,则应该解决您的问题:

public PageCentering() {
        super((Frame) null, "Modification - Absence d'employé", true);

        //setPreferredSize(new Dimension(600, 250));

        ...

}
于 2012-12-19T20:53:16.547 回答