比如我的图片.在src目录下的tupian目录里
那么路径的写法是
new ImageIcon("src/tupian/首页.jpg")或者
new ImageIcon(this.getClass().getResource("/tupian/首页.jpg"))
或者
new ImageIcon("D:\\workspace\\JavaDemo\\src\\tupian\\首页.jpg")
效果图
参考代码
import javax.swing.*import javax.swing.border.*
import javax.swing.event.*
/*
* JSplitPane 可拆分窗口
* JSplitPane.setOneTouchExpandable(true)设置是否可伸缩
*
* */
public class P10_JspltPance extends JFrame implements ListSelectionListener{
private static final long serialVersionUID = 1L
JPanel jp1, jp2
JList<String> jlist
JSplitPane jsp
JScrollPane jspane
JLabel jl
String[] ly = { "首页","长白山", "峨眉山", "华山"}
String[] path= {"/tupian/首页.jpg","/tupian/长白山.jpg","/tupian/峨眉山.jpg","/tupian/华山.jpg"}
public static void main(String[] args) {
new P10_JspltPance()
}
public P10_JspltPance() {
jl = new JLabel(new ImageIcon(this.getClass().getResource(path[0])))
jlist = new JList<String>(ly)
jlist.setVisibleRowCount(5)
jlist.addListSelectionListener(this)
jspane = new JScrollPane(jlist)
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jspane, jl)
jsp.setEnabled(false)
// 是否可伸缩
jsp.setOneTouchExpandable(true)
jl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "页面展示", TitledBorder.TOP,
TitledBorder.RIGHT))
// 设置窗口属性布局管理
JPanel jp = new JPanel()
jp.add(jsp)
add(jp)
this.setTitle("图片浏览器")
this.setSize(630, 500)
this.setLocationRelativeTo(null)//窗口居中
this.setVisible(true)//窗口可见
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
}
@Override
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting()) {
jl.setIcon(new ImageIcon(this.getClass().getResource(path[jlist.getSelectedIndex()])))
}
}
}
public class demo_9 extends JFrame {JSplitPane jsp = null
JList jlist
JLabel jlabel
public static void main(String[] args) {
demo_9 a = new demo_9()
}
public demo_9(){
String []words ={"boy","girl"}
JList jlist = new JList(words)
jlabel = new JLabel(new ImageIcon("Image//真三.gif")) //这里就是引入图片了
//拆分窗格
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jlist,jlabel)
jsp.setDividerLocation(70)
//设置可以伸缩
jsp.setOneTouchExpandable(true)
this.add(jsp)
this.setTitle("test")
this.setSize(400,300)
this.setLocation(400,200)
this.setVisible(true)
}
}
步骤:首先先在project里新建个文件夹(Folder),然后把你要插入的图片复制黏贴到这个文件夹里面。
例如我那个引入的图片代码:jlabel = new JLabel(new ImageIcon("Image//真三.gif"))
我new一个folder叫Image,图片名称叫"真三.gif"
p4=new JPanel(new BorderLayout())p2=new JPanel(new CardLayout())
//处理p2,p3,p4
p2_lab1=new JLabel(new ImageIcon("image/21.jpg"))
p2_lab2=new JLabel(new ImageIcon("image/22.jpg"))
//把以上俩加入到P2中
p2.add(p2_lab1,"0")
p2.add(p2_lab2,"1")
this.cardp3=new CardLayout()
p3=new JPanel(this.cardp3)
//先给P3加入一个主界面的卡片
Image zhu_image=null
try {
zhu_image = ImageIO.read(new File("image/11.jpg"))
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
ImagePanel ip=new ImagePanel(zhu_image)
p3.add(ip,"0")
//对P3添加几个JLabel
JLabel rs=new JLabel(new ImageIcon("image/renshi.jpg"))
p3.add(rs,"1")
JLabel drgl=new JLabel(new ImageIcon("image/dengru.jpg"))
p3.add(drgl,"2")
//把p2 p3装入P4中
p4.add(p2,"West")
p4.add(p3,"Center")
//做一个拆分窗口分别存放P1 P4
jsp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,p1,p4)
//指定左边的面板占多大像素
jsp1.setDividerLocation(120)
//把边界线设为没有
jsp1.setDividerSize(0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)