我的JPanel中的组件没有居中

时间:2015-03-15 16:36:35

标签: java swing layout jpanel border-layout

我遇到BorderLayout的问题...在headerPanel中,所有组件都应该居中,但看起来它们都被移到了上面。

你能解释一下为什么没有“this.add(new JPanel())”所有的布局都完全被破坏了吗?

屏幕截图:

http://puu.sh/gBoP4/60eaebe3bb.png

 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 -> {

});

}
}

1 个答案:

答案 0 :(得分:2)

您使用的布局不正确:

  • 您在组件上调用setBounds(...)仅适用于null布局的内容,这是您不应使用的布局。
  • 您将所有内容放在使用默认FlowLayout
  • 的JPanel中
  • 我猜你正在设置顶级窗口的大小,而不是在这个窗口上调用pack()

相反,你会想:

  • 小心使用FlowLayout,尤其是在无意中使用时,因为它是“最愚蠢”的布局管理器之一。不要误解我的意思 - 它有它的用途,但我通常会避免使用它作为我的“主”JPanel的布局,这是我的GUI组件的大部分内容。
  • 避免致电setSize(...)setPreferredSize(...)
  • 避免在任何
  • 上致电setBounds(...)
  • 避免使用null布局的诱惑。
  • 在添加所有组件后,在调用pack()
  • 之前,在GUI 上调用setVisible(true)
  • 如果仍然卡住,请显示所需GUI布局的图片
  • 并且发布没有大量与您的问题无关的垃圾的代码。例如,您发布的代码应该编译,应该运行,并且不应该扩展IDPanel或依赖于我们无权访问的任何内容。