我将一个 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();
}
});
}
}