Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout不';t显示JPanel组件_Java_Swing_Gridbaglayout - Fatal编程技术网

Java GridBagLayout不';t显示JPanel组件

Java GridBagLayout不';t显示JPanel组件,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我需要帮助,因为我的JPanel组件没有显示在另一个组件中。我曾经使用过这种布局,所以我在这方面有一些经验,但在这种情况下仍然有问题。请不要回答使用其他布局管理器。我的大部分项目都基于GridBagLayout 因此,它是我的父容器: public class MainArea extends WorkArea { private static final long serialVersionUID = 1L; private GridBagConstraints gbc; private Lo

我需要帮助,因为我的JPanel组件没有显示在另一个组件中。我曾经使用过这种布局,所以我在这方面有一些经验,但在这种情况下仍然有问题。请不要回答使用其他布局管理器。我的大部分项目都基于GridBagLayout

因此,它是我的父容器:

public class MainArea extends WorkArea
{
private static final long serialVersionUID = 1L;
private GridBagConstraints gbc;
private LogoPanel logo;

public MainArea()
{
    gbc = new GridBagConstraints();
    logo = new LogoPanel();
    JTextField tf = new JTextField(20);
    gbc.gridx = 0;
    gbc.gridy = 0;
    add(logo,gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    add(tf,gbc);
}
}
布局管理器是在WorkArea类中设置的,它肯定是正确完成的。我添加的JTextField仅用于测试,它正确显示,不像徽标。以下是徽标代码:

public class LogoPanel extends JPanel
{
private BufferedImage image;

public LogoPanel()
{
    File imageFile = new File("images/Logo.jpg");
    try
    {
        image = ImageIO.read(imageFile);
    }
    catch (IOException e)
    {
        System.err.println("Blad odczytu logo");
        e.printStackTrace();
    }
}

@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2d = (Graphics2D) g;
    Screen screen = Screen.getInstance();
    g2d.drawImage(image, screen.getScreenWidth()/2-200, screen.getScreenHeight()/2-100, this);
}   
}

请帮帮我。我不知道这里可能出了什么问题。我还在添加后尝试了重新绘制方法和验证,但没有结果。

尝试在徽标面板上调用
setVisible(true)

尝试覆盖
公共维度getPreferredSize()
在LogoPanel类中,以便GridBagLayout知道面板的大小


正如@nIcE cOw指出的,始终调用
super.paintComponent(g)
作为
paintComponent()方法的第一行

您对
super.paintComponent(g)
的呼叫在哪里?你确定它能很好地加载图像吗?如果不能,请看一下这个关于如何使用setPreferredSize解决LogoPanel问题的线程。这很奇怪,因为如果我使用不同的布局,它不是必需的。泰晤士报