gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:54:00 25 4
gpt4 key购买 nike

我做了一个简单的骰子游戏,我有一个 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
}

}

最后我有一个查看器类,它有一个 main 方法和一个框架。直接在框架上绘制新的骰子面对象效果很好(显示骰子),如下所示 封装面板;

    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);




}

}

最佳答案

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

另一种选择是

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

关于java,组件仅在直接添加到框架时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19738332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com