我是Java编程的新手,我正在构建一个应用程序,它将添加,显示和删除给定文件夹位置的文件.
我使用JFileChooser添加了文件,并知道如何删除文件.但是我被显示部分困住了.
我想在我的应用程序中使用不同的图标显示文件和文件夹.我试图在显示面板中添加一个JFileChooser并禁用对话框的按钮和菜单组件,但我还没有成功.有没有更好的方法来做到这一点?
现在,我有一个设置类路径,但我想弹出一个打开的文件,用户选择打开哪个文件.我已经尝试过JFileChooser,但到目前为止还没有成功.这是我的代码:
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog( null ) ;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
File file = chooser.getSelectedFile() ;
}
// I don't want this to be hard-coded:
String filePath = "/Users/Bill/Desktop/hello.txt";
Run Code Online (Sandbox Code Playgroud)
我应该怎么做呢?
如何从 JFileChooser 中删除组件(文件类型);标签及其组合框?
我有以下代码:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("Select Folder");
fileChooser.setApproveButtonText("Select Folder");
fileChooser.setAcceptAllFileFilterUsed(false);
hideComponents(fileChooser.getComponents());
private void hideComponents(Component[] components) {
for (int i= 0; i < components.length; i++) {
if (components[i] instanceof JPanel)
hideComponents(((JPanel)components[i]).getComponents());
else if (//component to remove)//what do I check for in here?
components[i].setVisible(false);
}
Run Code Online (Sandbox Code Playgroud) 这是我目前的菜单:
public class DrawPolygons
{
public static void main (String[] args) throws FileNotFoundException
{
/**
* Menu - file reader option
*/
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// Create the menu bar.
menuBar = new JMenuBar();
// Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("I have items");
menuBar.add(menu);
// a group of JMenuItems
menuItem = new JMenuItem("Load",KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Load your old polygons");
menu.add(menuItem);
menuItem = new JMenuItem("Save",KeyEvent.VK_U);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Save the contents of your polygons");
menu.add(menuItem); …Run Code Online (Sandbox Code Playgroud) java model-view-controller swing jfilechooser actionlistener
最近提出的一个问题是:如何在详细信息视图中启动JFileChooser? 和答案做这件事提供了一个很好的技术.
我想在此提出的愿望一层:因为我现在知道如何打开JFileChooser中的详细信息视图,我也把它用日期排列的文件打开?我知道用户当然可以点击标题,但有没有办法在代码中实现这一点?
搜索不会解决此问题.
我正在使用简单的代码来显示带有自定义标题和接受按钮的JFileChooser对话框:
JFileChooser fc = new JFileChooser();
fc.showDialog(null,"MyText");
Run Code Online (Sandbox Code Playgroud)
在Windows 7上,这按预期工作:显示"保存"对话框,"接受"按钮和对话框标题上的"保存"替换为"MyText" .
但是,在Mac OS X上,只更改了"接受"按钮文本 - 对话框标题为空白.我正在使用Java SE 7和MacOS 10.8.5.
通过在上面两者之间插入这一行:
fc.setDialogTitle("MyText");
Run Code Online (Sandbox Code Playgroud)
显示正确的标题.这是一个已知问题,和/或其他人是否可以重现此行为?
我希望在JTextField中有一个JButton(带有一个文件夹图标图像),就像在JTextField的最右边一样,这样当单击时,该按钮会打开一个JFileChooser,当选择一个文件时,该路径为该文件出现在JTextField中.
我已经制作了这段代码,但没有显示出来.
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
Run Code Online (Sandbox Code Playgroud) 我需要创建一个组件,如图所示 - 一个通过复选框选择目录的树.是否有内置组件(如其他JFileChooser等)?

我想从中更改内置的java图标JFileChooser.JFrameclass有一个setIconImage()set icon的设置方法.但我找不到类似的东西JFileChooser.在不更换咖啡杯的情况下,任何人都可以轻松识别我的软件是用java制作的.任何人都可以帮我避免这个吗?
我正在使用JFileChooser进行浏览文件类.我在编译时遇到问题.它告诉我比找不到符号actionlistener.下面是我的代码:
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.filechooser.*;
public class BrowseForFile
{
private JTextField txtFileName;
private JFrame layout;
public BrowseForFile()
{
super();
initialize();
}
public void initialize()
{
//empty layout
layout = new JFrame();
layout.setTitle("Task Synchronization ");
layout.setBounds(100, 100, 800, 600);
layout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
layout.getContentPane().setLayout(null);
//set the copyright
JLabel lblNewLabel_5 = new JLabel("(c) 2012 All Rights Reserved");
lblNewLabel_5.setForeground(Color.GRAY);
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblNewLabel_5.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel_5.setBounds(527, 548, 255, 14);
layout.getContentPane().add(lblNewLabel_5);
//set the label
JLabel lblSendAFile = new JLabel("Select a …Run Code Online (Sandbox Code Playgroud) java ×10
jfilechooser ×10
swing ×8
date ×1
file-io ×1
icons ×1
import ×1
jbutton ×1
jcomponent ×1
jtextfield ×1
macos ×1