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

📄 tryui.java

📁 实现打开文件夹的功能
💻 JAVA
字号:
/*
 * tryUI.java
 *
 * Created on 2007年9月22日, 下午5:43
 */

package my.tryUI;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  Administrator
 */
public class tryUI extends javax.swing.JFrame {
    
    /** Creates new form tryUI */
    public tryUI() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        Fileopen = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        openbtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txa = new javax.swing.JTextArea();

        Fileopen.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
        Fileopen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileopenActionPerformed(evt);
            }
        });

        Fileopen.getAccessibleContext().setAccessibleParent(openbtn);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("tryUI");
        openbtn.setMnemonic('0');
        openbtn.setText("Open");
        openbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openbtnActionPerformed(evt);
            }
        });

        openbtn.getAccessibleContext().setAccessibleParent(null);

        txa.setColumns(20);
        txa.setRows(5);
        jScrollPane1.setViewportView(txa);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                    .addComponent(openbtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openbtn))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void FileopenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileopenActionPerformed
// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_FileopenActionPerformed

    private void openbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openbtnActionPerformed
     JFileChooser Fileopen = new JFileChooser();
     Fileopen.showOpenDialog(openbtn);

    /* int retval = Fileopen.showDialog(openbtn, null);
	if(retval == JFileChooser.APPROVE_OPTION) {
	    File fname = Fileopen.getSelectedFile();
	    if(fname != null) {
		if(fname.isDirectory()) {
		    JOptionPane.showMessageDialog(
			openbtn, "You chose this directory: " +
			Fileopen.getSelectedFile().getAbsolutePath()
		    );
		} else {
		    JOptionPane.showMessageDialog(
			openbtn, "You chose this file: " +
			Fileopen.getSelectedFile().getAbsolutePath()
		    );
		}
		return;
	    }
	}
	JOptionPane.showMessageDialog(openbtn, "No file was chosen.");
     */
     
     
    String filename=Fileopen.getSelectedFile().getAbsolutePath();
    try{
         FileInputStream fi=new FileInputStream(filename);
         byte ba[]=new byte[fi.available()];
         
         fi.read(ba);
         
         txa.setText(new String(ba));
         fi.close();
     }
     
     catch(IOException ioe){}
    
     
    
    }//GEN-LAST:event_openbtnActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public  static void  main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tryUI().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JFileChooser Fileopen;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton openbtn;
    private javax.swing.JTextArea txa;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

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