如何将JTEXTFIELD输入保存到Java中的.txt文件?

发布于 2025-02-04 15:00:01 字数 5323 浏览 5 评论 0原文

我要做的就是拥有库存表格,并拥有保存在文本文件中的“ jtextfield”输入以供以后召回。在“保存”按钮上,我需要它来保存文本字段以将文件作为表单。请把我指向正确的方向,任何建议都将不胜感激。香港专业教育学院几乎尝试了在线和书中的所有内容,但我找不到解决方案。 PS我正在使用Eclipse IDE



public class inputFrameNXA implements ActionListener {
    
    

    public static void main(String[ ] args) 
    {

       
      
     //Contructs Frame 
     JFrame inputFrame = new JFrame( );   
     inputFrame.setBounds(100, 100, 740, 439);
     inputFrame.setVisible(true);
     inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLayeredPane layeredPane = new JLayeredPane();
    GroupLayout groupLayout = new GroupLayout(inputFrame.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                .addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 724, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addComponent(layeredPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
    );

    {
    JLabel bckGndLbl = new JLabel("");
    layeredPane.setLayer(bckGndLbl, 1);
    bckGndLbl.setBounds(-23, -244, 1212, 894);
    layeredPane.add(bckGndLbl);
}
    {
    JButton newBtn = new JButton( );
    layeredPane.setLayer(newBtn, 2);
    newBtn.setBounds(10, 43, 89, 23);
    newBtn.setToolTipText("New Entry");
    newBtn.addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e)
            {
            
            }
            });
    
    layeredPane.add(newBtn);
    
    //Buttons Word Over Background  
    JLabel newLtrs = new JLabel("New");
      newLtrs.setFont(new Font("Takoma", Font.BOLD, 11));
      newLtrs.setHorizontalAlignment(SwingConstants.CENTER);
      layeredPane.setLayer(newLtrs, 3);
      newLtrs.setBounds(10, 43, 89, 23);
      layeredPane.add(newLtrs);
      //Set Action
     
      
      
      
     
    

    JButton delButton = new JButton( );
    layeredPane.setLayer(delButton, 2);
    delButton.setBounds(10, 72, 89, 23);
    delButton.setToolTipText("Delete Entry");
    delButton.addActionListener(new ActionListener( ) {
        public void actionPertformed(ActionEvent e)
        {
            actionPerformed( e );
        }

    });
      
     
    layeredPane.add(delButton);
    
    JLabel delLtrs = new JLabel("Delete");
    delLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
    delLtrs.setHorizontalAlignment(SwingConstants.CENTER);
    layeredPane.setLayer(delLtrs, 3);
    delLtrs.setBounds(10, 72, 89, 23);
    layeredPane.add(delLtrs);
    
    
    
    

    
  
    JButton saveBtn = new JButton( );
    layeredPane.setLayer(saveBtn, 2);
    saveBtn.setBounds(10, 102, 89, 23);
    saveBtn.setToolTipText("Save Entry");
    saveBtn.addActionListener(new ActionListener( ) {
        public void actionPertformed(ActionEvent e)
        {
            saveField( );
        }

        private void saveField() {
            
            
        }
    });
    
    layeredPane.add(saveBtn);   
    //Action Method
    
    
    
    JLabel saveLtrs = new JLabel("Save");
    saveLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
    saveLtrs.setHorizontalAlignment(SwingConstants.CENTER);
    layeredPane.setLayer(saveLtrs, 3);
    saveLtrs.setBounds(10, 102, 89, 23);
    layeredPane.add(saveLtrs);
    
      
     


    
    



    JLabel lblNewLabel_1 = new JLabel("Inventory Item");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
    lblNewLabel_1.setForeground(Color.WHITE);
    layeredPane.setLayer(lblNewLabel_1, 2);
    lblNewLabel_1.setBounds(151, 43, 116, 23);
    layeredPane.add(lblNewLabel_1);
    

    JTextField invField = new JTextField(" ");
    invField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(invField, 2);
    invField.setBounds(266, 39, 391, 23);
    layeredPane.add(invField);
    invField.setColumns(10);
    
    JLabel lblNewLabel_2 = new JLabel("Location");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
    lblNewLabel_2.setForeground(Color.WHITE);
    layeredPane.setLayer(lblNewLabel_2, 2);
    lblNewLabel_2.setBounds(151, 76, 116, 19);
    layeredPane.add(lblNewLabel_2);


    JTextField locationField = new JTextField(" ");
    locationField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(locationField, 2);
    locationField.setBounds(266, 73, 391, 23);
    layeredPane.add(locationField);
    locationField.setColumns(10);

    

    JLabel lblNewLabel_3 = new JLabel("Item Number");
    lblNewLabel_3.setForeground(Color.WHITE);
    lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14));
    layeredPane.setLayer(lblNewLabel_3, 2);
    lblNewLabel_3.setBounds(151, 103, 116, 22);
    layeredPane.add(lblNewLabel_3);

    
    JTextField idNumField = new JTextField(" ");
    idNumField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(idNumField, 2);
    idNumField.setBounds(266, 107, 391, 23);
    layeredPane.add(idNumField);
    idNumField.setColumns(10);
    
    
    
    
    }
    
    inputFrame.getContentPane( ).setLayout(groupLayout);
    
    
    }
}
        

All I'm looking to do is have a inventory form and have that "jtextField" input saved to text file for later recall. On the save buttons click I need it to save the text fields to file as a form. Please point me in the right direction, any advise would be very appreciated. Ive tried just about everything online and within my books and I could not find the solution. PS I am using Eclipse IDE



public class inputFrameNXA implements ActionListener {
    
    

    public static void main(String[ ] args) 
    {

       
      
     //Contructs Frame 
     JFrame inputFrame = new JFrame( );   
     inputFrame.setBounds(100, 100, 740, 439);
     inputFrame.setVisible(true);
     inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLayeredPane layeredPane = new JLayeredPane();
    GroupLayout groupLayout = new GroupLayout(inputFrame.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                .addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 724, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addComponent(layeredPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
    );

    {
    JLabel bckGndLbl = new JLabel("");
    layeredPane.setLayer(bckGndLbl, 1);
    bckGndLbl.setBounds(-23, -244, 1212, 894);
    layeredPane.add(bckGndLbl);
}
    {
    JButton newBtn = new JButton( );
    layeredPane.setLayer(newBtn, 2);
    newBtn.setBounds(10, 43, 89, 23);
    newBtn.setToolTipText("New Entry");
    newBtn.addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e)
            {
            
            }
            });
    
    layeredPane.add(newBtn);
    
    //Buttons Word Over Background  
    JLabel newLtrs = new JLabel("New");
      newLtrs.setFont(new Font("Takoma", Font.BOLD, 11));
      newLtrs.setHorizontalAlignment(SwingConstants.CENTER);
      layeredPane.setLayer(newLtrs, 3);
      newLtrs.setBounds(10, 43, 89, 23);
      layeredPane.add(newLtrs);
      //Set Action
     
      
      
      
     
    

    JButton delButton = new JButton( );
    layeredPane.setLayer(delButton, 2);
    delButton.setBounds(10, 72, 89, 23);
    delButton.setToolTipText("Delete Entry");
    delButton.addActionListener(new ActionListener( ) {
        public void actionPertformed(ActionEvent e)
        {
            actionPerformed( e );
        }

    });
      
     
    layeredPane.add(delButton);
    
    JLabel delLtrs = new JLabel("Delete");
    delLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
    delLtrs.setHorizontalAlignment(SwingConstants.CENTER);
    layeredPane.setLayer(delLtrs, 3);
    delLtrs.setBounds(10, 72, 89, 23);
    layeredPane.add(delLtrs);
    
    
    
    

    
  
    JButton saveBtn = new JButton( );
    layeredPane.setLayer(saveBtn, 2);
    saveBtn.setBounds(10, 102, 89, 23);
    saveBtn.setToolTipText("Save Entry");
    saveBtn.addActionListener(new ActionListener( ) {
        public void actionPertformed(ActionEvent e)
        {
            saveField( );
        }

        private void saveField() {
            
            
        }
    });
    
    layeredPane.add(saveBtn);   
    //Action Method
    
    
    
    JLabel saveLtrs = new JLabel("Save");
    saveLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
    saveLtrs.setHorizontalAlignment(SwingConstants.CENTER);
    layeredPane.setLayer(saveLtrs, 3);
    saveLtrs.setBounds(10, 102, 89, 23);
    layeredPane.add(saveLtrs);
    
      
     


    
    



    JLabel lblNewLabel_1 = new JLabel("Inventory Item");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
    lblNewLabel_1.setForeground(Color.WHITE);
    layeredPane.setLayer(lblNewLabel_1, 2);
    lblNewLabel_1.setBounds(151, 43, 116, 23);
    layeredPane.add(lblNewLabel_1);
    

    JTextField invField = new JTextField(" ");
    invField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(invField, 2);
    invField.setBounds(266, 39, 391, 23);
    layeredPane.add(invField);
    invField.setColumns(10);
    
    JLabel lblNewLabel_2 = new JLabel("Location");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
    lblNewLabel_2.setForeground(Color.WHITE);
    layeredPane.setLayer(lblNewLabel_2, 2);
    lblNewLabel_2.setBounds(151, 76, 116, 19);
    layeredPane.add(lblNewLabel_2);


    JTextField locationField = new JTextField(" ");
    locationField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(locationField, 2);
    locationField.setBounds(266, 73, 391, 23);
    layeredPane.add(locationField);
    locationField.setColumns(10);

    

    JLabel lblNewLabel_3 = new JLabel("Item Number");
    lblNewLabel_3.setForeground(Color.WHITE);
    lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14));
    layeredPane.setLayer(lblNewLabel_3, 2);
    lblNewLabel_3.setBounds(151, 103, 116, 22);
    layeredPane.add(lblNewLabel_3);

    
    JTextField idNumField = new JTextField(" ");
    idNumField.setBackground(Color.LIGHT_GRAY);
    layeredPane.setLayer(idNumField, 2);
    idNumField.setBounds(266, 107, 391, 23);
    layeredPane.add(idNumField);
    idNumField.setColumns(10);
    
    
    
    
    }
    
    inputFrame.getContentPane( ).setLayout(groupLayout);
    
    
    }
}
        

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-11 15:00:01

我在下面修改您的代码是更新的代码。要从输入中保存文本,您需要知道要在哪里写文本,然后需要获取文本;之后,将文本保存到文件中。我做的第一件事是使JTEXTFIELDS全局变量使我可以从另一种方法访问它们,然后创建一个方法(Savetofile)以从JTEXFIELDS获取文本并将其保存到文件中。

    private static JTextField invField;
private static JTextField locationField;
private static JTextField idNumField;

