java 将图像导入标签,标签不显示图片
importjava.awt.*;importjavax.swing.*;//为什么不能导入图像publicclassLabelDemo{publicstaticvoid...
import java.awt.*;
import javax.swing.*;
//为什么不能导入图像
public class LabelDemo
{
public static void main (String [] args)
{
ImageIcon icon=new ImageIcon ("080221025482169.gif");
JFrame frame=new JFrame ("LabelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1,label2,label3;
label1=new JLabel ("left",icon,SwingConstants.CENTER);
label2=new JLabel ("right",icon,SwingConstants.CENTER);
label2.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.BOTTOM);
label3=new JLabel ("above",icon,SwingConstants.CENTER);
label3.setHorizontalTextPosition(SwingConstants.CENTER);
label3.setVerticalTextPosition(SwingConstants.BOTTOM);
JPanel panel=new JPanel();
panel.setBackground(Color.cyan);
panel.setPreferredSize(new Dimension(200,250));
panel.add(label1);
panel.add(label2);
panel.add(label3);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
} 展开
import javax.swing.*;
//为什么不能导入图像
public class LabelDemo
{
public static void main (String [] args)
{
ImageIcon icon=new ImageIcon ("080221025482169.gif");
JFrame frame=new JFrame ("LabelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1,label2,label3;
label1=new JLabel ("left",icon,SwingConstants.CENTER);
label2=new JLabel ("right",icon,SwingConstants.CENTER);
label2.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.BOTTOM);
label3=new JLabel ("above",icon,SwingConstants.CENTER);
label3.setHorizontalTextPosition(SwingConstants.CENTER);
label3.setVerticalTextPosition(SwingConstants.BOTTOM);
JPanel panel=new JPanel();
panel.setBackground(Color.cyan);
panel.setPreferredSize(new Dimension(200,250));
panel.add(label1);
panel.add(label2);
panel.add(label3);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询