第五十一講 GUI程式設計練習
阿新 • • 發佈:2018-12-15
檔案(夾)搜尋
在文字框中輸入目錄,點選“確定”按鈕,將該目錄中的檔案與資料夾名稱列在下面的文字區域中,如圖所示。
package cn.liayun.swing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MyWindow extends JFrame {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable () {
public void run() {
try {
MyWindow frame = new MyWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 851, 578);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u786E\u5B9A");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// System.out.println("hello button");
btnNewButtonActionPerformed(evt);
}
});
btnNewButton.setBounds(671, 31, 105, 43);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
textFieldKeyPressed(evt);
}
});
textField.setBounds(27, 31, 630, 43);
contentPane.add(textField);
textField.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(27, 97, 748, 401);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
protected void textFieldKeyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
showDir();
}
}
protected void btnNewButtonActionPerformed(ActionEvent evt) {
// System.out.println("hello button");
showDir();
}
private void showDir() {
//通過在文字框輸入具體的目錄,將目錄中的當前檔案或者資料夾的名稱列出到文字區域中
/*
* 思路:
* 1,獲取文字框中的路徑
* 2,將路徑封裝成File物件,list()方法
* 3,將list()列表中的內容展現在文字區域中
*/
//1,獲取路徑,通過文字框物件來完成
String str_dir = textField.getText();
//2,將字串路徑封裝成File物件
File dir = new File(str_dir);
//判斷健壯性
if (dir.exists() && dir.isDirectory()) {
//清空文字區域
textArea.setText("");
//3,獲取檔名稱列表
String[] names = dir.list();
for (String name : names) {
//4,將名字顯示到文字區域
textArea.append(name + LINE_SEPARATOR);
}
}
}
}
模仿Windows的記事本程式
模仿Windows的記事本(notepad.exe)程式完成檔案的開啟、退出的操作,如下圖所示。
package cn.liayun.swing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import cn.liayun.separator.Separator;
public class MyMenu extends JFrame {
// private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private JPanel contentPane;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyMenu frame = new MyMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyMenu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 875, 647);
JMenuBar menuBar = new JMenuBar();
menuBar.setToolTipText("");
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("\u6587\u4EF6");
menuBar.add(mnNewMenu);
JMenuItem menuItem = new JMenuItem("\u6253\u5F00");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuItemActionPerformed(evt);
}
});
mnNewMenu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("\u9000\u51FA");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuItem_1ActionPerformed(evt);
}
});
mnNewMenu.add(menuItem_1);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
protected void menuItemActionPerformed(ActionEvent evt) {
//建立一個檔案選擇器
JFileChooser chooser = new JFileChooser();
//建立一個副檔名過濾器
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Java & Txt", "java", "txt");
//給檔案選擇器設定過濾器
chooser.setFileFilter(filter);
//檔案選擇器呼叫了顯示選擇檔案對話方塊,並獲取該對話方塊的返回值
int returnVal = chooser.showOpenDialog(this);
//判斷返回值
if(returnVal == JFileChooser.APPROVE_OPTION) {
// System.out.println("You chose to open this file: " +
// chooser.getSelectedFile().getName());
//清空文字區域
textArea.setText("");
//獲取選擇的目錄
File file = chooser.getSelectedFile();
// System.out.println(file.getAbsolutePath());
//讀取檔案
try {
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bufr.readLine()) != null) {
textArea.append(line + Separator.LINE_SEPARATOR);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void menuItem_1ActionPerformed(ActionEvent evt) {
System.exit(0);
}
}