public static void main(String[] args) {
    //Contructs Frame
    JFrame inputFrame = new JFrame();
    inputFrame.setBounds(100, 100, 740, 439);
    inputFrame.setVisible(true);
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLayeredPane layeredPane = new JLayeredPane();
    GroupLayout groupLayout = new GroupLayout(inputFrame.getContentPane());
    groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(GroupLayout.Alignment.TRAILING, groupLayout.createSequentialGroup()
                            .addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 724, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(layeredPane, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
    );

    {
        JLabel bckGndLbl = new JLabel("");
        layeredPane.setLayer(bckGndLbl, 1);
        bckGndLbl.setBounds(-23, -244, 1212, 894);
        layeredPane.add(bckGndLbl);
    }
    {
        JButton newBtn = new JButton();
        layeredPane.setLayer(newBtn, 2);
        newBtn.setBounds(10, 43, 89, 23);
        newBtn.setToolTipText("New Entry");
        newBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        layeredPane.add(newBtn);

        //Buttons Word Over Background
        JLabel newLtrs = new JLabel("New");
        newLtrs.setFont(new Font("Takoma", Font.BOLD, 11));
        newLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(newLtrs, 3);
        newLtrs.setBounds(10, 43, 89, 23);
        layeredPane.add(newLtrs);
        //Set Action


        JButton delButton = new JButton();
        layeredPane.setLayer(delButton, 2);
        delButton.setBounds(10, 72, 89, 23);
        delButton.setToolTipText("Delete Entry");
        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });


        layeredPane.add(delButton);

        JLabel delLtrs = new JLabel("Delete");
        delLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
        delLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(delLtrs, 3);
        delLtrs.setBounds(10, 72, 89, 23);
        layeredPane.add(delLtrs);

        JButton saveBtn = new JButton();
        layeredPane.setLayer(saveBtn, 2);
        saveBtn.setBounds(10, 102, 89, 23);
        saveBtn.setToolTipText("Save Entry");
        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveToFile();
            }
        });

        layeredPane.add(saveBtn);
        //Action Method

        JLabel saveLtrs = new JLabel("Save");
        saveLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
        saveLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(saveLtrs, 3);
        saveLtrs.setBounds(10, 102, 89, 23);
        layeredPane.add(saveLtrs);

        JLabel lblNewLabel_1 = new JLabel("Inventory Item");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setForeground(Color.WHITE);
        layeredPane.setLayer(lblNewLabel_1, 2);
        lblNewLabel_1.setBounds(151, 43, 116, 23);
        layeredPane.add(lblNewLabel_1);

        invField = new JTextField(" ");
        invField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(invField, 2);
        invField.setBounds(266, 39, 391, 23);
        layeredPane.add(invField);
        invField.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Location");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_2.setForeground(Color.WHITE);
        layeredPane.setLayer(lblNewLabel_2, 2);
        lblNewLabel_2.setBounds(151, 76, 116, 19);
        layeredPane.add(lblNewLabel_2);

        locationField = new JTextField(" ");
        locationField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(locationField, 2);
        locationField.setBounds(266, 73, 391, 23);
        layeredPane.add(locationField);
        locationField.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("Item Number");
        lblNewLabel_3.setForeground(Color.WHITE);
        lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14));
        layeredPane.setLayer(lblNewLabel_3, 2);
        lblNewLabel_3.setBounds(151, 103, 116, 22);
        layeredPane.add(lblNewLabel_3);

        idNumField = new JTextField(" ");
        idNumField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(idNumField, 2);
        idNumField.setBounds(266, 107, 391, 23);
        layeredPane.add(idNumField);
        idNumField.setColumns(10);
    }
    inputFrame.getContentPane().setLayout(groupLayout);
}

private static void saveToFile() {
    try {
        //Path to the file
        Path path = Paths.get("add the location of the file");

        //Check to see if it exists, if it doesn't create it
        if (!Files.exists(path)) {
            Files.createFile(path);
            System.out.println("Just created file");
        }

        //Get texts from the input fields
        String invFieldStr = invField.getText();
        String locationFieldText = locationField.getText();
        String idNumFieldText = idNumField.getText();

        //Add input fields value in an HTML form
        String inputFormStr = "<form>" +
                "<label>" + invFieldStr + "</label>" +
                "<label>" + locationFieldText + "</label>" +
                "<label>" + idNumFieldText + "</label>" +
                "</form>";

        //Save input form string to file
        Files.write(path, inputFormStr.getBytes(StandardCharsets.UTF_8));

    } catch (IOException e) {
        System.out.println("Error: " + e.getMessage());
    }
}

I revise your code a bit below is the updated code. To save a text from an input you'll need to know where you want the text to be written, then you'll need to get the text; after that, save the text to the file. The first thing I did was, made the JTextFields global variables so I could access them from another method, then create a method (saveToFile) to get the text from the JTexFields and save it to a file.

    private static JTextField invField;
private static JTextField locationField;
private static JTextField idNumField;

