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

📄 filesplitframe.java

📁 char22-2 文件拆分实例 提供了本书第2章的实例文件拆分实例的源程序;
💻 JAVA
字号:
/*
 * Created on 2005-3-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author yujun
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.border.*;

class FileSplitFrame extends JFrame implements ActionListener, MouseListener
{
	public FileSplitFrame()
	{
		setSize(350,250);
		//设置程序界面的大小
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置默认的关闭操作
		setTitle("File Splitter");
		//设置界面的标题
		setResizable(false);
		//以下为设置位置的过程
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		int screenHeight = d.height;
		int screenWidth = d.width;
		setLocation(screenWidth / 4, screenHeight / 4);
		
		try
		{
			String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
			UIManager.setLookAndFeel(plaf);
			//设置界面风格
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception e) 
		{} 
		
		
		
		JMenuBar mbar = new JMenuBar();
		//生成菜单
		setJMenuBar(mbar);
		
		JMenu File_Menu =  new JMenu("File");
		File_Menu.setMnemonic('F');
		FSplit =  new JMenuItem("Split",'S');
		FSplit.addActionListener(this);
		Exit = new JMenuItem("Exit",'E');
		Exit.addActionListener(this);
		//为菜单添加选项
		File_Menu.add(FSplit);
		File_Menu.addSeparator();
		File_Menu.add(Exit);
		mbar.add(File_Menu);
		
		JMenu Help = new JMenu("Help");
		File_Menu.setMnemonic('H');
		About = new JMenuItem("About",'A');
		About.addActionListener(this);
		HelpMenu = new JMenuItem("Read Me",'R');
		HelpMenu.addActionListener(this);
		Help.add(About);
		Help.add(HelpMenu);
		mbar.add(Help);
		
		Container Pane = getContentPane();
		//设置布局格式
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,2));
		//以下设置文本框和控制面板
		FileLabel = new JLabel("File",JLabel.LEFT);
		FileText = new JTextField("",50);
		FileText.addActionListener(this);
		FileText.addMouseListener(this);
		panel.add(FileLabel);
		panel.add(FileText);
		
		Pane.add(panel,"North");
		
		Border etched = BorderFactory.createEtchedBorder();
		Border title = BorderFactory.createTitledBorder(etched,"Select a File to Split");
		panel.setBorder(title);
		
		//以下的部分用来为界面添加按纽 
		  ButtonGroup group = new ButtonGroup();
		  JPanel panel1 = new JPanel();
		  
			JButton FloppyRadio = new JButton("Floppy");
			FloppyRadio.addActionListener(this);
			group.add(FloppyRadio);
			panel1.add(FloppyRadio);
			
			  JButton EMailRadio = new JButton("E-Mail");
			  EMailRadio.addActionListener(this);
			  group.add(EMailRadio);
			  panel1.add(EMailRadio);
			  
				Border title1 = BorderFactory.createTitledBorder(etched,"Split To");
				
				  
					Pane.add(panel1, "Center");
		//添加另一个面板
		JPanel panel2 = new JPanel();
		panel2.setLayout(new GridLayout(1,2));
		SplitButton = new JButton("Split File");
		SplitButton.addActionListener(this);
		ResultLabel = new JLabel("Result", JLabel.CENTER);
		ResultText = new JTextField("",8);
		panel2.add(SplitButton);
		
		panel2.add(ResultLabel);
		panel2.add(ResultText);
		
		Border title2 = BorderFactory.createTitledBorder(etched,"Output");
		panel2.setBorder(title2);
		Pane.add(panel2 , "South");
		
	} //  FileSplitFrame 的构建终结处
	
	//以下部分设置各种操作的响应事件,包括了
	//按纽操作,菜单操作
	public void actionPerformed(ActionEvent e) 
	{
		Object source = e.getSource();
		if (source == FSplit)
		{
			int returnVal = Chooser.showOpenDialog(FileSplitFrame.this);
			
			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				FileText.setText("" + Chooser.getSelectedFile().getAbsolutePath());
			}
		}
		
		if (source == About)
		{
			if(dialog==null)
				dialog = new AboutDialog(this);
			dialog.show();
		}
		
		if ( source == Exit)
		{	System.exit(0);	System.exit(0);
		}
		
		
		if (source == SplitButton)
		{
			try
			{
				FileSplitter file = new FileSplitter();
				file.Split(Chooser.getSelectedFile().getAbsolutePath());
				
				ResultText.setText("" + "Split Complete");
			} catch(Exception evt) 
			{ 
				ResultText.setText("" + "Some Error Occured");
			}
		}
		
		
		if (source == HelpMenu)
		{
			Runtime r = Runtime.getRuntime();
			Process p = null;
			try
			{
				p = r.exec("notepad readme.txt");
			} catch (Exception evt)
			{System.out.println("Error exectuing notepad");
			}
		}	
		
	}
	
	public void mouseClicked(MouseEvent e)
	{
		int returnVal = Chooser.showOpenDialog(FileSplitFrame.this);
		if (returnVal == JFileChooser.APPROVE_OPTION) 
		{
			FileText.setText("" + Chooser.getSelectedFile().getAbsolutePath());
		}
		
	}
	public void mousePressed(MouseEvent e)
	{
	}
	public void mouseEntered(MouseEvent e) 
	{
	}
	public void mouseExited(MouseEvent e) 
	{
	}
	public void mouseReleased(MouseEvent e) 
	{
	}
	
	private JLabel FileLabel;
	private JTextField FileText;
	//private JRadioButton FloppyRadio;
	//private JRadioButton EMailRadio;
	private JButton SplitButton;
	private JLabel ResultLabel;
	private JTextField ResultText;
	
	private JMenuItem FSplit;
	private JMenuItem Exit;
	
	private JMenuItem About;
	private JMenuItem HelpMenu;
	private AboutDialog dialog;
	JFileChooser Chooser = new JFileChooser();
	String filename;
} // FileSplitFrame 类结束

//以下为设置对话框,它的方法和主界面的设置类似
class AboutDialog extends JDialog
{
	public AboutDialog(JFrame parent)
	{
		super(parent,"About File Splitter",true);
		
		Box b = Box.createVerticalBox();
		b.add(Box.createGlue());
		b.add(new JLabel("Made by --"));
		b.add(new JLabel("Abhinav Gupta"));
		b.add(new JLabel("B.Tech (C.S.E.)"));
		b.add(new JLabel("National Institute of Technology Silchar"));
		b.add(new JLabel("Assam, India"));
		b.add(new JLabel("abhinav_rec@rediffmail.com"));
		b.add(Box.createGlue());
		
		Container Pane = getContentPane();
		Pane.add(b,"Center");
		
		JPanel p2 = new JPanel();
		JButton ok = new JButton("OK");
		p2.add(ok);
		Pane.add(p2,"South");
		
		ok.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{ setVisible(false); 
			}
		});
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		int screenHeight = d.height;
		int screenWidth = d.width;
		setLocation(screenWidth / 3, screenHeight / 3);
		setSize(275,175);
	}
} //Dialog 类结束

class FileSplitGui
{
	public static void main(String args[])
	{
		JFrame frame =  new FileSplitFrame();
		frame.show();
	}
}

⌨️ 快捷键说明

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