今からはじめるプログラミング51(検索画面を追加)
以前に、カードデータベースみたいになってしまった。
ノベルゲームのような、画面遷移アプリ・・・下参照。
今からはじめるプログラミング47(ノベルゲームふたたび?) - yo2an’s blog
ですが、データベースといえば、データが検索できなければ。。。
とふと思ったので、検索画面と結果のリストを追加してみました。
そして、検索結果をダブルクリックすると、また画面がひらく、みたいなギミックも追加。ただ、めんどいのでファイルの中は検索していません。
・・・半分意味のない検索に見えてしまいますね。。。
検索画面のプログラム(SearchFrame)
-----------------------------------------------------------------
package sample33;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
public class SearchFrame extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private String rootPath = null;
public void setRoot(String root) {
this.rootPath = root;
}
public SearchFrame() {
init();
}
private JTextField txt = new JTextField();
private void init() {
// TODO Auto-generated method stub
this.setTitle("検索画面");
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
JPanel centerPnl = new JPanel();
centerPnl.setLayout(new BorderLayout());
JLabel lbl= new JLabel("文字検索キーワード:");
JButton btn = new JButton("けんさく");
centerPnl.add(lbl,BorderLayout.WEST);
centerPnl.add(txt,BorderLayout.CENTER);
centerPnl.add(btn,BorderLayout.EAST);
btn.addActionListener(this);
container.add(centerPnl,BorderLayout.CENTER);
if(this.rootPath==null) {
rootPath = "content2";
}
JButton btnClose = new JButton("とじる");
btnClose.addActionListener(this);
btnClose.setActionCommand("close");
container.add(btnClose,BorderLayout.NORTH);
this.setSize(600,400);
this.setVisible(true);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("close".equals(command)) {
this.setVisible(false);
return;
}
String word = txt.getText();
System.out.print("word="+word);
List<String> result = new ArrayList<String>();
result = this.search(word,this.rootPath,result);
new ListFrame(result);
}
public List<String> search(String word,String path,List<String> object) {
File file = new File(path);
File files = file.listFiles();
for(int i=0;i<files.length;i++) {
String fileName = files[i].getName();
int indexOfword = fileName.indexOf(word);
System.out.println("fileName="+fileName);
if(files[i].isDirectory()) {
//小階層も
object = this.search(word,files[i].getAbsolutePath(),object);
}
if(indexOfword==-1) {
//TODO 普通に考えられないひどいif文
}else {
//
object.add(files[i].getAbsolutePath());
}
}
return object;
}
public static void main(String args) {
new SearchFrame();
//Rootをセットする場合
// SearchFrame frame = new SearchFrame();
// frame.setRoot("path");
}
}
-----------------------------------------------------------------
リストのプログラム(ListFrame)
-----------------------------------------------------------------
package sample33;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
public class ListFrame extends JFrame implements MouseListener, ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public ListFrame(List<String> list) {
init(list);
}
JTable table = null;
private void init(List<String> list) {
this.setTitle("検索結果:");
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
table = new JTable(list.size(), 1);
for (int i = 0; i < list.size(); i++) {
table.setValueAt(list.get(i), i, 0);
}
table.addMouseListener(this);
this.getContentPane().add(table, BorderLayout.CENTER);
JButton btnClose = new JButton("とじる");
btnClose.addMouseListener(this);
btnClose.setActionCommand("close");
container.add(btnClose, BorderLayout.SOUTH);
this.setSize(400, 600);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
if (table == null) {
this.setVisible(false);
return;
}
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
if (row == -1 || col == -1) {
this.setVisible(false);
return;
}
String s = (String) table.getValueAt(row, col);
Frame frame = new Frame(s);
frame.setRoot();
frame.setVisible(true);
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
}
}
-----------------------------------------------------------------