开发者

Java ImageIcon on a Applet

I'm trying to set a ImageIcon next to a JLabel but

I cannot find what import the ImageIcon is.. I tried awt but it's nogo.

I'm gettign this error:

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

Tool completed with exit code 1

Code:

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);开发者_如何学Go
    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);
  }
}


Try javax.swing.ImageIcon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