为什么 Java Jbutton 没有正确调用 JFileChooser?
以下代码是由 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?
}
嗯...当我单击该按钮时,我只得到一个空表格...知道错误在哪里吗?
The following code was generated automatically by Netbeans 6.8 Mac version
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
}
I'm trying to make a button that call it (to allow the user to select a file) with the following code:
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
fileBrowser fileBrowser = new fileBrowser();
fileBrowser.setVisible(true);//why not working?
}
Well...when I click the button, I only get an empty form...Any idea where is the bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JFileChooser 本身并不是一个组件,就像按钮一样。这是一个对话框。所以这是“正确”工作的。检查 JFileChooser Java 文档以了解如何使用 JFileChooser。
A JFileChooser is not a component per se, like a button. It's a dialog. So this is working "correctly". Check the JFileChooser Java Doc for how to use JFileChooser.
您不应该使用 MouseListener 来单击按钮。您应该使用 ActionListener。
阅读 JFileChooser API 并点击有关“如何使用文件选择器”的 Swing 教程的链接,获取有关如何显示文件选择器的工作示例。基本上,您的代码将类似于示例程序中 ActionListener 中的代码。
You should not be using a MouseListener for clicking on a button. You should be using an ActionListener.
Read the JFileChooser API and follow the link to the Swing tutorial on "How to Use File Choosers" for a working example of how to display a file chooser. Basically your code will look something like the code in the ActionListener from the example program.