简体   繁体   English

JFrame显示一个面板但没有显示另一个面板

[英]JFrame showing one panel but not showing another

Hey guys I am creating a JFrame and I have created 2 panels. 嘿伙计我正在创建一个JFrame,我创建了2个面板。 The first one is in the center with 3 buttons in a row and the second one is in the south with 3 buttons in a row. 第一个位于中心,连续有3个按钮,第二个位于南方,连续有3个按钮。 The frame is only showing the last panel made. 框架仅显示最后一个面板。 It is like it creates the first panel and then overwrites it with the second panel. 它就像它创建了第一个面板,然后用第二个面板覆盖它。 Here is my code: 这是我的代码:

private JPanel getSouthPanel()
{
  JPanel panel1 = new JPanel(new BorderLayout());
  panel1.add(button1, BorderLayout.WEST);
  panel1.add(button2, BorderLayout.CENTER);
  panel1.add(button3, BorderLayout.EAST);
  return panel1;
}

private JPanel getCenterPanel()
{
  JPanel panel = new JPanel(new BorderLayout());
  panel.add(button1, BorderLayout.WEST);
  panel.add(button2, BorderLayout.CENTER);
  panel.add(button3, BorderLayout.EAST);
  return panel;
}

public void layoutComponents()
{
  Container container = frame.getContentPane();
  container.setLayout(new BorderLayout());
  container.add(getCenterPanel(), BorderLayout.NORTH);
  container.add(getSouthPanel(), BorderLayout.SOUTH);
  frame.setSize(300, 300);
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

问题是您不能将相同的按钮放在两个不同的面板中,您需要为每个面板创建单独的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM