链接输入的文本信息

时间:2011-03-25 11:50:13

标签: java eclipse

现在我有一个GUI,当我点击Add Customer时弹出一个框,我可以输入名称,地址等信息。完成后,我单击添加客户,它将信息添加到.txt文件。现在在原始GUI上有一个“刷新”按钮,当按下它时,它将信息输入到名为“NameTextCustomers”的文本框中。它没有这样做。我已经研究了几个小时试图解决它。我在代码中有一行说“NameTextCustomers.setText(”File Info Here“);”点击刷新按钮后,“File Info Here”显示出来,因此该部分有效。我假设代码将插入那里。我认为这是我必须要做的最后一部分,让我的代码以我想要的方式工作。

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.util.List;

public class FVolume extends JFrame implements ActionListener{
    private JTabbedPane jtabbedPane;

    private JPanel Customers;

    private List<Customer> customers = new ArrayList<Customer>();
    JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors, ExistTextContractors;
    JTextField lengthTextPool, widthTextPool, depthTextPool, volumeTextPool;

    public FVolume(){
        setTitle("Volume Calculator");
        setSize (300, 200);

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        createCustomers();

        jtabbedPane = new JTabbedPane();

        jtabbedPane.addTab("Customer", Customers);

        topPanel.add(jtabbedPane, BorderLayout.CENTER);
    }

        /*      CREATE CUSTOMERS    */

