Swing-文件显示切换
发布日期:2021-05-14 04:14:17 浏览次数:19 分类:精选文章

本文共 8660 字,大约阅读时间需要 28 分钟。

设计思路

1. 包名定义与窗口创建

我将使用Java Swing框架来构建一个文件浏览器应用程序。首先,定义包名并创建主窗口。通过import java.awt.Container;import java.awt.FlowLayout;等必要的包来实现布局。在MyDemo类中,创建一个带标题的窗口,并设置其默认关闭行为为退出程序。

package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyDemo {
private static void createGUI() {
JFrame frame = new MyFrame("目录列表");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
}

2. 文件浏览器实现

接下来,定义MyFrame类,继承自JFrame。在构造函数中,创建一个JPanel作为内容面板,并设置适当布局。通过DefaultListModelJList来展示文件目录。为了实现右键菜单功能,使用了MouseAdapterJPopupMenu

package my;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
public class MyFrame extends JFrame {
public MyFrame(String title) {
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
JList list = new JList();
list.setModel(new DefaultListModel());
list.setVisibleRowCount(-1);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setCellRenderer(new FileIconCellRenderer());
list.addMouseListener(new FileMouseListener());
JScrollPane listScrollPane = new JScrollPane(list);
root.add(listScrollPane, BorderLayout.CENTER);
loadDir(new File("c:/"));
}
private void loadDir(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
FileListItem item = new FileListItem(f);
((DefaultListModel) list.getModel()).addElement(item);
list.revalidate();
}
}
}
private void showContextMenu(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {
list.setSelectedIndex(index);
}
JPopupMenu menu = new JPopupMenu();
JMenuItem listModeCmd = new JCheckBoxMenuItem("列表模式");
listModeCmd.setActionCommand("listMode");
listModeCmd.setSelected(!iconMode);
listModeCmd.addActionListener(menuActionListener);
menu.add(listModeCmd);
JMenuItem iconModeCmd = new JCheckBoxMenuItem("图标模式");
iconModeCmd.setActionCommand("iconMode");
iconModeCmd.setSelected(iconMode);
iconModeCmd.addActionListener(menuActionListener);
menu.add(iconModeCmd);
menu.show(e.getComponent(), e.getX(), e.getY());
}
private class FileMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTONvouit) {
showContextMenu(e);
}
}
}
private class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("listMode")) {
iconMode = false;
list.setLayoutOrientation(JList.VERTICAL);
list.setCellRenderer(new FileListCellRenderer());
} else if (action.equals("iconMode")) {
iconMode = true;
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setCellRenderer(new FileIconCellRenderer());
}
}
}
private boolean iconMode = true;
}

3. 文件属性定义

定义FileListItem类,用于存储文件的基本属性。通过File类获取文件信息,并设置日期格式来显示最后修改时间。

package my;
import java.io.File;
import java.text.SimpleDateFormat;
public class FileListItem {
public String name;
public File file;
public boolean isDir;
public String lastModified;
public FileListItem(File f) {
this.file = f;
this.name = f.getName();
this.isDir = f.isDirectory();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.lastModified = sdf.format(f.lastModified());
}
}

4. 文件图标显示

自定义FileIconCellRenderer类来实现文件或目录的图标显示。通过JsImageViewJLabel布局来展示图标和文件名。

package my;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import js.swing.JsColumnLayout;
import js.swing.JsImageView;
import js.swing.JsPanel;
import js.swing.JsRowLayout;
public class FileIconCellRenderer extends JsPanel implements ListCellRenderer {
private JsImageView thumbField = new JsImageView();
private JLabel nameField = new JLabel();
private Image icFolder, icFile;
public FileIconCellRenderer() {
this.setLayout(new JsColumnLayout(10));
this.padding(10);
this.perferredSize(80, 110);
this.add(thumbField, "1w");
this.add(nameField, "30px");
try {
icFolder = ImageIO.read(getClass().getResource("/icon/ic_folder_64.png"));
icFile = ImageIO.read(getClass().getResource("/icon/ic_file_64.png"));
} catch (IOException e) {
e.printStackTrace();
}
thumbField.setBgColor(new Color(255, 255, 255, 0));
Font textFont = nameField.getFont().deriveFont(Font.PLAIN);
nameField.setFont(textFont);
nameField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
FileListItem item = (FileListItem) value;
if (item.isDir()) {
thumbField.setImage(icFolder);
} else {
thumbField.setImage(icFile);
}
nameField.setText(item.name);
if (isSelected) {
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
}
}

5. 列表显示

同样地,定义FileListCellRenderer类来实现列表显示,展示图标、文件名和最后修改时间。

package my;
import java.awt.Component;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import js.swing.JsPanel;
import js.swing.JsRowLayout;
public class FileListCellRenderer extends JsPanel implements ListCellRenderer {
private JLabel iconField = new JLabel();
private JLabel nameField = new JLabel();
private JLabel timeField = new JLabel();
private ImageIcon icFolder, icFile;
public FileListCellRenderer() {
this.setLayout(new JsRowLayout());
this.padding(2);
this.perferredHeight(30);
this.add(iconField, "20px");
this.add(nameField, "1w");
this.add(timeField, "150px");
try {
icFolder = new ImageIcon(getClass().getResource("/icon/ic_folder.png"));
icFile = new ImageIcon(getClass().getResource("/icon/ic_file.png"));
} catch (IOException e) {
e.printStackTrace();
}
Font textFont = timeField.getFont().deriveFont(Font.PLAIN);
nameField.setFont(textFont);
timeField.setFont(textFont);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
FileListItem item = (FileListItem) value;
if (item.isDir()) {
iconField.setIcon(icFolder);
} else {
iconField.setIcon(icFile);
}
nameField.setText(item.name);
timeField.setText(item.lastModified);
if (isSelected) {
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
}
}
上一篇:Swing-JTable(表格控件)1/9
下一篇:Swing-自定义对话框/创建对话框

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月29日 06时28分55秒