gpt4 book ai didi

Java JPanel 不会显示在 JFrame 中

转载 作者:行者123 更新时间:2023-11-29 08:01:36 25 4
gpt4 key购买 nike

这是我第一次使用 stackoverflow,我遇到了一个我似乎无法深究的问题。我正在尝试独立开发一个小型桌面游戏,它使用一个名为“Frame.java”的类来创建一个包含所有菜单的 JFrame,因此我需要使用一个名为“create()”的方法来返回框架。我还使用了另一个名为“MainPanel.java”的类,它扩展了 JPanel,以创建 MainPanel 的实例以添加到框架的内容 Pane 中。然而,当我运行我的小驱动程序中的所有代码时,似乎没有显示任何内容。任何帮助将不胜感激!

public class MainPanel extends JPanel{

//the background image of the game
private BufferedImage img = null;
//GUI components of the game
private JPanel gameWindow, gameWindowHolder, gameInfoHolder, LevelPanel, RevenuePanel,
ActionPanel, TimePanel;

public MainPanel(String path, int width, int height){

//create BufferedImage based on path
img = new ImageHelper().createBufferedImage(path);
//use img to create JPanel gameWindow
gameWindow = ImageHelper.makeImageComponent(img, width, height);

gameInfoHolder = new JPanel();
gameInfoHolder.setPreferredSize(new Dimension(width+10, height+10));
gameInfoHolder.setBackground(Color.black);
gameInfoHolder.add(gameWindow);

//set size of this MainPanel
setPreferredSize(new Dimension(width+300, height+10));
//add gameInfoHolder to MainPanel
add(gameInfoHolder);

}

public void paint(Graphics g) {
super.paintComponent(g);

}
}

public class Driver {


public static void main( String []args){
JFrame frame = Frame.create();

JPanel panel = new MainPanel("images/backgrounds/jessicaAlba.jpg", 330, 500);

frame.getContentPane().add(panel);

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

}

public class ImageHelper {


//
//Returns an ImageIcon object based on the path
//
public ImageIcon createImageIcon(String path, String description){

URL imgURL = getClass().getResource(path);
if (imgURL != null)
return new ImageIcon(imgURL, description);
else {
System.err.println("Couldn't find this file: " + path + ". Check path.");
return null;
}
}

//
//Returns an Image object based on the path
//
public Image createImage(String path){

URL imgURL = getClass().getResource(path);
Toolkit kit = Toolkit.getDefaultToolkit();

if (imgURL != null){
return kit.createImage(imgURL);
}
else {
System.err.println("Couldn't find this file: " + path + ". Check path.");
return null;
}

}

//
//Returns a BufferedImage object based on the path
//
public BufferedImage createBufferedImage(String path){
BufferedImage image;
URL imgURL = getClass().getResource(path);
try
{
image = ImageIO.read(imgURL);
}
catch (IOException e)
{
System.err.println("Couldn't find this file: \""+ path +"\". Check path.");
return null;
}
return image;
}

//
//Returns a JPanel object composed of the image found in the path.
//
public static JPanel makeImageComponent(String path, int width, int height){
BufferedImage image;
JLabel picLabel;
ImageIcon icon;

JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
ImageHelper h = new ImageHelper();

image = h.createBufferedImage(path);
image = resize(image, width, height);
icon = new ImageIcon(image);

panel.setPreferredSize(new Dimension(width, height));
panel2.setPreferredSize(new Dimension(width+10, height+10));

panel2.setBackground(Color.black);
picLabel = new JLabel(icon);
panel.add(picLabel);
panel2.add(panel);

return panel2;
}

//
//Returns a JPanel object composed of the BufferedImage object in the argument
//
public static JPanel makeImageComponent(BufferedImage image, int width, int height){
JLabel picLabel;
ImageIcon icon;

JPanel panel = new JPanel();
image = resize(image, width, height);
icon = new ImageIcon(image);
panel.setPreferredSize(new Dimension(width, height));
picLabel = new JLabel(icon);
panel.add(picLabel);

return panel;
}

//
//Resizes the BufferedImage object to the specified new width and new height.
//
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
}

最佳答案

当您使用 super.paintComponent 将绘画“堆栈”短路时,不会显示任何内容,因此不会绘制任何子组件。

public void paint(Graphics g) {
super.paintComponent(g);
}

因为这确实没有任何用处,所以可以将其删除并显示添加的面板。

关于Java JPanel 不会显示在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14047996/

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