我遇到BorderLayout的问题...在headerPanel中,所有组件都应该居中,但看起来它们都被移到了上面。
你能解释一下为什么没有“this.add(new JPanel())”所有的布局都完全被破坏了吗?
屏幕截图:
public class HomePanel extends IDPanel {
private static final String DATE = "Par date";
private static final String ENTREPRISE = "Par entreprise";
private static final String PDC = "Par PDC";
private static final String ACCESS = "Accès";
private static final String NAME = "Nom";
private static final String ADRESSE = "Adresse";
private static final String PART_STATE = "Etat de la participation";
private BasicArrowButton rowRight;
private BasicArrowButton rowLeft;
private JButton newJourneyButton;
private JButton researchOkButton;
private JButton adminButton;
private JButton deconnexionButton;
private JComboBox<String> researchChoice;
private JTextField research;
private JLabel errorMsg;
private JLabel userHello;
private JLabel entrepriseJourney;
private JLabel entrepriseAccess;
private JLabel entrepriseName;
private JLabel entrepriseAdresse;
private JLabel entreprisePart;
private JPanel headerJPanel;
private JPanel headerWest;
private JPanel headerCenter;
private JPanel headerEast;
public HomePanel(boolean isAdmin) {
super();
this.setId(IHMConstantes.ID_HOME_PANEL);
String[] test = {DATE, ENTREPRISE, PDC};
if (isAdmin) {
adminButton = new JButton("ADMIN");
}
rowRight = new BasicArrowButton(BasicArrowButton.RIGHT);
rowLeft = new BasicArrowButton(BasicArrowButton.LEFT);
newJourneyButton = new JButton("Nouvelle journée");
researchOkButton = new JButton("OK");
deconnexionButton = new JButton("Déconnexion");
researchChoice = new JComboBox<>(test);
research = new JTextField("Rechercher...");
errorMsg = new JLabel("");
userHello = new JLabel("Bonjour Utilisateur.Prenom");
entrepriseJourney = new JLabel("Entre.journée");
entrepriseAccess = new JLabel(ACCESS);
entrepriseName = new JLabel(NAME);
entrepriseAdresse = new JLabel(ADRESSE);
entreprisePart = new JLabel(PART_STATE);
headerJPanel = new JPanel(new BorderLayout());
headerWest = new JPanel();
headerCenter = new JPanel();
headerEast = new JPanel();
headerWest.setBorder(BorderFactory.createLineBorder(Color.BLACK));
headerCenter.setBorder(BorderFactory.createLineBorder(Color.BLUE));
headerEast.setBorder(BorderFactory.createLineBorder(Color.GREEN));
if (isAdmin) {
headerJPanel.setBounds(45, 75, 655, 50);
adminButton.setBounds(700, 75, 90, 50);
} else {
headerJPanel.setBounds(80, 75, 680, 50);
}
deconnexionButton.setBounds(650, 20, 150, 25);
userHello.setBounds(335, 190, 200, 20);
newJourneyButton.setPreferredSize(new Dimension(150, 25));
headerWest.setPreferredSize(new Dimension(180, 0));
headerEast.setPreferredSize(new Dimension(290, 0));
if (isAdmin) {
this.add(adminButton);
}
this.add(deconnexionButton);
this.add(userHello);
this.add(headerJPanel, BorderLayout.CENTER);
this.add(new JPanel());
headerJPanel.add(headerWest, BorderLayout.WEST);
headerJPanel.add(headerCenter, BorderLayout.CENTER);
headerJPanel.add(headerEast, BorderLayout.EAST);
headerWest.add(newJourneyButton, BorderLayout.CENTER);
headerCenter.add(rowLeft);
headerCenter.add(entrepriseJourney);
headerCenter.add(rowRight);
headerEast.add(researchChoice);
headerEast.add(research);
headerEast.add(researchOkButton);
// entrepriseJourney.add(entrepriseAccess);
// entrepriseJourney.add(entrepriseName);
// entrepriseJourney.add(entrepriseAdresse);
// entrepriseJourney.add(entreprisePart);
if (isAdmin) {
adminButton.addActionListener(e -> {
});
}
deconnexionButton.addActionListener(e -> {
});
newJourneyButton.addActionListener(e -> {
});
researchOkButton.addActionListener(e -> {
});
researchOkButton.addActionListener(e -> {
});
}
}
答案 0 :(得分:2)
您使用的布局不正确:
setBounds(...)
仅适用于null
布局的内容,这是您不应使用的布局。pack()
。相反,你会想:
setSize(...)
或setPreferredSize(...)
setBounds(...)
null
布局的诱惑。pack()
setVisible(true)