在每行上添加图像和JLabel

时间:2018-11-07 17:11:33

标签: java image user-interface add gif

尝试添加带有标签的JLabel。每张图片的名称均根据程序。因此,我希望它们单独显示在右侧的图标上。 4.创建一个JFrame,显示您的三个
JFrame中最喜欢的图片(将图像缩小以使框架适合
进入屏幕,同时显示所有三个图像…并且图像必须是 像家人,朋友,宠物,汽车等)。每个图片都应标有
名称和描述图片的文本区域,例如Fido Description

import javax.swing.*;
import java.awt.*;
public class Lab2Part4 extends JFrame {
public Lab2Part4(){

        super("Pictures");
        Container c = getContentPane();
        JPanel panel = new JPanel();

        JLabel labelDog = new JLabel("This is a dog");
        Icon dogIcon = new ImageIcon("dog1.GIF");
        labelDog.setIcon(dogIcon);
        labelDog.setHorizontalAlignment(JLabel.RIGHT);


        panel.add(labelDog);

        JLabel labelCat = new JLabel("This is a cat");
        Icon catIcon = new ImageIcon("cat1.gif");
        labelCat.setIcon(catIcon);
        labelCat.setHorizontalAlignment(JLabel.RIGHT);

        panel.add(labelCat);

        JLabel labelSnake = new JLabel("This is a snake");
        Icon snakeIcon = new ImageIcon("snake1.gif");
        labelSnake.setIcon(snakeIcon);
        labelSnake.setHorizontalAlignment(JLabel.RIGHT);

        panel.add(labelSnake);

        c.add(panel);
        setSize(800,500);
        setVisible( true );

      }

    public static void main(String args [])
    {

    Lab2Part4 myFrame = new Lab2Part4();
    myFrame.setDefaultCloseOperation( 
    JFrame.EXIT_ON_CLOSE );

    }


  }//end of class

0 个答案:

没有答案