从文本字段中搜索数组列表,并在JOptionPane中打印出结果

时间:2016-09-08 00:23:12

标签: java swing

此论坛的新手和Java新手。我试图从文本字段中搜索数组列表。这是一个例子:用户将在文本字段中键入部分描述,按回车键,它将在数组列表中搜索描述,并在JOptionPane中打印信息。我想我最终希望将它打印在带有滚动条的文本区域中,因为可能有多行。示例:用户类型拟合,它将搜索数组列表,然后打印出来"描述:(拟合,带刺,旋转弯头)位置:c04-01-e04 PB部件号:34036制造商#:208302"。搜索拟合实际上会打印许多行。我将发送2个程序,每个程序在不同的帖子中。第一个将是我试图去上班的人。我知道的第二个工作,但只打印到控制台。我希望这是有道理的。

//Program with GUI

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



@SuppressWarnings("serial")
public class Frame extends JFrame {

    private JTextField descField;
    private JTextField locField;
    private JTextField pbField;
    private JTextField manufField;
    private JPanel panel;
    private JLabel title;
    private JLabel descSearch;
    private JLabel pbSearch;
    private JLabel locSearch;
    private JLabel manufSearch;
    private String desc;
    private String location;
    private String pbPartNum;
    private String manufPartNum;

    public Frame(){

        super("Parts look-up");
        setLayout(null);

        panel = new JPanel();
        add(panel);

        descField = new JTextField(20);
        descField.setBounds(160, 100, 250, 30);
        add(descField);

        locField = new JTextField(20);
        locField.setBounds(160, 150, 250, 30);
        add(locField);

        pbField = new JTextField(20);
        pbField.setBounds(160, 200, 250, 30);
        add(pbField);

        manufField = new JTextField(20);
        manufField.setBounds(160, 250, 250, 30);
        add(manufField);

        title = new JLabel("Parts look-up");
        title.setBounds(180, 20, 100, 15);
        add(title);

        descSearch = new JLabel("Description");
        descSearch.setBounds(25, 100, 200, 30);
        add(descSearch);

        locSearch = new JLabel("Location");
        locSearch.setBounds(25, 150, 200, 30);
        add(locSearch);

        pbSearch = new JLabel("PB part#");
        pbSearch.setBounds(25, 200, 200, 30);
        add(pbSearch);

        manufSearch = new JLabel("Manufacturers part#");
        manufSearch.setBounds(25, 250, 200, 30);
        add(manufSearch);

        ThisHandler handler = new ThisHandler();
        descField.addActionListener(handler);
        locField.addActionListener(handler);
        pbField.addActionListener(handler);
        manufField.addActionListener(handler);

    }

    public Frame(String desc, String location, String pbPartNum, String manufPartNum){
        this.desc = desc;
        this.location = location;
        this.pbPartNum = pbPartNum;
        this.manufPartNum = manufPartNum;
    }


    public String getDesc(){
        return desc;
    }

    public String getLoc(){
        return location;
    }

    public String getPbPartNum(){
        return pbPartNum;
    }

    public String getManufPartNum(){
        return manufPartNum;
    }

    public String setDesc(){
        return desc;
    }

    public String setLoc(){
        return location;
    }
    public String setPbPartNum(){
        return pbPartNum;
    }
    public String setManufPartNum(){
        return manufPartNum;
    }

    public class ThisHandler implements ActionListener{

        public void searchByDesc(String desc){ 
            for(int i = 0; i < Run.partsList.size(); i++){
                if(Run.partsList.get(i).getDesc().contains(desc.toLowerCase())){
                    JOptionPane.showMessageDialog(null, "HELLO");
                    return;
                }   
            }
        }

        public void actionPerformed(ActionEvent event){
            if(event.getSource() == descField){
                searchByDesc(desc);





    }                       
 }

}
}

运行