public static void main(String[] args) {
    //Contructs Frame
    JFrame inputFrame = new JFrame();
    inputFrame.setBounds(100, 100, 740, 439);
    inputFrame.setVisible(true);
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLayeredPane layeredPane = new JLayeredPane();
    GroupLayout groupLayout = new GroupLayout(inputFrame.getContentPane());
    groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(GroupLayout.Alignment.TRAILING, groupLayout.createSequentialGroup()
                            .addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 724, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(layeredPane, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
    );

    {
        JLabel bckGndLbl = new JLabel("");
        layeredPane.setLayer(bckGndLbl, 1);
        bckGndLbl.setBounds(-23, -244, 1212, 894);
        layeredPane.add(bckGndLbl);
    }
    {
        JButton newBtn = new JButton();
        layeredPane.setLayer(newBtn, 2);
        newBtn.setBounds(10, 43, 89, 23);
        newBtn.setToolTipText("New Entry");
        newBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        layeredPane.add(newBtn);

        //Buttons Word Over Background
        JLabel newLtrs = new JLabel("New");
        newLtrs.setFont(new Font("Takoma", Font.BOLD, 11));
        newLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(newLtrs, 3);
        newLtrs.setBounds(10, 43, 89, 23);
        layeredPane.add(newLtrs);
        //Set Action


        JButton delButton = new JButton();
        layeredPane.setLayer(delButton, 2);
        delButton.setBounds(10, 72, 89, 23);
        delButton.setToolTipText("Delete Entry");
        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });


        layeredPane.add(delButton);

        JLabel delLtrs = new JLabel("Delete");
        delLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
        delLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(delLtrs, 3);
        delLtrs.setBounds(10, 72, 89, 23);
        layeredPane.add(delLtrs);

        JButton saveBtn = new JButton();
        layeredPane.setLayer(saveBtn, 2);
        saveBtn.setBounds(10, 102, 89, 23);
        saveBtn.setToolTipText("Save Entry");
        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveToFile();
            }
        });

        layeredPane.add(saveBtn);
        //Action Method

        JLabel saveLtrs = new JLabel("Save");
        saveLtrs.setFont(new Font("Tahoma", Font.BOLD, 11));
        saveLtrs.setHorizontalAlignment(SwingConstants.CENTER);
        layeredPane.setLayer(saveLtrs, 3);
        saveLtrs.setBounds(10, 102, 89, 23);
        layeredPane.add(saveLtrs);

        JLabel lblNewLabel_1 = new JLabel("Inventory Item");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setForeground(Color.WHITE);
        layeredPane.setLayer(lblNewLabel_1, 2);
        lblNewLabel_1.setBounds(151, 43, 116, 23);
        layeredPane.add(lblNewLabel_1);

        invField = new JTextField(" ");
        invField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(invField, 2);
        invField.setBounds(266, 39, 391, 23);
        layeredPane.add(invField);
        invField.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Location");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_2.setForeground(Color.WHITE);
        layeredPane.setLayer(lblNewLabel_2, 2);
        lblNewLabel_2.setBounds(151, 76, 116, 19);
        layeredPane.add(lblNewLabel_2);

        locationField = new JTextField(" ");
        locationField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(locationField, 2);
        locationField.setBounds(266, 73, 391, 23);
        layeredPane.add(locationField);
        locationField.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("Item Number");
        lblNewLabel_3.setForeground(Color.WHITE);
        lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14));
        layeredPane.setLayer(lblNewLabel_3, 2);
        lblNewLabel_3.setBounds(151, 103, 116, 22);
        layeredPane.add(lblNewLabel_3);

        idNumField = new JTextField(" ");
        idNumField.setBackground(Color.LIGHT_GRAY);
        layeredPane.setLayer(idNumField, 2);
        idNumField.setBounds(266, 107, 391, 23);
        layeredPane.add(idNumField);
        idNumField.setColumns(10);
    }
    inputFrame.getContentPane().setLayout(groupLayout);
}

private static void saveToFile() {
    try {
        //Path to the file
        Path path = Paths.get("add the location of the file");

        //Check to see if it exists, if it doesn't create it
        if (!Files.exists(path)) {
            Files.createFile(path);
            System.out.println("Just created file");
        }

        //Get texts from the input fields
        String invFieldStr = invField.getText();
        String locationFieldText = locationField.getText();
        String idNumFieldText = idNumField.getText();

        //Add input fields value in an HTML form
        String inputFormStr = "<form>" +
                "<label>" + invFieldStr + "</label>" +
                "<label>" + locationFieldText + "</label>" +
                "<label>" + idNumFieldText + "</label>" +
                "</form>";

        //Save input form string to file
        Files.write(path, inputFormStr.getBytes(StandardCharsets.UTF_8));

    } catch (IOException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文