    public JPanel createCustomers(){
        Customers = new JPanel();
        Customers.setLayout(null);

        NameTextCustomers = new JTextArea("Select Add Customer to Add Customer.       Select Refresh to Refresh This Pane.");
        NameTextCustomers.setLineWrap(true);
        NameTextCustomers.setBounds(10, 10, 390, 150);
        Customers.add(NameTextCustomers);

        JButton Exit = new JButton("Exit");
        Exit.setBounds(30,170,80,20);
        Exit.addActionListener(this);
        Exit.setBackground(Color.white);
        Customers.add(Exit);

        JButton AddCustomers = new JButton("Add Customer");
        AddCustomers.setBounds(130,170,120,20);
        AddCustomers.setBackground(Color.white);
        Customers.add(AddCustomers);

        JButton Refresh = new JButton("Refresh");
        Refresh.setBounds(260,170,80,20);
        Refresh.setBackground(Color.white);
        Customers.add(Refresh);

        ExistTextCustomers = new JTextArea();
        ExistTextCustomers.setBounds(10, 200, 390, 60);
        ExistTextCustomers.setLineWrap(true);
        Customers.add(ExistTextCustomers);

        final JTextArea custArea = new JTextArea(6, 30); 
        final JTextArea ExistTextCustomers; 

        ExistTextCustomers = new JTextArea(2, 30); 
        ExistTextCustomers.setLineWrap(true); 
        ExistTextCustomers.setWrapStyleWord(true); 

        AddCustomers.addActionListener(new ActionListener() 
        {   
            public void actionPerformed(ActionEvent e) 
            { 
            Customers.add(new Customer("Customer")); 
        } 
            }); 

        Customers.add(custArea);
        Customers.add(AddCustomers);

        Customers.add(Refresh);
        Refresh.setMnemonic('R');

        Refresh.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                NameTextCustomers.setText ("File Info Here");
        try
        {
            File custOpen = new File("customer.txt"); 
            FileReader custAreaIn = new FileReader(custOpen); 
            custArea.read(custAreaIn, custOpen.getAbsolutePath()); 
            ExistTextCustomers.setText("File exists and can be read.");
        }
        catch (IOException e3){
            ExistTextCustomers.setText("The file could not be read." + e3.getMessage());
            }
        }
    }
    );
        return Customers;
}
class Customer extends JFrame
{
    private String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
            "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", 
            "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
            "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
            "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"}; 
            private JComboBox StateList = new JComboBox(states); 
            private JTextField NameText = new JTextField(25); 
            private JTextField AddressText = new JTextField(25); 
            private JTextField CityText = new JTextField(25); 
            private JTextField ZipText = new JTextField(9); 
            private JTextField PhoneText = new JTextField(10); 
            private JTextField ExistTextCustomers = new JTextField(30); 
            private static final long serialVersionUID = 1L; 

            private AddCustButtonHandler addCusHandler = new AddCustButtonHandler(); 

            public Customer(String who) 
            {
                popUpWindow (who);
            }
            public void popUpWindow(final String who) {
                final JFrame popWindow; 
                popWindow = new JFrame(who); 
                popWindow.setSize(425, 350); 
                popWindow.setLocation(100, 100); 
                popWindow.setVisible(true); 
                setDefaultCloseOperation(EXIT_ON_CLOSE); 

                Container c = new Container(); 

                popWindow.add(c); 

                c.setLayout(new FlowLayout()); 

                JPanel one = new JPanel(); 
                JPanel two = new JPanel(); 
                JPanel three = new JPanel(); 
                JPanel four = new JPanel(); 
                JPanel five = new JPanel(); 
                JPanel six = new JPanel(); 

                one.add(new JLabel(who + " Name ")); 
                one.add(NameText); 
                two.add(new JLabel("Address ")); 
                two.add(AddressText); 
                three.add(new JLabel("City ")); 
                three.add(CityText); 
                four.add(new JLabel("State ")); 
                StateList.setSelectedIndex(0); 
                four.add(StateList); 
                four.add(new JLabel("ZIP")); 
                four.add(ZipText); 
                four.add(new JLabel("Phone")); 
                four.add(PhoneText); 
                JButton addwho = new JButton("Add " + who); 
                addwho.setMnemonic('A'); 
                JButton close = new JButton("Exit"); 
                close.setMnemonic('C'); 
                JButton deleteFile = new JButton("Delete File"); 
                deleteFile.setMnemonic('D'); 
                five.add(addwho); 
                five.add(close); 
                five.add(deleteFile); 
                ExistTextCustomers.setEditable(false); 
                ExistTextCustomers.setHorizontalAlignment(JTextField.CENTER); 

                six.add(ExistTextCustomers); 
                c.add(one); 
                c.add(two); 
                c.add(three); 
                c.add(four); 
                c.add(five); 
                c.add(six); 

                deleteFile.setToolTipText("Delete File"); 
                addwho.setToolTipText("Add "+ who); 
                close.setToolTipText("Exit");

                if (who == "Customer")
                    addwho.addActionListener(addCusHandler);
                close.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        NameText.setText(""); 
                        AddressText.setText(""); 
                        CityText.setText(""); 
                        ZipText.setText(""); 
                        PhoneText.setText(""); 
                        ExistTextCustomers.setText(""); 
                        popWindow.dispose();
                    }
                }
                );
                deleteFile.addActionListener(new ActionListener() { 
                    public void actionPerformed(ActionEvent e) {
                        ExistTextCustomers.setText("");
                        if (who.equals("Customer")) {
                            File file = new File("Customer.txt"); 
                            boolean cusFileDeleted = file.delete(); 
                            if (cusFileDeleted) {
                                ExistTextCustomers 
                                .setText("Customer file has been deleted"); 
                            } else {
                                ExistTextCustomers 
                                .setText("There was an error in deleting file"); 
                            }
                            }
                        }
                    }
                );
            }
            class AddCustButtonHandler implements ActionListener { 
                public void actionPerformed(ActionEvent addCusHandler) { 
                    int StateIndex; 
                    try { 
                        File file = new File("Customer.txt");
                        boolean success = file.createNewFile();
                        if (success) {
                            ExistTextCustomers
                            .setText("Customer.txt file created file added");
                        } else if (file.canWrite()) {
                            ExistTextCustomers
                            .setText("Writing data to Customer.txt, file added");
                        } else {
                            ExistTextCustomers.setText("Cannot create file: Customer.txt");
                        }
                        try {
                            FileWriter fileW = new FileWriter("Customer.txt", true);
                            fileW.write(NameText.getText());
                            fileW.write(",");
                            fileW.write(AddressText.getText());
                            fileW.write(",");
                            fileW.write(CityText.getText());
                            fileW.write(",");
                            StateIndex = StateList.getSelectedIndex();
                            fileW.write(states[StateIndex]);
                            fileW.write(",");
                            fileW.write(ZipText.getText());
                            fileW.write(",");
                            fileW.write(PhoneText.getText());
                            fileW.write("\r\n");
                            fileW.close();
                            ExistTextCustomers.setText("A new Customer has been added!");
                            FileReader fileR = new FileReader("Customer.txt");
                            BufferedReader buffIn = new BufferedReader(fileR);
                            String textData = buffIn.readLine();
                            buffIn.close(); 

                        }
                        catch (IOException e1) { 
                            JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2);
                        }
                        NameText.setText(""); 
                        AddressText.setText(""); 
                        CityText.setText(""); 
                        ZipText.setText(""); 
                        PhoneText.setText(""); 
                        }
                    catch (IOException e1) { 
                    }

                }
            }
            public void actionPerformed(ActionEvent event){
            }
                private void Exit_pressed(){
                    System.exit(0);
                }
            }


                public static void main(String[] args){
                    JFrame frame = new FVolume();
                    frame.setSize(420, 350);
                    frame.setVisible(true);
    }

                public void actionPerformed(ActionEvent e) {


                }

}