import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class Run {
    static ArrayList<Frame> partsList = new ArrayList<>();

    public static void main(String[] args) {

        Frame parts = new Frame();
        parts.pack();
        parts.setSize(450, 400);
        parts.setLocationRelativeTo(null);
        parts.setResizable(false);
        parts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        parts.setVisible(true);

        //Cabinet 1, C01-01-A01 to C01-01-Q04
        add("open", "c04-01-a01", "", "");
        add("open", "c04-01-a02", "", "");
        add("open", "c04-01-a03", "", "");
        add("open", "c04-01-a04", "", "");
        add("pressure guage", "c04-01-b01", "32856", "sp206317");
        add("gauge vacuum", "c04-01-b02", "32857", "sp206316");
        add("open", "c04-01-b03", "", "");
        add("nut", "c04-01-b04", "26892", "206996");
        add("muffler pneumatic exhaust", "c04-01-c01", "26900", "206041");
        add("valve poppet", "c04-01-c02", "27010", "iji205992");
        add("gasket manifold", "c04-01-c03", "21790", "355074");
        add("transfer tube .06id", "c04-01-c04", "21921", "390592");
        add("relief valve", "c04-01-d01", "26899", "206010");
        add("open", "c04-01-d02", "", "");
        add("gasket mylar .180id", "c04-01-d03", "26779", "201872");
        add("open", "c04-01-do4", "", "");
        add("fitting - l", "c04-01-e01", "26772", "203303");
        add("tubing, teflon, .053 id x .016w per foot", "c04-01-e02", "26909", "206214");
        add("open", "c04-01-e03", "", "");
        add("fitting, barbed, swivel elbow", "c04-01-e04", "34036", "208302");
        add("regulator - air", "c04-01-f01", "26883", "205995");
        add("transducer air/vac 40psi 14 in hg", "c04-01-f02", "26791", "202050");
        add("valve needle", "c04-01-f03", "36376", "210955");
        add("air pressure monitor switch", "c04-01-f04", "32875", "sp204447");
        add("solenoid packed video jet", "c04-01-g01", "32855", "sp206429");
        add("open", "c04-01-g02", "", "");
        add("ijp flat washer 193499", "c04-01-g03", "13286", "193499");
        add("tee barbed", "c04-01-g04", "27006", "sp205987");
        add("fitting tube-male 1/8 inch", "c04-01-h01", "26770", "203292");
        add("fitting - male connector", "c04-01-h02", "26978", "203473");
        add("fitting - tube - barbed - 1/16in. id", "c04-01-h03", "26731", "201844");
        add("fitting tube plug 5/32", "c04-01-h04", "26726", "201868");
        add("fitting barb - 10-32 tube male cnnctr1/8 npt", "c04-01-i01", "26733", "201864");
        add("switch differential pressure (w/o mount)", "c04-01-i02", "32876", "sp204446");
        add("open", "c04-01-i03", "", "");



    }

    public static void add(String desc, String location, String pbPartNum, String manufPartNum) {
        Frame part = new Frame(desc, location, pbPartNum, manufPartNum);
        partsList.add(part);
    }
}

零件

//Program that works when printing to the console



public class Parts {
    private String desc;
    private String location;
    private String pbPartNum;
    private String manufPartNum;

    public Parts(String desc, String location, String pbPartNum, String manufPartNum){
        this.desc = desc;
        this.location = location;
        this.pbPartNum = pbPartNum;
        this.manufPartNum =manufPartNum;
    }

    public String getDesc(){
        return desc;
    }
    public String getLocation(){
        return location;
    }

    public String getPbPartNum(){
        return pbPartNum;
    }

    public String getManufPartNum(){
        return manufPartNum;
    }

    public String setDesc(){
        return desc;
    }

    public String setLocation(){
        return location;
    }
    public String setPbPartNum(){
        return pbPartNum;
    }
    public String setManufPartNum(){
        return manufPartNum;
    }

    public void print(){
         System.out.println(
                     "Description: " + "(" + getDesc().toUpperCase() + ")" + "\t" + 
                     "Location: " + "(" + getLocation().toUpperCase()+ ")" + "\t" + 
                     "P.B. Part #: " + "(" + getPbPartNum() + ")" + "\t" + 
                     "Manufacturers #: " + "(" + getManufPartNum().toUpperCase() + ")" + "\n");
     }


























}

InventoryList

import java.util.ArrayList;

public class InventoryList{

    static ArrayList<Parts> partsList = new ArrayList<>();

