关于label.setIcon(new ImageIcon("xxx.jpg"));无法显示问题
来源:互联网 发布:mac os 10.13.1 重装 编辑:程序博客网 时间:2024/06/01 10:20
运行课本实例发现的问题如题。
代码:
package ksxt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class ClientWindow extends JFrame{
ClientLogin clientLogin;
GetTestFile getTestFile;
ClientTestArea clientTestArea;
JLabel label=null;
JTabbedPane tabbedPane;
public ClientWindow(){
setTitle("标准化考试系统(客户端)");
label=new JLabel();
label.setText(getTitle());
label.setForeground(Color.orange);
label.setFont(new Font("隶书",Font.BOLD,22));
label.setIcon(new ImageIcon("welcome.jpg"));
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setBackground(Color.green);
tabbedPane=new JTabbedPane(JTabbedPane.LEFT);
clientLogin=new ClientLogin();
getTestFile=new GetTestFile();
clientTestArea=new ClientTestArea();
tabbedPane.add("系统标题",label);
tabbedPane.add("登录",clientLogin);
tabbedPane.add("选择试卷",getTestFile);
tabbedPane.add("答卷",clientTestArea);
add(tabbedPane,BorderLayout.CENTER);
validate();
setVisible(true);
setBounds(100,50,460,280);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int n=JOptionPane.showConfirmDialog(null,"确认退出吗?","确认对话框",
JOptionPane.YES_NO_OPTION );
if(n==JOptionPane.YES_OPTION)
System.exit(0);
}});
validate();
}
public static void main(String args[]){
new ClientWindow();
}
}
自然认为welcome.jpg文件只要与当前类文件在同一目录下即可,其实所谓相对路径的起点是工程的根目录
如图所示,此时运行ksxt应用程序,welcome.jpg在此ksxt目录下是无法显示的。Test2017java才是相对路径的起点,故修改方式可以是:
①在Test2017java文件夹下copy一个welcome.jpg文件
②将label.setIcon(new ImageIcon("welcome.jpg"));改为
label.setIcon(new ImageIcon("src/ksxt/welcome.jpg"));
或者是label.setIcon(new ImageIcon("./src/ksxt/welcome.jpg"));
参考资料:http://www.doc88.com/p-5015902363089.html
- 关于label.setIcon(new ImageIcon("xxx.jpg"));无法显示问题
- Java ImageIcon 不能显示问题
- new ImageIcon(String filename)的路径问题
- java在JFrame里 this.setIconImage((new ImageIcon("image\\qq.jpg")).getImage());
- JAVA中如何解决 new ImageIcon()的路径问题
- 关于IList无法new的问题
- 网页无法显示jpg图片
- 关于无法显示该网页,弹出对话框显示"Internet Explorer无法打开Internet站点xxx,已终止操作"问题的解决方法
- 关于label显示另一个线程发来的图片的问题
- ImageIcon显示不出来.
- SetIcon
- SetIcon
- 关于Hbuilder打包Bug—plus.maps.Marker setIcon不显示解决方法
- ImageIcon缓存问题
- java ImageIcon路径问题
- Spring3无法显示css/js/jpg/gif
- ie浏览器无法显示jpg的图片
- chtmlview 使用内嵌资源比如JPG 无法正常显示的问题
- 键盘输入任意个数,负数结尾求和!
- HDU 1879 继续畅通工程
- Linux note0x00
- 1!+2!+。。。+m!<n,求m的值
- 基于Docker的微服务架构-企业级服务集群实践(二)
- 关于label.setIcon(new ImageIcon("xxx.jpg"));无法显示问题
- 求一个整数的因数
- mac上: mysql Access denied for user root@localhost
- 被自己菜哭-阅读理解不及格系列
- 浅谈Spring设计模式
- Linux下压缩的压缩命令
- WebService应用
- 第一年年初应该存多少
- Bailian3709 2进制转化为3进制【进制】