如何将文件内容打印到GUI

时间:2015-11-13 14:13:10

标签: java swing user-interface

对于我的程序,我们要做的主要事情之一是将.csv文件的内容打印到GUI。我已经找到了GUI和排序方法,但是我们无法弄清楚如何将文件实际打印到GUI。关于如何做到这一点的任何想法?以下是GUI的代码:

 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.IOException;

 import javax.swing.*;
 public class FirstGUI2 {
 private static JTextArea textAreaMon;
 private static JTextArea textAreaTue;
 private static JTextArea textAreaWed;
 private static JTextArea textAreaThu;
 private static JTextArea textAreaFri;
 private static JTextArea description;
 private static String gE1 = "Group Exam 1";
 private static String gE2 = "Group Exam 2";
 private static String gE3 = "Group Exam 3";
 private static String gE4 = "Group Exam 4";
 private static String gE5 = "Group Exam 5";
 private static String gE6 = "Group Exam 6";
 private static String gE7 = "Group Exam 7";
 private static String gE8 = "Group Exam 8";
 private static String gE9 = "Group Exam 9";
 private Container pane;
 final static boolean fill = true;
 final static boolean weightX = true;
 final static boolean RIGHT_TO_LEFT = false;
 public static String mon = "";
 public static String tues = "";
 public static String wed = "";
 public static String thu = "";
 public static String fri = "";

 public static void addComponents(Container pane) {
    if (RIGHT_TO_LEFT)
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;

    button = new JButton("Open File");
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(5, 5, 5, 5);
    pane.add(button, c);

    textAreaMon = new JTextArea(mon);
    //textAreaMon.append(mon.substring(0));
    textAreaMon.setLineWrap(true);
    textAreaMon.setEditable(false);
    c.gridx = 1;
    c.gridy = 2;
    pane.add(textAreaMon, c);


    textAreaTue = new JTextArea(tues);
    textAreaTue.setLineWrap(true);
    textAreaTue.setEditable(false);
    c.gridx = 1;
    c.gridy = 3;
    pane.add(textAreaTue, c);

    textAreaWed = new JTextArea(wed);
    textAreaWed.setLineWrap(true);
    textAreaWed.setEditable(false);
    c.gridx = 1;
    c.gridy = 4;
    pane.add(textAreaWed, c);

    textAreaThu = new JTextArea(thu);
    textAreaThu.setLineWrap(true);
    textAreaThu.setEditable(false);
    c.gridx = 1;
    c.gridy = 5;
    pane.add(textAreaThu, c);

    textAreaFri = new JTextArea(fri);
    textAreaFri.setLineWrap(true);
    textAreaFri.setEditable(false);
    c.gridx = 1;
    c.gridy = 6;
    pane.add(textAreaFri, c);

    description = new JTextArea(
            "Click the open button in order to select and import your .csv       file. Your scehdule and times will be displayed in the schedule below.");
    description.setEditable(false);
    c.gridx = 1;
    c.gridy = 0;

    pane.add(description, c);

    // attaching the file opener to the open file button
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            new InterfacePanel();
        }
    });
 }


//creates the frame and showing the GUI to the user
 public static void makeGUI(){
    JFrame frame = new JFrame("Final Exam Scheduler");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponents(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
 }

 public static void printToGUI(){
    //JText Fields

    //textAreaMon
    mon= "Monday: \n \n 8:00 - 10:00:\t Group Exam 1 \n 10:15-12:15:\t Group    Exam 2 \n 12:45 - 2:45:\t";
    while(!ArrayListsForClasses.time1.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time1.size(); i++){
            mon += ArrayListsForClasses.time1.get(i);
            mon += ", ";
        }
    }
    mon += "\n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time2.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time2.size(); i++){
            mon += ArrayListsForClasses.time2.get(i);
            mon += ", ";
        }
    }
    mon += "\n 5:30 - 7:30:\t Group Exam 3 \n \n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time3.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time3.size(); i++){
            mon += ArrayListsForClasses.time3.get(i);
            mon += ", ";
        }
    }
    mon+= "\n \n";


    // textAreaTues
    tues = "Tuesday: \n \n 8:00 - 10:00: \t";
    while (!ArrayListsForClasses.time4.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time4.size(); i++) {
            tues += ArrayListsForClasses.time4.get(i);
            tues += ", ";
        }
    }
    tues += "\n 10:15 - 12:15:\t Group Exam 4 \n \n 12:45 - 2:45:\t ";
    while (!ArrayListsForClasses.time5.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time5.size(); i++) {
            tues += ArrayListsForClasses.time5.get(i);
            tues += ", ";
        }
    }
    tues += "\n 3:00 - 5:00:\t ";
    while (!ArrayListsForClasses.time6.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time6.size(); i++) {
            tues += ArrayListsForClasses.time6.get(i);
            tues += ", ";
        }
    }
    tues += "\n 5:30 - 7:30:\t ";
    while (!ArrayListsForClasses.time7.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time7.size(); i++) {
            tues += ArrayListsForClasses.time7.get(i);
            tues += ", ";
        }
    }
    tues += "\n 7:45 - 9:45:\t ";
    while (!ArrayListsForClasses.time8.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time8.size(); i++) {
            tues += ArrayListsForClasses.time8.get(i);
            tues += ", ";
        }
    }

    //TextAreaWed
    wed = "Wednesday \n \n 8:00 - 10:00:\t";
    while(!ArrayListsForClasses.time9.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time9.size(); i++) {
            wed += ArrayListsForClasses.time9.get(i);
            wed += ", ";
        }
    }
    wed += "\n 10:15-12:15:\t";
    while(!ArrayListsForClasses.time10.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time10.size(); i++) {
            wed += ArrayListsForClasses.time10.get(i);
            wed += ", ";
        }
    }
    wed += "\n 12:45 - 2:45 :\t Group Exam 5 \n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time11.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time11.size(); i++) {
            wed += ArrayListsForClasses.time11.get(i);
            wed += ", ";
        }
    }
    wed += "\n 5:30 - 7:30:\t";
    while(!ArrayListsForClasses.time12.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time12.size(); i++) {
            wed += ArrayListsForClasses.time12.get(i);
            wed += ", ";
        }
    }
    wed += "\n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time13.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time13.size(); i++) {
            wed += ArrayListsForClasses.time13.get(i);
            wed += ", ";
        }
    }

    //TextAreaThurs
    thu = "Thursday \n \n 8:00 - 10:00:\t";
    while(!ArrayListsForClasses.time14.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time14.size(); i++) {
            thu += ArrayListsForClasses.time14.get(i);
            thu += ", ";
        }
    }
    thu += "\n 10:15-12:15 :\t Group Exam 6 \n 12:45 - 2:45:\t";
    while(!ArrayListsForClasses.time15.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time15.size(); i++) {
            thu += ArrayListsForClasses.time15.get(i);
            thu += ", ";
        }
    }
    thu += "\n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time16.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time16.size(); i++) {
            thu += ArrayListsForClasses.time16.get(i);
            thu += ", ";
        }
    }
    thu += "\n 5:30 - 7:30:\t Group Exam 7 \n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time17.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time17.size(); i++) {
            thu += ArrayListsForClasses.time17.get(i);
            thu += ", ";
        }
    }

    //TextAreaFri
    fri = "Friday \n \n 8:00 - 10:00:\t Group Exam 8 \n 10:15-12:15:\t";
    while(!ArrayListsForClasses.time18.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time18.size(); i++) {
            fri += ArrayListsForClasses.time18.get(i);
            fri += ", ";
        }
    }
    fri += "\n 12:45 - 2:45:\t Group Exam 9";

 }

 public static void main(String[] args){
     javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            makeGUI();
        }
     });

  }

}

