以下代码由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?
}Well...when我点击按钮,我只得到一个空的form...Any的想法,错误在哪里?
发布于 2010-02-18 14:33:55
JFileChooser本身并不是一个组件,就像按钮一样。这是一个对话。所以这是“正确的”工作。有关如何使用JFileChooser,请查看JFileChooser Java文档。
发布于 2010-02-18 14:49:03
您不应该使用MouseListener来单击按钮。您应该使用ActionListener。
阅读JFileChooser应用编程接口,并按照“如何使用文件选择器”的Swing教程的链接,获取有关如何显示文件选择器的实际示例。基本上,您的代码将类似于示例程序中的ActionListener中的代码。
https://stackoverflow.com/questions/2286728
复制相似问题