Java JFrame 添加带有字段的浏览按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7639306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java JFrame adding browse button with field
提问by anton2g
How do I create in Java a GUI to allow a user to enter a directory into a text field and have a browse button next to it, so they can chose the folder easier? I need to do this using JComponents hopefully. Like the last entry shown here: http://www.adobe.com/support/dreamweaver/assets/flashbutton/images/ss5insertdialog.gif
如何在 Java 中创建 GUI 以允许用户在文本字段中输入目录并在其旁边有一个浏览按钮,以便他们可以更轻松地选择文件夹?希望我需要使用 JComponents 来做到这一点。就像这里显示的最后一个条目:http: //www.adobe.com/support/dreamweaver/assets/flashbutton/images/ss5insertdialog.gif
回答by Cris
Use a JTextField and JFileChooser You have a demo here : http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
使用 JTextField 和 JFileChooser 你在这里有一个演示:http: //download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html
http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html
回答by Sajid
You need to trigger a JFileChooser with the button, and then listen to its response to populate the field. Tutorial here. If you don't want people to manually edit the field, set it disabled so that it can't be edited, but does show the text.
您需要使用按钮触发 JFileChooser,然后监听其响应以填充该字段。教程在这里。如果您不希望人们手动编辑该字段,请将其设置为禁用,以便无法对其进行编辑,但会显示文本。