如何自定义Swing的FileDialog?

时间:2014-07-17 07:03:15

标签: swing encoding awt openfiledialog filedialog

我想为FileDialog添加一些额外的功能。那就是我想将文件格式和编码选项添加到Swing的默认FileDialog。如何添加其他功能?

这是Default FileDialog代码:

public class JFrameDemo extends javax.swing.JFrame {

public JFrameDemo() {
   initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    tabbedPane = new javax.swing.JTabbedPane();
    menuBar = new javax.swing.JMenuBar();
    jMenu3 = new javax.swing.JMenu();
    open = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    menuBar.setBackground(new java.awt.Color(51, 51, 255));

    jMenu3.setText("File");

    open.setText("Open");
    open.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            openActionPerformed(evt);
        }
    });
    jMenu3.add(open);

    menuBar.add(jMenu3);

    setJMenuBar(menuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     
    FileDialog fd = new FileDialog(JFrameDemo.this, "Select File", FileDialog.LOAD);
    fd.show();
}                                    

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
           // ImageIcon img = new ImageIcon("C:\\Icons\book-edit-icon.png");
            JFrameDemo fdemo=new JFrameDemo();

            fdemo.setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration                   
}

0 个答案:

没有答案