⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 1.文件和目录管理
💻 JAVA
字号:
/**
 * Main frame of the program
 */
package file;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class MainFrame extends JFrame {
	
	private FileOpener opener;
	private FileCopier copier;
	private FileAnalyser analyser;
	private Container c;
	private JLabel fileMessage;
	private JTextField fileNameIn;
	private JTextArea ta;
	private JList list;
	private JScrollPane scroller;
	private JButton ok,open,copy,stastic,search;
	private JTextField searchInput;
	private JLabel currentFileName;
	private JPanel centerPane;
	private JPanel bottomPane;
	private String parent;
	
	public MainFrame() {
		
		super("A little program to do with file");
		init();
	}
	
	public void init() {

		c = this.getContentPane();
		opener = new FileOpener();
		copier = new FileCopier();
		analyser = new FileAnalyser();
		JLabel label = new JLabel("File Name");
		fileNameIn = new JTextField("E:\\",15);
		JLabel fileInfo = new JLabel("File Information",JLabel.CENTER);
		fileMessage = new JLabel();
		ta = new JTextArea(10,15);
		open = new JButton("Open");
		copy = new JButton("Copy");
		stastic = new JButton("Stastic");
		search = new JButton("Search");
		searchInput = new JTextField(15);
		currentFileName = new JLabel("Current file",JLabel.LEFT);
		list = new JList();
		scroller = new JScrollPane(list);
		
		JPanel topPane = new JPanel();
		topPane.add(label);
		topPane.add(fileNameIn);
		JPanel leftPane = new JPanel();
		leftPane.setLayout(new BorderLayout());
		leftPane.add(fileInfo,BorderLayout.NORTH);
		leftPane.add(ta,BorderLayout.CENTER);
		JPanel rightPane = new JPanel();
		rightPane.setLayout(new GridLayout(3,1));
		rightPane.add(open);
		rightPane.add(copy);
		rightPane.add(stastic);
		bottomPane = new JPanel();
		bottomPane.add(currentFileName);
		bottomPane.add(searchInput);
		bottomPane.add(search);
		bottomPane.setVisible(false);
		centerPane = new JPanel();
		centerPane.setLayout(new BorderLayout());
		centerPane.add(fileMessage,BorderLayout.NORTH);
		centerPane.add(scroller,BorderLayout.CENTER);
		c.add(topPane,BorderLayout.NORTH);
		c.add(leftPane,BorderLayout.WEST);
		c.add(centerPane,BorderLayout.CENTER);
		c.add(rightPane,BorderLayout.EAST);
		c.add(bottomPane,BorderLayout.SOUTH);
		
		open.addActionListener(
			new ActionListener() {
				
				public void actionPerformed(ActionEvent e) {
					parent = fileNameIn.getText();
					String info = new String(opener.getInfo(parent));
					System.out.println(info);
					ta.setText(info);
					ta.setEditable(false);
					fileMessage.setText("file list");
					if(scroller != null)
						centerPane.remove(scroller);
					list = new JList(opener.getFileList());	
					list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					list.addListSelectionListener(
						new ListSelectionListener() {
				
							public void valueChanged(ListSelectionEvent e) {
					
								String s = (String)
									((JList)e.getSource()).getSelectedValue();
								if(parent.charAt(parent.length() - 1) != '\\')
									fileNameIn.setText(parent + "\\" + s);
								else fileNameIn.setText(parent + s);
							}
					});
    				scroller = new JScrollPane(list);				
    				centerPane.add(scroller,BorderLayout.CENTER);
    				
				}
			});
		copy.addActionListener(
			new ActionListener() {
			
				public void actionPerformed(ActionEvent e) {
				
					String s = ""; 
					s = JOptionPane.showInputDialog(null, "To where",
					"Destination", JOptionPane.INFORMATION_MESSAGE);
					if(s != null)
						copier.copy(fileNameIn.getText(),s);	
				}
			});
		stastic.addActionListener(
			new ActionListener() {
			
				public void actionPerformed(ActionEvent e) {
				
					analyser.stastic(fileNameIn.getText());
					ta.setEditable(true);
					String s = "Total Words:" + analyser.getTotalWords()
							+ "\nTotal Numbers: " + analyser.getTotalNumbers() 
							+ "\nTotal Interpunctions: " 
							+ analyser.getTotalInterpunctions();
					ta.setText(s);
					ta.setEditable(false);
					fileMessage.setText("Tokens in file " + fileNameIn.getText());
					if(scroller != null)
						centerPane.remove(scroller);
					list = new JList(analyser.getContent());
					list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					list.addListSelectionListener(
						new ListSelectionListener() {
				
							public void valueChanged(ListSelectionEvent e) {
					
								String s = (String)((JList)e.getSource()).getSelectedValue();
								searchInput.setText(s);
							}
					});	
    				scroller = new JScrollPane(list);
    				centerPane.add(scroller,BorderLayout.CENTER);
    				currentFileName.setText(fileNameIn.getText());
    				bottomPane.setVisible(true);
				}
			});
			
		search.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					int times = analyser.frequency(searchInput.getText());
					JOptionPane.showMessageDialog(null,"Words  " +
						searchInput.getText() + "  appears  " + times 
						+ "  times !");
				}
				
			});
	}
	

	public static void main(String[] args) {
		
		MainFrame mf = new MainFrame();
		mf.init();
		mf.setSize(600,350);
		mf.setDefaultCloseOperation(mf.EXIT_ON_CLOSE);
		mf.setLocation(300,100);
		mf.setVisible(true);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -