当前位置: 首页 > 面试题库 >

难以在同一jlabel上重新显示其他img

庄元龙
2023-03-14
问题内容

我有点卡住。当我按下按钮Submit时,应该在JLabel图像的相同位置重新显示另一张图片,因此,如果有人有任何想法,我将感谢他们使用eclipse,并且该程序正在编译并运行。这是代码

/** Here is the GUI of the program
 * class name SlideShowGui.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SlideShowGui extends JPanel  implements ActionListener
{
    JLabel name, comments, images;
    JTextField namejtf, commentsjtf, captionjtf;
    JButton submit;
    ImageIcon pictures;


    SlideShowGui()
    {


        name = new JLabel("Name:");
        this.add(name);

        namejtf = new JTextField(15);
        this.add(namejtf);

        comments = new JLabel("Comments:");
        this.add(comments);

        commentsjtf = new JTextField(15);
        this.add(commentsjtf);

        submit = new JButton("Submit");
        this.add(submit);
        submit.addActionListener(this);


        pictures = new ImageIcon("galileo1.jpg");
        images = new JLabel(pictures);
        this.add(images);


//      pictures2 = new ImageIcon("galileo2.jpg");
//      images2 = new JLabel(pictures2);
//      this.add(images2);



        captionjtf = new JTextField(24);
        this.add(captionjtf);

           public void actionPerformed(ActionEvent ae)
        {
        if(ae.getSource() == submit)
        {

            pictures = new ImageIcon("galileo2.jpg");
            images = new JLabel(pictures);

            System.out.println("test");
        }

    }
}

    }

/**The driver class of the program. Here is the JFrame 
 * class name TestSlideShow.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import javax.swing.*;
public class TestSlideShow 
{
    public static void main(String[] args) 
    {
        JFrame application = new JFrame();
        SlideShowGui panel = new SlideShowGui();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,600);
        application.setLocation(400,100);
        application.setVisible(true);


    }

}

问题答案:

改变这个

if(ae.getSource() == submit)
    {

        pictures = new ImageIcon("galileo2.jpg");
        images = new JLabel(pictures);

        System.out.println("test");
    }

if(ae.getSource() == submit)
    {

        pictures = new ImageIcon("galileo2.jpg");
        images.setIcon(pictures);

        System.out.println("test");
    }


 类似资料:
  • 我试图在GUI中显示卡片,我希望重叠卡片以节省空间。然而,我似乎无法让它发挥作用。我目前正在使用MigLayout,但感觉它更像是一个JLabel问题,而不是其他问题。 要把它描述在一个高水平,考虑在现实生活中玩纸牌游戏。当你正常拿牌时,你只需要看到牌的一部分,因此牌的大部分被另一张牌重叠,以此类推,直到最后一张牌完全显示出来,因为上面没有任何东西。我想重叠的方式,只有一部分卡下面显示,然后顶部的

  • 问题内容: Java可以显示png,jpg和其他图片格式,但是我必须通过获取文件路径在JLable中显示bmp文件。 ImageIcon支持典型图像。 在我正在工作的项目中,我无法打开bmp文件并以jpg格式存储同一文件,因为我不允许在运行时存储某些内容。我只能将图像保存在内存中。但是我不知道该怎么做。 我怎样才能显示在 ? 谢谢 问题答案: 我发现一些用Java 1.5编写的类,但是您可以轻松更

  • 问题内容: 我有一个位于JButton顶部的JLabel,但它不会显示在顶部。注释掉JButton的代码后,将显示JLabel,这意味着它在其中,但在底部。有没有办法在JButton的顶部显示JLabel? 任何帮助都是极好的。谢谢! 编辑: 为了澄清起见,我需要在游戏中使用该按钮,单击该按钮时,按钮上方会显示一个JLabel,以显示添加的“得分”。在我的游戏中,JLabel将比JButton小,

  • 我使用Liferay portlet bundle 6.2(Tomcat)和Liferay IDE(eclipse)。我创建了两个Liferay插件项目。在第一个portlet(第一个插件portlet)中,我使用portlet首选项保存了一些数据。现在,我将在第一个portlet中显示第二个portlet。我怎么能做到?我找到了下面的代码和这个链接 从其他portlet打开portlet 但是,

  • 我有png图标,并将它们用作/中的图标 问题是,运行时显示的图像比原始图标大,而且由于这种大小调整,它非常难看。 下面是一个例子: 原始图标(左)以及它如何在中呈现(右) 这个简单示例的源代码如下: 这是预期的吗?如果没有,我如何避免这种情况?我尝试了很多方法来强制显示图像的大小、按钮等,但无法显示正确的图像。 我测试过各种尺寸的图标:16x16、17x17、18x18、19x19、20x20,每