Jframe.无法显示多个组件 [英] Jframe. Cant get multiple components to display

查看:162
本文介绍了Jframe.无法显示多个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法同时显示两个不同的组件.

I am having trouble getting two different components to display at the same time.

public class BandViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.setSize(1000, 800);
      frame.setTitle("band");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      BandComponent component = new BandComponent();
      ConcertBackground component1 = new ConcertBackground();
      frame.add(component);
      frame.add(component1);

      frame.setVisible(true);
   }
}

现在,我在这个论坛上阅读了您可以做的一些事情,以同时显示两者,但无法弄清楚它是如何完成的.谁能帮忙吗?我想让一个在另一个的前面.他们有某种控制分层的方法吗?提前致谢.

Now I read on this forum that you can do something to display both at the same time but wasn't able to figure out how it was done. Can anyone please help? I want to have one to be in front of the other. Is their some way to control the layering? Thanks in advance.

推荐答案

您遇到的问题是因为JFrame默认情况下使用BorderLayout. BorderLayout仅允许单个组件出现在其五个可用位置中的任何一个位置.

The problem you are having is because JFrame by default uses a BorderLayout. BorderLayout only allows a single component to appear in any one of it's five available positions.

要将多个组件添加到一个容器中,您需要配置布局或使用更能满足您需求的布局...

To add multiple components to a single container, you need to configure the layout or use one which better meets your needs...

看看布局管理器的可视指南有关更多示例

这篇关于Jframe.无法显示多个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