目录
前言:
关于Swing进行一些创作和样品设计,不过笔者还是更加推荐去网上寻找一些视频教程,这里直接看代码的话会比较的蒙,建议初学者以视频讲解类的内容为主。
目标:
下图是使用以下代码实现的成品结构图。
实现:
Book类:
package com.swing07;
import javax.swing.*;
public class Book {
private String name;
private Icon icon;
private String desc;
public Book(String name, Icon icon, String desc) {
this.name = name;
this.icon = icon;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", icon=" + icon +
", desc='" + desc + '\'' +
'}';
}
}
导入包:
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
组件文本准备:
创建相关的容器主键,这里文本中有了一定的注释,具体还有那里不懂可以私聊笔者进行询问。
//创建图书对象
Book[] books = {new Book("美女照片1",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F101920105F8%2F201019105F8-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661781812&t=ed43f911811639db14fde4aad7413a89"),"美女1"),
new Book("美女照片2",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.phbzj.com%2Fxiaohua%2F0906%2F090609386324.jpg&refer=http%3A%2F%2Fimg.phbzj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661782125&t=08aba00d018f1796c0f82cd6a3126cfb"),"真的是美女"),
new Book("美女照片3",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.phbzj.com%2Fxiaohua%2F0906%2F090609115291.jpg&refer=http%3A%2F%2Fimg.phbzj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661782298&t=27916ad1322e3f6efd058472f21d45bc"),"超级美女")
};
//创建窗口对象
JFrame jf = new JFrame("测试JSplitPane");
//声明程序中用到的组件
JList<Book> bookJList = new JList<>(books);
JLabel bookCover = new JLabel();
JTextArea bookDesc = new JTextArea();
注意注意!!!这里有个小bug,照片必须为本地照片,笔者这里用的是网页图片链接,所以无法打开图片,要打开图片需要使用已经下载到电脑上的图片。
初始化界面:
初始化界面也就是对组件进行一定的封装,将小的组件封装到容器中并实现。
//组装视图
//设定组件大小
bookJList.setPreferredSize(new Dimension(150,400));
bookCover.setPreferredSize(new Dimension(220,270));
bookDesc.setPreferredSize(new Dimension(220,130));
//为jList设置条目选中监听器
bookJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//获取当前选中的是那个条目
Book book = bookJList.getSelectedValue();
//把书籍的图书用bookCover展示
bookCover.setIcon(book.getIcon());
System.out.println(bookCover);
//把书籍的描述用bookDesc展示
bookDesc.setText(book.getDesc());
}
});
//组装左边区域
JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT,bookCover,new JScrollPane(bookDesc));
left.setOneTouchExpandable(true);
//组装整体
JSplitPane hole = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,bookJList);
//支持连续布局
hole.setContinuousLayout(true);
hole.setDividerSize(10);
jf.add(hole);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
总代码:
接下来就是完整的代码实现了
package com.swing07;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class Demo07 {
//创建图书对象
Book[] books = {new Book("美女照片1",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F101920105F8%2F201019105F8-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661781812&t=ed43f911811639db14fde4aad7413a89"),"美女1"),
new Book("美女照片2",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.phbzj.com%2Fxiaohua%2F0906%2F090609386324.jpg&refer=http%3A%2F%2Fimg.phbzj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661782125&t=08aba00d018f1796c0f82cd6a3126cfb"),"真的是美女"),
new Book("美女照片3",new ImageIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.phbzj.com%2Fxiaohua%2F0906%2F090609115291.jpg&refer=http%3A%2F%2Fimg.phbzj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661782298&t=27916ad1322e3f6efd058472f21d45bc"),"超级美女")
};
//创建窗口对象
JFrame jf = new JFrame("测试JSplitPane");
//声明程序中用到的组件
JList<Book> bookJList = new JList<>(books);
JLabel bookCover = new JLabel();
JTextArea bookDesc = new JTextArea();
public void init(){
//组装视图
//设定组件大小
bookJList.setPreferredSize(new Dimension(150,400));
bookCover.setPreferredSize(new Dimension(220,270));
bookDesc.setPreferredSize(new Dimension(220,130));
//为jList设置条目选中监听器
bookJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//获取当前选中的是那个条目
Book book = bookJList.getSelectedValue();
//把书籍的图书用bookCover展示
bookCover.setIcon(book.getIcon());
System.out.println(bookCover);
//把书籍的描述用bookDesc展示
bookDesc.setText(book.getDesc());
}
});
//组装左边区域
JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT,bookCover,new JScrollPane(bookDesc));
left.setOneTouchExpandable(true);
//组装整体
JSplitPane hole = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,bookJList);
//支持连续布局
hole.setContinuousLayout(true);
hole.setDividerSize(10);
jf.add(hole);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo07().init();
}
}