[Javaのスイング] JavaでのJFileChooser

JavaでJFileChooserがオブジェクト表示枠で、ファイルを開いたり保存することができます. それはこのようなものです:

Javaでファイルを開いたり保存

今、私たちはオープンの簡単な例に移動し、ファイルを保存します.

開いて、Javaでファイルを保存

package nguyenvanquan7826.JFileChooser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 * ---------- date: Jul 29, 2014 - filename: DemoJFileChooser.java ----------
 */
public class DemoJFileChooser extends JFrame implements ActionListener {

	public static String iconOpen = "iconOpen.png";
	public static String iconSave = "iconSave.png";

	private JTextArea taLog;
	private JButton btnOpen, btnSave;
	private JFileChooser fc = new JFileChooser();

	public DemoJFileChooser() {
		addContent();
		setDisplay();
	}

	/**
	 * set display for JFrame
	 */
	private void setDisplay() {
		setTitle("Demo JFileChooser");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * add content for JFrame, JFrame contain a ButtonPane (contain buttons) and
	 * a JTextArea to show something you work
	 */
	private void addContent() {
		setLayout(new BorderLayout());
		add(createButtonPanel(), BorderLayout.PAGE_START);
		add(new JScrollPane(taLog = createTextArea(15, 30)),
				BorderLayout.CENTER);
	}

	/**
	 * create a JPanel contain 2 button (open and save) on TOP of JFrame
	 */
	private JPanel createButtonPanel() {
		JPanel panel = new JPanel(new FlowLayout());
		panel.add(btnOpen = createButton("Open file", iconOpen));
		panel.add(btnSave = createButton("Save file", iconSave));
		return panel;
	}

	/**
	 * create JTextArea
	 */
	private JTextArea createTextArea(int row, int col) {
		JTextArea ta = new JTextArea(row, col);
		ta.setWrapStyleWord(true);
		ta.setLineWrap(true);
		return ta;
	}

	/**
	 * create a JButton with text and icon and add Action for it
	 */
	private JButton createButton(String text, String iconLink) {
		ImageIcon icon = createImageIcon(iconLink);
		JButton btn = new JButton(text, icon);
		btn.addActionListener(this);
		return btn;
	}

	/**
	 * get a icon via link to icon
	 */
	private ImageIcon createImageIcon(String iconLink) {
		ImageIcon icon = new ImageIcon(getClass().getResource(iconLink));
		return icon;
	}

	/**
	 * Handle when you open a file
	 */
	private void openFile() {
		int select = fc.showOpenDialog(this);
		if (select == JFileChooser.APPROVE_OPTION) {
			taLog.append("You open " + fc.getSelectedFile().getName());
		} else {
			taLog.append("You cancelled open!");
		}
		taLog.append("\n");
	}

	/**
	 * Handle when you save a file
	 */
	private void saveFile() {
		int select = fc.showSaveDialog(this);
		if (select == JFileChooser.APPROVE_OPTION) {
			taLog.append("You save " + fc.getSelectedFile().getName());
		} else {
			taLog.append("You cancelled save!");
		}
		taLog.append("\n");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnOpen) {
			openFile();
			return;
		}
		if (e.getSource() == btnSave) {
			saveFile();
			return;
		}
	}

	public static void main(String[] args) {
		new DemoJFileChooser();
	}
}

注目ます上記のコードで 2 オープンの下に表示され、別のファイルを保存するためのコマンド.

int select = fc.showOpenDialog(this);
int select = fc.showSaveDialog(this);

次の引数 JFileChooser.APPROVE_OPTION あなたが受け入れたことを示します “オープン” または “保存” ファイル.
アイコン セーブオープン 次のようにして、同じパッケージに入れます:

開いて、Javaでファイルを保存

上記の例では、JFileChooserのを使用する方法について説明します, 仕事 テキストフ​​ァイルの読み取りと書き込み または オブジェクトに読み込みます あなた自身NHE.

続きを読む: JFileChooserのを使用, クラスのJFileChooser, Javaのスイング