Java 如何从各种文本字段中找到特定条件,并从文本文件中显示匹配项?

Java 如何从各种文本字段中找到特定条件,并从文本文件中显示匹配项?,java,swing,user-interface,Java,Swing,User Interface,我应该从GUI制作一个清单程序。到目前为止,我的代码可以打开该文件。但是,我现在需要进行修改,以便只显示符合条件的文件。主要问题是文本文件的排序方式很奇怪,这是解码所必需的 所以,如果我把:宝马在制造领域,21123的序列号,消声器的名称,119.99的价格,0的股票。我应该得到文本区域的前5行 如何从各种文本字段中找到特定条件,并从文本文件中显示匹配项 BMW Muffler 21123 119.99 0 Chevy Muffler 20911 52.99 2 Ford Muffler 268

我应该从GUI制作一个清单程序。到目前为止,我的代码可以打开该文件。但是,我现在需要进行修改,以便只显示符合条件的文件。主要问题是文本文件的排序方式很奇怪,这是解码所必需的

所以,如果我把:宝马在制造领域,21123的序列号,消声器的名称,119.99的价格,0的股票。我应该得到文本区域的前5行

如何从各种文本字段中找到特定条件,并从文本文件中显示匹配项

BMW
Muffler
21123
119.99
0
Chevy
Muffler
20911
52.99
2
Ford
Muffler
26854
129.59
2
Honda
Muffler
23319
159.99
2
Toyota
Muffler
27813
199.29
1
BMW
Catalytic Converter
900122
132.33
2
Chevy
Catalytic Converter
902391
69.59
1
Ford
Catalytic Converter
905778
61.23
4
代码
包库;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
公共类Show扩展了javax.swing.JFrame{
/**
*创建新的表单显示
*/
公开展览(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
FileName=newjavax.swing.JTextField();
name=newjavax.swing.JTextField();
serialNumber=newjavax.swing.JTextField();
price=newjavax.swing.JTextField();
onHand=newjavax.swing.JTextField();
make=newjavax.swing.JTextField();
jScrollPane1=newjavax.swing.JScrollPane();
display=newjavax.swing.JTextArea();
open=newjavax.swing.JButton();
save=newjavax.swing.JButton();
find=newjavax.swing.JButton();
Clear=newjavax.swing.JButton();
FileLabel=newjavax.swing.JLabel();
namelab=newjavax.swing.JLabel();
serialNumberLabel=newjavax.swing.JLabel();
pricelab=newjavax.swing.JLabel();
InstockLabel=newjavax.swing.JLabel();
makeLabel=newjavax.swing.JLabel();
Update=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
FileName.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的文件名操作(evt);
}
});
name.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的名称(evt);
}
});
serialNumber.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
已执行的序列号操作(evt);
}
});
显示设置列(20);
显示。设置行(5);
jScrollPane1.setViewportView(显示);
display.setEditable(false);
open.setText(“open”);
open.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的开放操作(evt);
}
});
save.setText(“save”);
find.setText(“find”);
find.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
已执行的FindAction(evt);
}
});
Clear.setText(“Clear”);
Clear.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的清除操作(evt);
}
});
FileLabel.setText(“文件名”);
nameLabel.setText(“名称”);
serialNumberLabel.setText(“序列号”);
pricelab.setText(“价格”);
InstockLabel.setText(“库存:”);
makeLabel.setText(“Make”);
Update.setText(“更新”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.training)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
.addComponent(文件标签)
.addGap(18,18,18)
.addComponent(文件名,javax.swing.GroupLayout.DEFAULT\u SIZE,102,Short.MAX\u VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(名称标签)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(serialNumberLabel、javax.swing.GroupLayout.Alignment.Training)
.addComponent(价格标签))
.addComponent(InstockLabel)
.addComponent(makeLabel))
.addGap(18,18,18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(serialNumber、javax.swing.GroupLayout.PREFERRED\u SIZE、javax.swing.GroupLayout.DEFAULT\u SIZE、javax.swing.GroupLayout.PREFERRED\u SIZE)
.addComponent(名称)
.addComponent(price,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addComponent(onHand,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.Gr
package lib;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Show extends javax.swing.JFrame {

/**
 * Creates new form Show
 */
public Show() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    FileName = new javax.swing.JTextField();
    name = new javax.swing.JTextField();
    serialNumber = new javax.swing.JTextField();
    price = new javax.swing.JTextField();
    onHand = new javax.swing.JTextField();
    make = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    display = new javax.swing.JTextArea();
    open = new javax.swing.JButton();
    save = new javax.swing.JButton();
    find = new javax.swing.JButton();
    Clear = new javax.swing.JButton();
    FileLabel = new javax.swing.JLabel();
    nameLabel = new javax.swing.JLabel();
    serialNumberLabel = new javax.swing.JLabel();
    PriceLabel = new javax.swing.JLabel();
    InstockLabel = new javax.swing.JLabel();
    makeLabel = new javax.swing.JLabel();
    Update = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    FileName.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            FileNameActionPerformed(evt);
        }
    });

    name.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nameActionPerformed(evt);
        }
    });

    serialNumber.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            serialNumberActionPerformed(evt);
        }
    });

    display.setColumns(20);
    display.setRows(5);
    jScrollPane1.setViewportView(display);
    display.setEditable(false);

    open.setText("Open");
    open.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            openActionPerformed(evt);
        }
    });

    save.setText("Save");

    find.setText("Find");
    find.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            findActionPerformed(evt);
        }
    });

    Clear.setText("Clear");
    Clear.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ClearActionPerformed(evt);
        }
    });

    FileLabel.setText("File Name");

    nameLabel.setText("Name");

    serialNumberLabel.setText("serial Number");

    PriceLabel.setText("Price");

    InstockLabel.setText("In Stock:");

    makeLabel.setText("Make");

    Update.setText("Update");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(FileLabel)
                            .addGap(18, 18, 18)
                            .addComponent(FileName, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(serialNumberLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(PriceLabel))
                                .addComponent(InstockLabel)
                                .addComponent(makeLabel))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(serialNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(name)
                                .addComponent(price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(onHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(make, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGap(18, 18, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(open)
                    .addGap(18, 18, 18)
                    .addComponent(save)
                    .addGap(18, 18, 18)
                    .addComponent(find)
                    .addGap(18, 18, 18)
                    .addComponent(Clear)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Update)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {FileName, make, name, onHand, price, serialNumber});

    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(FileLabel)
                                .addComponent(FileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(nameLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(serialNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(serialNumberLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(PriceLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(onHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(InstockLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(make, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(makeLabel))))
            .addGap(38, 38, 38)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(open)
                .addComponent(save)
                .addComponent(find)
                .addComponent(Clear)
                .addComponent(Update))
            .addContainerGap(57, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void FileNameActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void nameActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
}                                    

private void serialNumberActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

private void ClearActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    FileName.setText("");
    name.setText("");
    serialNumber.setText("");
    price.setText("");
    onHand.setText("");
    make.setText("");
    display.setText("");
}                                     

private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     
    try {
        File file = new File(FileName.getText());
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
                    int i=0;

                    while ((line = bufferedReader.readLine()) != null) {

                            stringBuffer.append(line+" ");
                            if(i==4){
                                stringBuffer.append("\n");
                                i =-1;
                            }
                            i++;

                    }
        fileReader.close();
        display.setText(display.getText() + stringBuffer.toString());
    } catch (IOException e) {
                display.setText("Error 101: File Not Found! ");
    }

}                                    

private void findActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:

}                                    

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Show.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Show.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Show.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Show.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Show().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton Clear;
private javax.swing.JLabel FileLabel;
private javax.swing.JTextField FileName;
private javax.swing.JLabel InstockLabel;
private javax.swing.JLabel PriceLabel;
private javax.swing.JButton Update;
private javax.swing.JTextArea display;
private javax.swing.JButton find;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField make;
private javax.swing.JLabel makeLabel;
private javax.swing.JTextField name;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField onHand;
private javax.swing.JButton open;
private javax.swing.JTextField price;
private javax.swing.JButton save;
private javax.swing.JTextField serialNumber;
private javax.swing.JLabel serialNumberLabel;
// End of variables declaration                   
}
 public void search() {
  String s = tf.getText();
  String content = textArea.getText(); // instead from a file
  int index = content.indexOf(s, 0);
  if (index >= 0) {   
   try {
   int end = index + s.length();
   // do something
  }
 }