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 用户单击clickme按钮时需要显示图像_Java_Image_Swing_Jlabel_Actionlistener - Fatal编程技术网

Java 用户单击clickme按钮时需要显示图像

Java 用户单击clickme按钮时需要显示图像,java,image,swing,jlabel,actionlistener,Java,Image,Swing,Jlabel,Actionlistener,当用户单击按钮时,我需要在标签中显示图像。我在ActionListener中编写了一些代码,但它不起作用 btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ImageIcon one = new ImageIcon("E:\\image1.jpg"); panel_1

当用户单击按钮时,我需要在标签中显示图像。我在ActionListener中编写了一些代码,但它不起作用

        btnNewButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            ImageIcon one = new ImageIcon("E:\\image1.jpg");
            panel_1.setLocation(20, 100);
            panel_1.setSize(115, 115);
            mbutton.setIcon(one);
            panel_1.add(mbutton);
            // mbutton.setText("You changed me");
        }
    });

我对你的代码有点困惑,你说要在JLabel中添加一个图像,但是代码显示你添加了一个变量mButton并设置了它的图标,这让我觉得它是JButton

无论哪种方式,JLabel/JButton是已经添加到容器中,还是也在动态添加

1如果可视容器上尚未出现JLabel/JButton:

在可见容器上添加/删除或更改组件的大小/布局后,应调用revalidate并在容器上重新绘制实例以反映更改:

//code which adds component to visible panel_1

//so changes can be reflected
panel_1.revalidate();
panel_1.repaint();
2如果JLabel/JButton可见,则setIcon应在调用重新验证时工作,并重新绘制自身,如JLabelSecon所示,JButton相同:

     * 108: getfield      #294                // Field defaultIcon:Ljavax/swing/Icon;
     * 111: invokeinterface #346,  1          // InterfaceMethod javax/swing/Icon.getIconHeight:()I
     * 116: aload_2
     * 117: invokeinterface #346,  1          // InterfaceMethod javax/swing/Icon.getIconHeight:()I
     * 122: if_icmpeq     129
     * 125: aload_0
     * 126: invokevirtual #319                // Method revalidate:()V
     * 129: aload_0
     * 130: invokevirtual #318                // Method repaint:()V
因此,还有其他问题,比如你的LayoutManager没有正确地支持添加新组件,因此它没有被放置在可见的空间中,请发布一篇文章

我还看到您正在使用setSize。。和设置位置。。因此,我感觉您正在使用绝对/空LayoutManager,这是不推荐的,可能会导致许多问题。而是使用适当的LayoutManager查看此处了解更多信息:


我对你的代码有点困惑,你说要在JLabel中添加一个图像,但是代码显示你添加了一个变量mButton并设置了它的图标,这让我觉得它是JButton

无论哪种方式,JLabel/JButton是已经添加到容器中,还是也在动态添加

1如果可视容器上尚未出现JLabel/JButton:

在可见容器上添加/删除或更改组件的大小/布局后,应调用revalidate并在容器上重新绘制实例以反映更改:

//code which adds component to visible panel_1

//so changes can be reflected
panel_1.revalidate();
panel_1.repaint();
2如果JLabel/JButton可见,则setIcon应在调用重新验证时工作,并重新绘制自身,如JLabelSecon所示,JButton相同:

     * 108: getfield      #294                // Field defaultIcon:Ljavax/swing/Icon;
     * 111: invokeinterface #346,  1          // InterfaceMethod javax/swing/Icon.getIconHeight:()I
     * 116: aload_2
     * 117: invokeinterface #346,  1          // InterfaceMethod javax/swing/Icon.getIconHeight:()I
     * 122: if_icmpeq     129
     * 125: aload_0
     * 126: invokevirtual #319                // Method revalidate:()V
     * 129: aload_0
     * 130: invokevirtual #318                // Method repaint:()V
因此,还有其他问题,比如你的LayoutManager没有正确地支持添加新组件,因此它没有被放置在可见的空间中,请发布一篇文章

我还看到您正在使用setSize。。和设置位置。。因此,我感觉您正在使用绝对/空LayoutManager,这是不推荐的,可能会导致许多问题。而是使用适当的LayoutManager查看此处了解更多信息:

希望这能对你有所帮助。希望这能对你有所帮助。