当我反向/反向输入时,我发现了一些问题。例如。我正在寻找姓名John Lovelock。程序找到这个短语并告诉我它。当我进入洛夫洛克约翰时,它找不到我的比赛。我怎样才能获得这种功能。
继承我所拥有的:
public class Programa extends JFrame implements ActionListener {
private JTextArea text;
public static void main(String[] args) {
new Programa().setVisible(true);
}
public Programa() {
super("Redagavimas");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initialize();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Paieska sarase");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Layout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public void initialize(){
text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);
JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("Failas");
JMenuItem open = new JMenuItem("Atidaryti");
JMenuItem save = new JMenuItem("Issaugoti");
JMenuItem exit = new JMenuItem("Uzdaryti");
JMenuItem[] items = { open, save, exit};
for(JMenuItem item : items) {
item.addActionListener(this);
}
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
bar.add(file);
add(scroll);
setJMenuBar(bar);
}
public class Layout extends JPanel {
private JTextField findText;
private JButton search;
private JTextArea text;
private JMenu menu;
private JMenuItem menuItem;
private JMenuItem exit;
private DefaultListModel<String> vieta;
private DefaultListModel<String> boob;
public Layout() {
setLayout(new BorderLayout());
JPanel boob = new JPanel(new GridBagLayout());
JPanel kvadratas = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints dbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
kvadratas.add(new JLabel("Rasti: "), gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
findText = new JTextField(20);
kvadratas.add(findText, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 4;
search = new JButton("Ieskoti");
kvadratas.add(search, gbc);
text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);
add(kvadratas, BorderLayout.NORTH);
vieta = new DefaultListModel<>();
JList list = new JList(vieta);
add(new JScrollPane(list));
ActionHandler handler = new ActionHandler();
search.addActionListener(handler);
findText.addActionListener(handler);
}
public class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
vieta.removeAllElements();
String searchText = findText.getText();
try (BufferedReader reader = new BufferedReader(new FileReader(new File("sarasas.txt")))) {
String text = null;
while ((text = reader.readLine()) != null) {
if (text.matches(searchText)) {
vieta.addElement(text);
}
}
} catch (IOException ee) {
ee.printStackTrace();
JOptionPane.showMessageDialog(Layout.this, "Nera failo", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Atidaryti")) {
open();
}else if(e.getActionCommand().equals("Issaugoti")) {
save();
} else if(e.getActionCommand().equals("Uzdaryti")) {
System.exit(0);
}
}
private void open(){
try{
BufferedReader atidarymas = new BufferedReader(new FileReader("sarasas.txt"));
String line;
while((line = atidarymas.readLine()) != null){
text.append(line + "\n");
}
atidarymas.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
private void save(){
try{
BufferedWriter irasymas = new BufferedWriter(new FileWriter("sarasas.txt"));
irasymas.write(text.getText());
irasymas.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
也许您应该考虑使用此if (text.contains(searchText))
代替此if (text.matches(searchText))
,因为这样您只能使用姓名或姓氏进行搜索。另外,为了使用更改的顺序进行搜索,您应该在代码中处理此问题。一个虚拟解决方案将是以下,当然会有一个更好的解决方案:
public void actionPerformed(ActionEvent e) {
vieta.removeAllElements();
String searchText = findText.getText();
String[] words = searchText.split(" ");
StringBuilder sb = null;
if(words.length > 1) {
sb = new StringBuilder();
sb.append(words[1]).append(" ").append(words[0]);
}
try (BufferedReader reader = new BufferedReader(new FileReader(new File("sarasas.txt")))) {
String text = null;
while ((text = reader.readLine()) != null) {
if (text.contains(searchText) || (sb != null && text.contains(sb.toString()))) {
vieta.addElement(text);
}
}
} catch (IOException ee) {
ee.printStackTrace();
JOptionPane.showMessageDialog(Layout.this, "No file", "Error", JOptionPane.ERROR_MESSAGE);
}
}