Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 JLabel和JApplet内的图像-图像不是';我没有出现_Java_Image_Swing_Jlabel_Japplet - Fatal编程技术网

Java JLabel和JApplet内的图像-图像不是';我没有出现

Java JLabel和JApplet内的图像-图像不是';我没有出现,java,image,swing,jlabel,japplet,Java,Image,Swing,Jlabel,Japplet,在输出标签上显示,但图像不显示。当我移除整个标签部分时,它会发生。我不知道我是否也必须使用clearRect方法 public class Applet1 extends JApplet{ Image img; JLabel label; public void init(){ img = getImage(getDocumentBase(), getParameter("imagePath")); label = new JLabel(getParameter("lab

在输出标签上显示,但图像不显示。当我移除整个标签部分时,它会发生。我不知道我是否也必须使用clearRect方法

public class Applet1 extends JApplet{

Image img;
JLabel label;

public void init(){

    img = getImage(getDocumentBase(), getParameter("imagePath"));

    label = new JLabel(getParameter("labelText"));
    label.setFont(new Font(getParameter("labelFont"), Font.PLAIN, Integer.parseInt(getParameter("labelSize"))));

    add(label);
    setVisible(true);
}

public void paint(Graphics g){
    g.clearRect(0, 0, 300, 200);
    g.drawImage(img, 0, 0, 300, 200, this);
}

}
不要重写JApplet的paint()方法

自定义绘制通过重写JPanel的
paintComponent()
方法完成。不要忘记调用super.paintComponent()。然后将面板添加到小程序中

最后,将标签添加到面板中

另外,有关创建小程序的正确方法,请参见。它展示了如何使用EDT以及加载图像的更好方法。

不要覆盖JApplet的paint()方法

自定义绘制通过重写JPanel的
paintComponent()
方法完成。不要忘记调用super.paintComponent()。然后将面板添加到小程序中

最后,将标签添加到面板中


另外,有关创建小程序的正确方法,请参见。它展示了如何使用EDT和一种更好的加载图像的方法。

现在我有了类似的东西,我在下面发布了。它起作用了。谢谢

public class Applet1 extends JApplet{

JLabel label;
CustomPanel panel;

public void init(){

    panel = new CustomPanel(getImage(getDocumentBase(), getParameter("imagePath")));

    // ustawianie etykiety
    label = new JLabel(getParameter("labelText"));
    label.setFont(new Font(getParameter("labelFont"), Font.PLAIN, Integer.parseInt(getParameter("labelSize"))));

    panel.add(label);
    panel.setVisible(true);

    add(panel);
    setVisible(true);
} 
}

class CustomPanel extends JPanel{

Image img;

public CustomPanel(Image img){
    this.setLayout(new GridLayout(1,2));
    this.img = img;
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.clearRect(0, 0, 300, 200);
    g.drawImage(img, 0, 0, 300, 200, this);
}
}

现在我有了这样的东西,我在下面发布了。它起作用了。谢谢

public class Applet1 extends JApplet{

JLabel label;
CustomPanel panel;

public void init(){

    panel = new CustomPanel(getImage(getDocumentBase(), getParameter("imagePath")));

    // ustawianie etykiety
    label = new JLabel(getParameter("labelText"));
    label.setFont(new Font(getParameter("labelFont"), Font.PLAIN, Integer.parseInt(getParameter("labelSize"))));

    panel.add(label);
    panel.setVisible(true);

    add(panel);
    setVisible(true);
} 
}

class CustomPanel extends JPanel{

Image img;

public CustomPanel(Image img){
    this.setLayout(new GridLayout(1,2));
    this.img = img;
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.clearRect(0, 0, 300, 200);
    g.drawImage(img, 0, 0, 300, 200, this);
}
}

我用了你的建议,效果很好。我将在8小时后发布解决方案(我的重复次数不到10次)。谢谢!仔细查看原始的
paint()
覆盖,第三段似乎是“.将标签添加到面板。”(带有
EmptyBorder
)是这个特定用例所需的全部内容-无需任何自定义绘制。我已经使用了您的建议,它很有效。我将在8小时后发布解决方案(我的重复次数不到10次)。谢谢!仔细查看原始的
paint()
覆盖,似乎第三段“.将标签添加到面板上。”(带有
EmptyBorder
)是此特定用例所需的全部内容-无需任何自定义绘制。