如果你有$并且要求功能比较完善的话,可以看看这个
http://www.zfqjava.com/products.html 其中的JListView从截图来看好像还不错,接近Windows的ListView控件的功能
http://www.zfqjava.com/images/listview-snapshot01.gif
http://www.zfqjava.com/images/listview-snapshot02.gif
http://www.zfqjava.com/images/listview-snapshot03.gif如果没有$或者要求不高的话,可以看看这个简单的例子,然后自己改一改import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;import javax.swing.*;
import javax.swing.border.Border;public class JListView { public static void main(String[] args) {
DefaultListModel model = new DefaultListModel();
for (int i = 0; i < 50; i++) {
model.addElement("Item-" + i);
}
JList list = new JList(model);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
list.setCellRenderer(new ListItemRenderer());
JScrollPane sp = new JScrollPane(list);
JFrame frame = new JFrame("Poor Man's ListView");
frame.getContentPane().add(sp, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static class ListItemRenderer extends DefaultListCellRenderer {
Border focusBorder =
BorderFactory.createCompoundBorder(
UIManager.getBorder("List.focusCellHighlightBorder"),
BorderFactory.createEmptyBorder(8,8,8,8)); Border noFocusBorder =
BorderFactory.createCompoundBorder(
super.noFocusBorder,
BorderFactory.createEmptyBorder(8,8,8,8)); public ListItemRenderer() {
setHorizontalAlignment(CENTER);
setVerticalTextPosition(BOTTOM);
setHorizontalTextPosition(CENTER);
} public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) { super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus); setIcon(TestIcon.Instance);
setBorder((cellHasFocus) ? focusBorder : noFocusBorder);
return this;
} } private static class TestIcon implements Icon {
private static Icon Instance = new TestIcon(); public int getIconHeight() {
return 30;
} public int getIconWidth() {
return 30;
} public void paintIcon(Component c, Graphics g, int x, int y) {
g.translate(x, y);
g.setColor(Color.lightGray);
g.fillRect(0, 0, getIconWidth(), getIconHeight());
g.setColor(Color.darkGray);
g.drawRect(0, 0, getIconWidth(), getIconHeight());
g.translate(-x, -y);
}
}
}
http://www.zfqjava.com/products.html 其中的JListView从截图来看好像还不错,接近Windows的ListView控件的功能
http://www.zfqjava.com/images/listview-snapshot01.gif
http://www.zfqjava.com/images/listview-snapshot02.gif
http://www.zfqjava.com/images/listview-snapshot03.gif如果没有$或者要求不高的话,可以看看这个简单的例子,然后自己改一改import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;import javax.swing.*;
import javax.swing.border.Border;public class JListView { public static void main(String[] args) {
DefaultListModel model = new DefaultListModel();
for (int i = 0; i < 50; i++) {
model.addElement("Item-" + i);
}
JList list = new JList(model);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
list.setCellRenderer(new ListItemRenderer());
JScrollPane sp = new JScrollPane(list);
JFrame frame = new JFrame("Poor Man's ListView");
frame.getContentPane().add(sp, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static class ListItemRenderer extends DefaultListCellRenderer {
Border focusBorder =
BorderFactory.createCompoundBorder(
UIManager.getBorder("List.focusCellHighlightBorder"),
BorderFactory.createEmptyBorder(8,8,8,8)); Border noFocusBorder =
BorderFactory.createCompoundBorder(
super.noFocusBorder,
BorderFactory.createEmptyBorder(8,8,8,8)); public ListItemRenderer() {
setHorizontalAlignment(CENTER);
setVerticalTextPosition(BOTTOM);
setHorizontalTextPosition(CENTER);
} public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) { super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus); setIcon(TestIcon.Instance);
setBorder((cellHasFocus) ? focusBorder : noFocusBorder);
return this;
} } private static class TestIcon implements Icon {
private static Icon Instance = new TestIcon(); public int getIconHeight() {
return 30;
} public int getIconWidth() {
return 30;
} public void paintIcon(Component c, Graphics g, int x, int y) {
g.translate(x, y);
g.setColor(Color.lightGray);
g.fillRect(0, 0, getIconWidth(), getIconHeight());
g.setColor(Color.darkGray);
g.drawRect(0, 0, getIconWidth(), getIconHeight());
g.translate(-x, -y);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货