一个JFrame中的两个面板

时间:2013-11-02 22:14:28

标签: java swing layout jframe jpanel

我知道之前有很多关于这个主题的主题,我已经广泛使用Google来解决这个问题,但无论我做什么,我都无法在一个JFrame中显示两个面板。我尝试过很多东西,包括CardLayout,但似乎没有用。我觉得这是一个非常简单的解决方案..

目的是通过随机定位来查看两个面板之间的差异。

以下是一些java文件(只有相关的代码 - 还有一个Snowman.java,但它只提供类似ghost.java的雪人的绘图):

Ghost.java

import java.awt.*;

public class Ghost
{
private int x, y;
private String name;
private boolean happy;
private Color color;

public Ghost (Color shade, int upperX, int upperY, String a, boolean b)
{
  color = shade;
  x = upperX;
  y = upperY;
  name = a;
  happy = b;
}   

public void draw (Graphics page)
{
  page.setColor(color);
  page.fillOval (x, y, 50, 125);
  page.fillRect (x+1, y+60, 49, 75); // body of ghost

  page.setColor(Color.black);
  page.fillOval (x+12, y+10, 8, 8);
  page.fillOval (x+32, y+10, 8, 8); // eyes

  page.setColor(Color.white);
  page.drawString(name, x+8, y-5); // name

  page.setColor(Color.black);
  if (happy == true)
     page.fillArc  (x+10, y+30, 33, 10, 180, 180);
  else
     page.fillArc  (x+10, y+30, 33, 10, 0, 180); // smile/frown
}     

public void setColor (Color shade)
{
  color = shade;
}

public void setX (int upperX)
{
  x = upperX;
}

public void setY (int upperY)
{
  y = upperY;
}

HalloweenPanel.java:

import javax.swing.*;   
import java.awt.*;
import java.util.Random;

public class HalloweenPanel extends JPanel
{
 private Ghost ghost1, ghost2, ghost3;
 private Snowman snowman1, snowman2;

 public HalloweenPanel()
 {
  setBackground (Color.black);

  Random rand = new Random();
  int randomX = rand.nextInt(700)+50;
  int randomY = rand.nextInt(400)+50;
  boolean randomHappy = (Math.random() < 0.5);
  boolean randomHappy2 = (Math.random() < 0.5);

  ghost1 = new Ghost (Color.white, randomX, randomY, "Scary", randomHappy2);
  ghost2 = new Ghost (Color.blue, randomX, 100, "Happy", true);
  ghost3 = new Ghost (Color.yellow, 300, randomY, "Scared", randomHappy);

  snowman1 = new Snowman (500, 50);
  snowman2 = new Snowman (750, 100);  
  }

  public void paintComponent (Graphics page)
  {
  Random rand = new Random();
  int randomX2 = rand.nextInt(900);
  int randomY2 = rand.nextInt(500);
  super.paintComponent(page);
  ghost2.setX(randomX2);
  ghost3.setY(randomY2);
  ghost1.draw(page);
  ghost2.draw(page);
  ghost3.draw(page);
  snowman1.draw(page);
  snowman2.draw(page);
 }
}

HalloweenDriver.java:

import javax.swing.*;
import java.awt.*;

public class HalloweenDriver
{
public static void main (String[] args)
 {
  JFrame frame = new JFrame ("Halloween - Winter is Coming..");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

  JPanel mainPanel = new JPanel();
  HalloweenPanel panel = new HalloweenPanel();
  HalloweenPanel panel2 = new HalloweenPanel();

  mainPanel.setLayout(new GridLayout(1,0));

  mainPanel.add(panel);
  mainPanel.add(panel2);

  frame.add(mainPanel);

  frame.pack();
  frame.setVisible(true);
 }
}   

1 个答案:

答案 0 :(得分:4)

frame.getContentPane().add(panel);
frame.getContentPane().add(panel2);

您正在尝试将两个面板添加到BorderLayout的CENTER中。这是不可能做到的。

尝试:

frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(panel2, BorderLayout.SOUTH);

看到差异。

如果这不是您想要的,请尝试另一个布局管理器。阅读Layout Managers上的Swing教程。

或者,如果您尝试将两个面板堆叠在一起(在Z轴上),请尝试OverlayLayout。本教程不涉及这一点,因此您需要查看API。您还需要使“顶部”面板非透明(),以便它不会在底部面板的顶部绘制。