Applet 上的 Java ImageIcon

标签 java

我正在尝试在 JLabel 旁边设置一个 ImageIcon,但是

我找不到 ImageIcon 的导入内容。我尝试了 awt,但不行。

我遇到了这个错误:

C:\Users\Dan\Documents\MainClass.java:23: cannot find symbol
symbol  : class ImageIcon
location: class MainClass
    ImageIcon warnIcon = new ImageIcon("group.png");
    ^
C:\Users\Dan\Documents\MainClass.java:23: cannot find symbol
symbol  : class ImageIcon
location: class MainClass
    ImageIcon warnIcon = new ImageIcon("group.png");
                             ^
2 errors

工具已完成,退出代码为 1

代码:

import java.awt.GridLayout;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.plaf.metal.MetalIconFactory;

import java.util.*;
import java.awt.*;

public class MainClass {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Label Text Pos");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));

    Border border = LineBorder.createGrayLineBorder();
    //Icon warnIcon = ImageIO.read(getClass().getResource("group.png"));
    ImageIcon warnIcon = new ImageIcon("group.png");
    //Image warnIcon = ImageIO.read(getClass().getResource("group.png"));

    JLabel label1 = new JLabel(warnIcon);
    label1.setText("Left-Bottom");
    label1.setHorizontalTextPosition(JLabel.LEFT);
    label1.setVerticalTextPosition(JLabel.BOTTOM);
    label1.setBorder(border);
    frame.add(label1);

    JLabel label2 = new JLabel(warnIcon);
    label2.setText("Right-TOP");
    label2.setHorizontalTextPosition(JLabel.RIGHT);
    label2.setVerticalTextPosition(JLabel.TOP);
    label2.setBorder(border);
    frame.add(label2);

    JLabel label3 = new JLabel(warnIcon);
    label3.setText("Center-Center");
    label3.setHorizontalTextPosition(JLabel.CENTER);
    label3.setVerticalTextPosition(JLabel.CENTER);
    label3.setBorder(border);
    frame.add(label3);

    JLabel label4 = new JLabel(warnIcon);
    label4.setText("Center-Bottom");
    label4.setHorizontalTextPosition(JLabel.CENTER);
    label4.setVerticalTextPosition(JLabel.BOTTOM);
    label4.setBorder(border);
    frame.add(label4);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

最佳答案

关于Applet 上的 Java ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518229/

相关文章:

java - 从 JUnit 测试时间中排除 @Before 方法持续时间

java - android中的解密 key 等 secret 数据在哪里存储,这样黑客就永远找不到

java - 如何更改 codenameone 中组件的大小(宽度和高度)

java - NoClassDefFoundError : DataAccessException using spring

java - 为什么 Spring 会尝试使用属性变量引用字符串而不是值?

使用 Kryo 序列化对象时出现 java.lang.StackOverflowError

java - 迷宫算法的数组未存储正确的值

java - 对迄今为止的集合进行排序

java - MIDP 应用程序,被 Connector.Open(..) 阻止

java - 不区分大小写检查 HttpServletRequest 中的角色