java,组件只在直接添加到框架时显示

时间:2013-11-02 02:27:17

标签: java swing jpanel jcomponent

我做了一个简单的骰子游戏,我有一个定义diceFace的DiceFace类

    package panel;
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class DiceFace
     {

  // Holds the seven possible dot positions on a standard die
  private Ellipse2D.Double[] dots = new Ellipse2D.Double[7];

 private Rectangle box;
 private int xLeft;
 private int yTop;
 private int side;
 private int diceValue;

public DiceFace(int s, int x, int y, int v)
{
    side = s;       // dimension of dice face
    xLeft = x;      // position
    yTop = y;       // position
    diceValue = v; // pip value
}

public void draw(Graphics2D g2)
{
    box = new Rectangle(xLeft, yTop, side, side);
  makeDots();

  // Black background
    g2.setColor(Color.BLACK);
  g2.fill(box);
  // White dots on black
  g2.setColor(Color.WHITE);

    // Draw dots
  if (diceValue == 1) 
        g2.fill(dots[0]);
  else if (diceValue == 2)
  {
     g2.fill(dots[1]); g2.fill(dots[2]);
  }
  else if (diceValue == 3)
  {
     g2.fill(dots[0]); g2.fill(dots[1]); g2.fill(dots[2]);
  }
  else if (diceValue == 4)
  {
     g2.fill(dots[1]); g2.fill(dots[2]);
     g2.fill(dots[3]); g2.fill(dots[4]);
  }
  else if (diceValue == 5)
  {
     g2.fill(dots[0]); g2.fill(dots[1]);
     g2.fill(dots[2]); g2.fill(dots[3]); g2.fill(dots[4]);
   }
  else if (diceValue == 6)
  {
     g2.fill(dots[1]); g2.fill(dots[2]); g2.fill(dots[3]);
     g2.fill(dots[4]); g2.fill(dots[5]); g2.fill(dots[6]);
   }
}


public void makeDots()
{
   int w = side/6;   // dot width
   int h = side/6;   // dot height

   dots[0] =  new Ellipse2D.Double(xLeft + (2.5 * side)/6,
                                     yTop + (2.5 * side)/6, h, w);

   dots[1] = new Ellipse2D.Double(xLeft + (3.75 * side)/6,
                                    yTop + (3.75 * side)/6, h, w);

   dots[2] = new Ellipse2D.Double(xLeft + (1.25 * side)/6,
                                    yTop + (1.25 * side)/6, h, w);

   dots[3] = new Ellipse2D.Double(xLeft + (1.25 * side)/6,
                                    yTop + (3.75 * side)/6, h, w);

   dots[4] = new Ellipse2D.Double(xLeft + (3.75 * side)/6,
                                    yTop + (1.25 * side)/6, h, w);

   dots[5] =  new Ellipse2D.Double(xLeft + (1.25 * side)/6,
                                     yTop + (2.5 * side)/6, h, w);

   dots[6] =  new Ellipse2D.Double(xLeft + (3.75 * side)/6,
                                     yTop + (2.5 * side)/6, h, w);
}

}

我还有一个DiceFaceConstructor类,它扩展了JComponent

package panel;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;


public class DiceFaceComponent extends JComponent {



public void paintComponent(Graphics g) //method returns void, takes object of type           Graphics which it refers to internally as g
{
    Graphics2D g2 = (Graphics2D)g; //casting 

    DiceFace dice1 = new DiceFace(60,0,0,6);  
    dice1.draw(g2); //draw the andgate  
    }

}

最后我有一个具有主方法和框架的查看器类。直接在框架上绘制一个新的骰子面对象可以正常工作(显示骰子),如下所示         包装面板;

    import java.awt.Component;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class PanelViewer
    {

/**
 * @param args
 */
public static void main(String[] args)
{

 JFrame frame = new JFrame();
 frame.setSize(600, 400);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.add(dice1)
 DiceFaceComponent dice1 = new DiceFaceComponent();
     frame.add(dice1)
 //JPanel panel1 = new JPanel();
 //panel1.add(dice1);

 //frame.add(panel1);
 //frame.pack();
 frame.setVisible(true); 




}

      }

问题是当我尝试将我的骰子组件添加到面板然后将面板添加到框架时,没有显示任何内容,如下所示。请帮忙,已经五个小时了。

    package panel;

    import java.awt.Component;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class PanelViewer
     {

/**
 * @param args
 */
public static void main(String[] args)
{

 JFrame frame = new JFrame();
 frame.setSize(600, 400);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 DiceFaceComponent dice1 = new DiceFaceComponent();
 JPanel panel1 = new JPanel();
 panel1.add(dice1);
 frame.add(panel1);
 frame.pack();
 frame.setVisible(true); 




}

  }

1 个答案:

答案 0 :(得分:1)

你的布局问题。默认情况下,JFrame使用BorderLayout,JPanel使用FlowLayout。通过将DiceFaceComponent JComponent添加到使用JPanel的FlowLayout,DiceFaceComponent JComponent将尝试调整其preferredSize的大小,该大小可能是[0,0]或最多[1,1]。一个快速的解决方案是给JPanel BorderLayout,然后你的JComponent将填充它并显示。

另一种选择是

  • 为您的DiceFace类添加一个public Dimension getPreferredSize(),返回一个足以显示图像的Dimension。这不是方法覆盖。
  • 为DiceFaceComponent JComponent提供一个私有DiceFace字段,该字段可由其构造函数设置,并可能使用setter方法更改(如果需要)。
  • 让DiceFaceComponent JComponent paintComponent方法覆盖绘制此DiceFace实例(如果它不为null)。不要在方法中创建新的DiceFace实例。
  • 为DiceFaceComponent JComponent提供getPreferredSize()覆盖方法,帮助它智能地选择最佳大小。您的方法将检查DiceFace实例是否为null,如果是,则调用super方法,否则它将调用DiceFace实例的getPreferredSize()方法。