Flow of events

单击“刷新”按钮后,应将信息输入文本框。

1 个答案:

答案 0 :(得分:1)

好的,找出你的代码的问题。花了一些时间,但并不是太复杂。 但首先,不要在文件中放置多于一个类,这是一种不好的做法(并且更难以完成代码。

问题是,在“客户”窗口中按“添加客户”后,您需要将该数据发送到主应用程序窗口。怎么做?

FVolume parent;添加到customer类并更改Customer类的构造函数,不仅要接收字符串,还要接收FVolume的对象:

public Customer(String who, FVolume _parent) 
    {
        popUpWindow (who);
        parent = _parent;
    }

您可以访问客户的主窗口。 现在您需要能够更改NameTextCustomers的文本,因此在FVolume类中创建一个允许从外部更改它的新方法:

public void setField(String str)
    {
        NameTextCustomers.setText(str);
        NameTextCustomers.validate();
    }

当有人在“Costumer”窗口中按“添加客户”时,接下来会调用此方法。你已经有了一个监听器,但让我们稍微更改它的actionPerformed方法,这样它不仅可以将String保存到你的文件中,还可以在NameTextCustomers中设置它。 (我没有把方法的最后一行,但没有改变)

public void actionPerformed(ActionEvent addCusHandler) { 
    int StateIndex; 
    try { 
        File file = new File("Customer.txt");
        StringBuilder sb = new StringBuilder();
        boolean success = file.createNewFile();
        if (success) {
            ExistTextCustomers
            .setText("Customer.txt file created file added");
        } else if (file.canWrite()) {
            ExistTextCustomers
            .setText("Writing data to Customer.txt, file added");
        } else {
            ExistTextCustomers.setText("Cannot create file: Customer.txt");
        }
        try {
            FileWriter fileW = new FileWriter("Customer.txt", true);
            sb.append(NameText.getText());
            sb.append(",");
            sb.append(AddressText.getText());
            sb.append(",");
            sb.append(CityText.getText());
            sb.append(",");
            StateIndex = StateList.getSelectedIndex();
            sb.append(states[StateIndex]);
            sb.append(",");
            sb.append(ZipText.getText());
            sb.append(",");
            sb.append(PhoneText.getText());
            sb.append("\r\n");
            fileW.write(sb.toString());
            parent.setField(sb.toString());
            fileW.close();
            ExistTextCustomers.setText("A new Customer has been added!");
            FileReader fileR = new FileReader("Customer.txt");
            BufferedReader buffIn = new BufferedReader(fileR);
            String textData = buffIn.readLine();
            buffIn.close(); 

唯一剩下的问题与Customer对象的构造有关。我们需要将FVolume对象添加到构造函数中,但是我们不应该将它添加到costumers面板,因为它是一个不同的框架:

AddCustomers.addActionListener(new ActionListener() 
        {   
            public void actionPerformed(ActionEvent e) 
            { 
            new Customer("Customer", FVolume.this); 
        } 
     }); 

这只能使它工作,我试图只在绝对必要的时候触摸代码,我没有尝试改变编码风格。