试图将.txt文件加载到JFrame(JFileChooser)

时间:2014-07-24 17:45:10

标签: java swing

所以我在大学时尝试进行JFrame分配,并且我正在尝试通过JFileChooser加载.txt文件,并让文件中的文本显示在此JFrame中的少数JTextField中。现在它不起作用,并且我从课堂讲座中得到的所有信息都是如此,我无法找到解决这个问题的方法。

所以这里是完整的代码,我正在尝试使用的具体部分是案例:“加载”部分。

package assignment3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class SocialMediaList extends JFrame {

private JPanel display;
private JLabel siteNameLabel;
private JTextField siteName;
private JLabel userIDLabel;
private JTextField userID;
private JLabel contactsLabel;
private JTextField contacts;

private JPanel btns;
private JButton loadBtn;
private JButton addBtn;
private JButton saveBtn;
private JButton exitBtn;

private JPanel list;
private JList myList;

private ArrayList<SocialMedia> mediaList;

public SocialMediaList() {

    mediaList = new ArrayList();
    myList = new JList(mediaList.toArray());

    this.setLayout(new GridLayout(2,1,5,5));

    display = new JPanel();
    display.setLayout(new GridLayout(5,2,5,5));

    siteNameLabel = new JLabel("  Site Name: ");
    siteName = new JTextField(10);
    userIDLabel = new JLabel("  User ID: ");
    userID = new JTextField(10);
    contactsLabel = new JLabel("  No. of Contacts: ");
    contacts = new JTextField(10);

    display.add(siteNameLabel);
    display.add(siteName);
    display.add(userIDLabel);
    display.add(userID);
    display.add(contactsLabel);
    display.add(contacts);

    this.add(display);

    btns = new JPanel();
    btns.setLayout(new GridLayout(1,3,5,5));

    loadBtn = new JButton("Load");
    loadBtn.setActionCommand("Load");
    addBtn = new JButton("Add");
    saveBtn = new JButton("Save");
    exitBtn = new JButton("Exit");

    btns.add(loadBtn);
    btns.add(addBtn);
    btns.add(saveBtn);
    btns.add(exitBtn);

    this.add(btns);

    list = new JPanel();
    list.setLayout(new FlowLayout());
    JScrollPane myScrollPane = new JScrollPane(myList);
    list.add(myScrollPane);

    setLayout(new BorderLayout());
    this.add(display,BorderLayout.NORTH);
    this.add(btns,BorderLayout.CENTER);
    this.add(list,BorderLayout.SOUTH);

    ButtonListeners buttonListener = new ButtonListeners();
    loadBtn.addActionListener(buttonListener);
    addBtn.addActionListener(buttonListener);
    saveBtn.addActionListener(buttonListener);
    exitBtn.addActionListener(buttonListener);
}

public static void main(String[] args) {
    SocialMediaList list = new SocialMediaList();
    list.setTitle("Assignment #3");
    list.setSize(600,350);
    list.setLocationRelativeTo(null);
    list.setVisible(true);
    list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class ButtonListeners implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand()) {

            case "Load":
                File file;
                try {

                    JFileChooser chooser = new JFileChooser();

                    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                        file = chooser.getSelectedFile();

                        Scanner input = new Scanner(file);

                        while (input.hasNextLine()) {
                            SocialMedia account = mediaList.get(myList.getSelectedIndex());
                            siteName.setText(account.getSiteName());
                            userID.setText(account.getUserID());
                            contacts.setText(String.valueOf(account.getContacts()));

                            myList.setListData(mediaList.toArray());
                            add(siteName);
                            add(userID);
                            add(contacts);

                            if (mediaList.size() > 0) {
                                myList.setSelectedIndex(0);
                            }
                        }


                    }

                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SocialMediaList.class.getName()).log(Level.SEVERE, null, ex);
                }


            case "Add": 
                String s = siteName.getText();
                String u = userID.getText();
                int c = Integer.valueOf(contacts.getText());
                SocialMedia mySocialMedia = new SocialMedia();
                mySocialMedia.setSiteName(s);
                mySocialMedia.setUserID(u);
                mySocialMedia.setContacts(c);

                mediaList.add(mySocialMedia);
                myList.setListData(mediaList.toArray());
                break;

            case "Save":
        try {
            FileWriter writer = new FileWriter("socialMedia2.txt");
            writer.write(mediaList.toString());
            writer.write(myList.toString());
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(SocialMediaList.class.getName()).log(Level.SEVERE, null, ex);
        }

            case "Exit":
                System.exit(0);
        }
    }
}

class ExitListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}
}

0 个答案:

没有答案