3 个答案:

答案 0 :(得分:1)

  

我已经找到了GUI

实际上,你的代码设计都是错误的。您不应该使用静态变量和静态方法。通常,唯一的静态方法应该是创建GUI的方法。所有其他应用程序代码都应该在类中定义,因此任何方法都可以访问类的变量。

  

我们要做的是将.csv文件的内容打印到GUI

对我来说,&#34; print&#34;意味着使用打印机将东西打印到一张纸上。我猜你真的想在GUI中显示文件的内容。

下面是一个简单的例子,展示了如何读写文件。文件的内容将显示在文本区域中。请注意,在读取文件之前,您需要先写入文件。

我建议你从这个基本代码开始并重写你的类,这样你就可以摆脱所有的静态变量和方法:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

class TextAreaLoad extends JPanel
{
    private JTextArea edit;

    public TextAreaLoad()
    {
        setLayout( new BorderLayout() );

        edit = new JTextArea(30, 60);
        add(new JScrollPane(edit), BorderLayout.NORTH);

        JButton read = new JButton("Read TextAreaLoad.txt");
        read.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileReader reader = new FileReader( "TextAreaLoad.txt" );
                    BufferedReader br = new BufferedReader(reader);
                    edit.read( br, null );
                    br.close();
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        add(read, BorderLayout.LINE_START);

        JButton write = new JButton("Write TextAreaLoad.txt");
        write.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileWriter writer = new FileWriter( "TextAreaLoad.txt" );
                    BufferedWriter bw = new BufferedWriter( writer );
                    edit.write( bw );
                    bw.close();
                    edit.setText("");
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        add(write, BorderLayout.LINE_END);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TextArea Load");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextAreaLoad());
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

答案 1 :(得分:0)

您尚未使用导入的数据将String值设置为TextAreas。你做了什么,给JTextAreas一个等于mon字符串的初始值的值,如下所示:

textAreaMon = new JTextArea(mon);

但是,这并不意味着如果mon更新,textAreaMon将会更新;它在视图初始化时非常重要。

向文件导入按钮侦听器添加方法调用,其中包含执行类似此操作的方法

textAreaMon.setText(mon); 

每次导入文件时都应该正确更新GUI。

答案 2 :(得分:0)

我可能错了,但在actionPerformed()方法中,您应该编写以下代码

 String myCsvFile = "mySCVFile.csv"; //you can modify this based on where the file is located 
 BufferedReader br = null;  
 String line = "";  

 try {  

   br = new BufferedReader(new FileReader(myCsvFile));  
   while ((line = br.readLine()) != null) {  
     String[] columns = line.split(",");  //you may do extra tweeks in here based on how you want to display the data
     for(int x=0; x <columns.length(); x++){
         description.append(columns[x]);  //I guess this is the textarea you want to display the content of the CSV file

     }

   }  

 } catch (FileNotFoundException e) {  
  e.printStackTrace();  
 } catch (IOException e) {  
  e.printStackTrace();  
 } finally {  
  if (br != null) {  
    try {  
      br.close();  
    } catch (IOException e) {  
      e.printStackTrace();  
    }  
  }  
}