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

📄 framefilesplitter.java

📁 图形界面swing 实现文件分割
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelSplitterLayout.setVerticalGroup(
            jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSplitterLayout.createSequentialGroup()
                .addGroup(jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSplitterSource)
                    .addComponent(jButtonSplitterSourceBrowse)
                    .addComponent(jTextFieldSplitterSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSplitterTarget)
                    .addComponent(jButtonSplitterTargetBrowse)
                    .addComponent(jTextFieldSplitterTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonSize)
                    .addComponent(jLabelSplitterWay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonNumber)
                    .addComponent(jTextFieldNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelSplitterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSplitter)
                    .addComponent(jButtonSplitterCancel))
                .addGap(29, 29, 29))
        );
        jPanelCenter.add(jPanelSplitter, "cardSplitter");

        jPanelUnion.setBorder(javax.swing.BorderFactory.createTitledBorder("\u6587\u4ef6\u5408\u5e76"));
        jLabelUnionSource.setText("\u539f\u59cb\u6587\u4ef6\uff1a");

        jButtonAdd.setText("\u6dfb\u52a0");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonUnionBrowse.setText("\u6d4f\u89c8");
        jButtonUnionBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUnionBrowseActionPerformed(evt);
            }
        });

        jTextFieldUnionSource.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldUnionSourceKeyPressed(evt);
            }
        });

        jLabelUnionTarget.setText("\u4fdd\u5b58\u4f4d\u7f6e\uff1a");

        jButtonUnion.setText("\u5408\u5e76");
        jButtonUnion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUnionActionPerformed(evt);
            }
        });

        jButtonUnionCancel.setText("\u53d6\u6d88");
        jButtonUnionCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUnionCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelUnionLayout = new javax.swing.GroupLayout(jPanelUnion);
        jPanelUnion.setLayout(jPanelUnionLayout);
        jPanelUnionLayout.setHorizontalGroup(
            jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUnionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelUnionTarget)
                    .addComponent(jLabelUnionSource))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelUnionLayout.createSequentialGroup()
                        .addComponent(jButtonUnion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                        .addComponent(jButtonUnionCancel))
                    .addComponent(jTextFieldUnionTarget, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addComponent(jTextFieldUnionSource, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonUnionBrowse)
                    .addComponent(jButtonAdd))
                .addContainerGap())
        );
        jPanelUnionLayout.setVerticalGroup(
            jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUnionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelUnionSource)
                    .addComponent(jTextFieldUnionSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAdd))
                .addGap(21, 21, 21)
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelUnionTarget)
                    .addComponent(jTextFieldUnionTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonUnionBrowse))
                .addGap(28, 28, 28)
                .addGroup(jPanelUnionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonUnionCancel)
                    .addComponent(jButtonUnion))
                .addContainerGap())
        );
        jPanelCenter.add(jPanelUnion, "cardUnion");

        getContentPane().add(jPanelCenter, java.awt.BorderLayout.CENTER);

        jTextAreaSouth.setColumns(20);
        jTextAreaSouth.setEditable(false);
        jTextAreaSouth.setRows(5);
        jTextAreaSouth.setText("\u5206\u5272\u4f7f\u7528\u65b9\u6cd5\uff1a\n\u539f\u59cb\u6587\u4ef6\u8f93\u5165\u8981\u5206\u5272\u7684\u6587\u4ef6\uff0c\u4fdd\u5b58\u4f4d\u7f6e\u8f93\u5165\u5206\u5272\u540e\u7684\u6587\u4ef6\n\u5b58\u653e\u5730\u5740\uff0c\u7136\u540e\u9009\u62e9\u5206\u5272\u65b9\u5f0f\uff0c\u70b9\u51fb\u786e\u8ba4\u5373\u53ef\u3002");
        jScrollPaneSouth.setViewportView(jTextAreaSouth);

        getContentPane().add(jScrollPaneSouth, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldUnionSourceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldUnionSourceKeyPressed
	switch(evt.getKeyCode()) {
            case KeyEvent.VK_ENTER: {//按了Enter键
                String filename=jTextFieldUnionSource.getText();//获取文本        				
                String str = filename.replace("\\", "/");//将文件名中"\"换为"/"
                File aimFile = new File(str);
                if (aimFile.exists()) {/////当原文件存在时显示其信息
                    if (aimFile.isDirectory()) {//当为文件夹时提示用户选择文件
                        jTextAreaSouth.setText("警告!!!\n该目标是一个文件夹\n文件夹名称:\n"
                                        + aimFile.getName() + "\n文件数目:"
                                        + aimFile.listFiles().length + "\n"
                                        + new Date(aimFile.lastModified()) + "\n请正确选择文件!!");
                    }
                    if (aimFile.isFile()) {//当为文件时将文件列为将要合并的文件
                        unionFileNames[Number++] = jTextFieldUnionSource.getText();
                        jTextAreaSouth.setText("将要合并的文件:\n");
                        for (int i = 0; i < Number; i++)
                            jTextAreaSouth.append(new File(unionFileNames[i]).getName() + "\n");
                    }
                } else {
                    jTextAreaSouth.setText("\n\n对不起,找不到原文件!\n");
                }
            }
        }
    }//GEN-LAST:event_jTextFieldUnionSourceKeyPressed

    private void jTextFieldSplitterSourceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldSplitterSourceKeyPressed
	switch(evt.getKeyCode()) {
            case KeyEvent.VK_ENTER: {//按了Enter键
                String filename=jTextFieldSplitterSource.getText();//获取文本        				
                String str = filename.replace("\\", "/");//将文件名中"\"换为"/"
                File aimFile = new File(str);
                if (aimFile.exists()) {/////当原文件存在时显示其信息
                    if (aimFile.isDirectory()) {//当为文件夹时提示用户选择文件
                        jTextAreaSouth.setText("警告!!!\n该目标是一个文件夹\n文件夹名称:\n"
                                        + aimFile.getName() + "\n文件数目:"
                                        + aimFile.listFiles().length + "\n"
                                        + new Date(aimFile.lastModified()) + "\n请正确选择文件!!");
                    }
                    if (aimFile.isFile()) {//当为文件时显示文件信息
                        jTextAreaSouth.setText("文件名称:\n" + aimFile.getName()
                                        + "\n大小:" + aimFile.length() + "byte\n"
                                        + new Date(aimFile.lastModified()));
                    }
                } else {
                    jTextAreaSouth.setText("\n\n对不起,找不到原文件!\n");
                }
            }
        }
    }//GEN-LAST:event_jTextFieldSplitterSourceKeyPressed

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        String filename="";
        JFileChooser fc=new JFileChooser();//创建一个文件选择器
        //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal=fc.showOpenDialog(this);//弹出一个打开文件选择器,
        //如果用户选择了一个文件,则将文件的绝对路径取出
        if(returnVal==JFileChooser.APPROVE_OPTION){
            filename=fc.getSelectedFile().getAbsolutePath();
        }    
        jTextFieldUnionSource.setText(filename);//在原始文件文本框中显示
        //待合并文件数加1并将文件名存储到unionFileNames数组中
        unionFileNames[Number++] = jTextFieldUnionSource.getText();
        //显示待合并文件信息
	jTextAreaSouth.setText("将要合并的文件:\n");
	for (int i = 0; i < Number; i++)
            jTextAreaSouth.append(new File(unionFileNames[i]).getName() + "\n");
    }//GEN-LAST:event_jButtonAddActionPerformed

    private void jButtonUnionBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUnionBrowseActionPerformed
        String filename="";
        JFileChooser fc=new JFileChooser();//创建一个文件选择器
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal=fc.showOpenDialog(this);//弹出一个打开文件选择器,
        //如果用户选择了一个文件,则将文件的绝对路径取出
        if(returnVal==JFileChooser.APPROVE_OPTION){
            filename=fc.getSelectedFile().getAbsolutePath();
        }    
        jTextFieldUnionTarget.setText(filename);//在保存位置文本框中显示
    }//GEN-LAST:event_jButtonUnionBrowseActionPerformed

    private void jButtonUnionCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUnionCancelActionPerformed
	//点击取消按钮则清空所有文本框
        jTextFieldUnionSource.setText("");
	jTextFieldUnionTarget.setText("");
    }//GEN-LAST:event_jButtonUnionCancelActionPerformed

    private void jButtonUnionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUnionActionPerformed
	File outFile = new File(jTextFieldUnionTarget.getText());//输出流的目标文件
        File[] unionFiles;//要合并的文件组
	if (outFile.isDirectory())
            outFile = new File(outFile, "union");//不指定合并文件名则创建union文件
	FileInputStream in = null;//输入流
	FileOutputStream out;//输出流 
	byte[] buff = new byte[1024];//用于读写,相当一个缓冲区

⌨️ 快捷键说明

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