0

以下代码由Netbeans 6.8 Mac版自动生成

public class fileBrowser extends javax.swing.JPanel {

/** Creates new form fileBrowser */
public fileBrowser() {
    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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jFileChooser1 = new javax.swing.JFileChooser();

    setName("Form"); // NOI18N

    jFileChooser1.setName("jFileChooser1"); // NOI18N

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, jFileChooser1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(jFileChooser1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
    );
}// </editor-fold>


// Variables declaration - do not modify
private javax.swing.JFileChooser jFileChooser1;
// End of variables declaration

}

我正在尝试使用以下代码制作一个调用它的按钮(以允许用户选择文件):

 private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
fileBrowser fileBrowser = new fileBrowser();
fileBrowser.setVisible(true);//why not working?

}

嗯...当我单击按钮时,我只得到一个空表单...知道错误在哪里吗?

4

2 回答 2

2

JFileChooser 本身不是一个组件,就像一个按钮。这是一个对话框。所以这是“正确”的工作。查看 JFileChooser Java Doc 以了解如何使用 JFileChooser。

于 2010-02-18T06:33:54.553 回答
2

您不应该使用 MouseListener 来单击按钮。您应该使用 ActionListener。

阅读 JFileChooser API 并点击关于“如何使用文件选择器”的 Swing 教程的链接,以获取有关如何显示文件选择器的工作示例。基本上,您的代码将类似于示例程序中 ActionListener 中的代码。

于 2010-02-18T06:49:02.590 回答