    public static void main(String[] args) {

        //Cabinet 1, C01-01-A01 to C01-01-Q04
        add("open", "c04-01-a01", "", "");
        add("open", "c04-01-a02", "", "");
        add("open", "c04-01-a03", "", "");
        add("open", "c04-01-a04", "", "");
        add("pressure guage", "c04-01-b01", "32856", "sp206317");
        add("gauge vacuum", "c04-01-b02", "32857", "sp206316");
        add("open", "c04-01-b03", "", "");
        add("nut", "c04-01-b04", "26892", "206996");
        add("muffler pneumatic exhaust", "c04-01-c01", "26900", "206041");
        add("valve poppet", "c04-01-c02", "27010", "iji205992");
        add("gasket manifold", "c04-01-c03", "21790", "355074");
        add("transfer tube .06id", "c04-01-c04", "21921", "390592");
        add("relief valve", "c04-01-d01", "26899", "206010");
        add("open", "c04-01-d02", "", "");
        add("gasket mylar .180id", "c04-01-d03", "26779", "201872");
        add("open", "c04-01-do4", "", "");
        add("fitting - l", "c04-01-e01", "26772", "203303");
        add("tubing, teflon, .053 id x .016w per foot", "c04-01-e02", "26909", "206214");
        add("open", "c04-01-e03", "", "");
        add("fitting, barbed, swivel elbow", "c04-01-e04", "34036", "208302");
        add("regulator - air", "c04-01-f01", "26883", "205995");
        add("transducer air/vac 40psi 14 in hg", "c04-01-f02", "26791", "202050");
        add("valve needle", "c04-01-f03", "36376", "210955");
        add("air pressure monitor switch", "c04-01-f04", "32875", "sp204447");
        add("solenoid packed video jet", "c04-01-g01", "32855", "sp206429");
        add("open", "c04-01-g02", "", "");
        add("ijp flat washer 193499", "c04-01-g03", "13286", "193499");
        add("tee barbed", "c04-01-g04", "27006", "sp205987");
        add("fitting tube-male 1/8 inch", "c04-01-h01", "26770", "203292");
        add("fitting - male connector", "c04-01-h02", "26978", "203473");
        add("fitting - tube - barbed - 1/16in. id", "c04-01-h03", "26731", "201844");
        add("fitting tube plug 5/32", "c04-01-h04", "26726", "201868");
        add("fitting barb - 10-32 tube male cnnctr1/8 npt", "c04-01-i01", "26733", "201864");
        add("switch differential pressure (w/o mount)", "c04-01-i02", "32876", "sp204446");
        add("open", "c04-01-i03", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");
        add("", "", "", "");

    }





    public static void space(){
        System.out.println("-----------------------");
        System.out.println("|||||||||||||||||||||||");
        System.out.println("-----------------------");
    }

    private static void add(String desc, String location, String pbPartNum, String manufPartNum) {
        Parts part = new Parts(desc, location, pbPartNum, manufPartNum);
        partsList.add(part);
    }

    public static void printAll(){
        for(int i = 0; i < partsList.size(); i++){
            partsList.get(i).print();
        }
    }

    public static void searchByPbNum(String pbPartNum){ 
        for(int i = 0; i < partsList.size(); i++){
            if(partsList.get(i).getPbPartNum().equals(pbPartNum)){
                System.out.println("Description: " + "(" + partsList.get(i).getDesc().toUpperCase() + ")" + "\t" 
                        + "Location: " + "(" + partsList.get(i).getLocation().toUpperCase() + ")" + "\t" 
                        + "PB part #: " + "(" + partsList.get(i).getPbPartNum() + ")" + "\t"  
                        + "Manufacturers #: " + "(" + partsList.get(i).getManufPartNum().toUpperCase() + ")");
            }   
        }   
    }

    public static void searchByLocation(String location){ 
        for(int i = 0; i < partsList.size(); i++){
            if(partsList.get(i).getLocation().equals(location.toLowerCase())){
                System.out.println("Description: " + "(" + partsList.get(i).getDesc().toUpperCase() + ")" + "\t" 
                        + "Location: " + "(" + partsList.get(i).getLocation().toUpperCase() + ")" + "\t" 
                        + "PB part #: " + "(" + partsList.get(i).getPbPartNum() + ")" + "\t"  
                        + "Manufacturers #: " + "(" + partsList.get(i).getManufPartNum().toUpperCase() + ")");
            }   
        }       
    }

    public static void searchByManufNum(String manufPartNum){ 
        for(int i = 0; i < partsList.size(); i++){
            if(partsList.get(i).getManufPartNum().equals(manufPartNum.toLowerCase())){
                System.out.println("Description: " + "(" + partsList.get(i).getDesc().toUpperCase() + ")" + "\t" 
                        + "Location: " + "(" + partsList.get(i).getLocation().toUpperCase() + ")" + "\t" 
                        + "PB part #: " + "(" + partsList.get(i).getPbPartNum() + ")" + "\t"  
                        + "Manufacturers #: " + "(" + partsList.get(i).getManufPartNum().toUpperCase() + ")");
            }   
        }
    }

    public static void searchByDesc(String desc){ 
        for(int i = 0; i < partsList.size(); i++){
            if(partsList.get(i).getDesc().contains(desc.toLowerCase())){
                System.out.println("Description: " + "(" + partsList.get(i).getDesc().toUpperCase()+ ")" + "\t" 
                        + "Location: " + "(" + partsList.get(i).getLocation().toUpperCase()+ ")" + "\t" 
                        + "PB part #: " + "(" + partsList.get(i).getPbPartNum() + ")" + "\t"  
                        + "Manufacturers #: " + "(" + partsList.get(i).getManufPartNum().toUpperCase() + ")"  + "\n" );
            }   
        }
    }
















}

1 个答案:

答案 0 :(得分:0)

尝试更换

 for(int i = 0; i < Run.partsList.size(); i++){
            if(Run.partsList.get(i).getDesc().contains(desc.toLowerCase())){
                JOptionPane.showMessageDialog(null, "HELLO");
                return;
            }   
        }

 for (String des : Run.partsList) {
        if (des.contains(desc.toLowerCase())) {
            JOptionPane.showMessageDialog(null, "HELLO");
            return;
        }
    }