Swing-文件显示切换

设计思路:

定义5个类

1、

package my;
//main 程序入口 
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指一个窗口,构造方法的参数为窗口标题
		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、

package my;
//自定义MyFrame:实现菜单切换功能
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;



public class MyFrame extends JFrame
{
	DefaultListModel<FileListItem>listModel=new DefaultListModel<>();
	JList<FileListItem>nameList=new JList<>();
	
	//iconMode记录当前的显示模式
	boolean iconMode=true;
	
	//右键菜单处理器
	MenuActionListener menuActionListener=new MenuActionListener();
	public MyFrame(String title)
	{
		super(title);
		
		// Content Pane
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new BorderLayout());
		
		nameList.setModel(listModel);
		nameList.setVisibleRowCount(-1);
		nameList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		nameList.setCellRenderer(new FileIconCellRenderer());
		nameList.addMouseListener(new FileMouseListener());
		JScrollPane listScrollPane = new JScrollPane(nameList);
		
		//添加到主界面
		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);
				listModel.addElement(item);
			}
		}		
	}
	private void showContextMenu(MouseEvent e)
	{
		int index=nameList.locationToIndex(e.getPoint());
		if(index>=0)
			nameList.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
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			if(e.getButton() == MouseEvent.BUTTON3)
			{
				showContextMenu(e);
			}
		}		
	}	
	
	private class MenuActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			String action = e.getActionCommand();
			if(action.equals("listMode"))
			{
				iconMode = false;
				nameList.setLayoutOrientation(JList.VERTICAL);
				nameList.setCellRenderer(new FileListCellRenderer());
			}
			else if(action.equals("iconMode"))
			{
				iconMode = true;
				nameList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
				nameList.setCellRenderer(new FileIconCellRenderer());
			}
		}		
	}
	
	
}

 

 3、

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());
	}
}
package my;
//大图标显示
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

import js.swing.JsColumnLayout;
import js.swing.JsImageView;
import js.swing.JsPanel;

public class FileIconCellRenderer extends JsPanel implements ListCellRenderer
{
	JsImageView thumbField=new JsImageView();
	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));
		
		// 去掉粗体显示(基于原有字体,修改一个style, deriveFont()是在原有基础上创建新Font的意思)
		Font textFont=nameField.getFont().deriveFont(Font.PLAIN);
		nameField.setFont(textFont);
		nameField.setHorizontalAlignment(SwingConstants.CENTER);
	}

	@Override
	public Component getListCellRendererComponent(JList list, 
			Object value, int index, boolean isSelected,
			boolean cellHasFocus) {
		// TODO Auto-generated method stub
		FileListItem item=(FileListItem)value;
		
		//图标列
		if(item.isDir)
			thumbField.setImage(icFolder);
		else
			thumbField.setImage(icFile);
		
		//名称和修改时间
		nameField.setText(item.name);
		
		this.setOpaque(true);
		
	    if (isSelected) {
	    	this.setBackground(list.getSelectionBackground());
	    	this.setForeground(list.getSelectionForeground());
	    	thumbField.setBgColor(list.getSelectionBackground());
        } else {
        	this.setBackground(list.getBackground());
        	this.setForeground(list.getForeground());
        	thumbField.setBgColor( list.getBackground());
        }
	    
	    return this;
		
	}
	
}
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
{
	JLabel iconField=new JLabel();
	JLabel nameField=new JLabel();
	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");
		
		//先加载好图标
		icFolder = new ImageIcon(getClass().getResource("/icon/ic_folder.png"));
		icFile = new ImageIcon(getClass().getResource("/icon/ic_file.png"));
		
		// 去掉粗体显示(基于原有字体,修改一个style, deriveFont()是在原有基础上创建新Font的意思)
		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);
		
		this.setOpaque(true);
		
	    if (isSelected) {
	    	this.setBackground(list.getSelectionBackground());
	    	this.setForeground(list.getSelectionForeground());
        } else {
        	this.setBackground(list.getBackground());
        	this.setForeground(list.getForeground());
        }

		return this;
	}
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值