2

下面的代码用于创建折线图。我能够在 TextArea 中显示整数,但我无法在文本文件中显示整数上方的句子。我该如何纠正这个程序?

文本文件的屏幕截图如下所示。

我希望文本文件的前三行也打印在 TextArea 中。

在此处输入图像描述

import java.awt.BorderLayout;   
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

public class Pio {
  static JFrame frame1 = new JFrame("Graph");
      static String URL = null;
      static JTextArea txt = new JTextArea();
      static JPanel panel = new JPanel();
 public static void main(String[] args) throws IOException {

   JButton but = new JButton("Open file");

    ![enter image description here][2]

    panel.add(but);    


    frame1.add(panel, BorderLayout.WEST);

   frame1.setVisible(true);

frame1.setSize(1000,400);

    but.addActionListener(new ActionListener()

                        {


                         public void actionPerformed(ActionEvent e) 

                         {



                         JFileChooser chooser = new JFileChooser();

                         int ret = chooser.showDialog(null, "Open file");

                         if (ret == JFileChooser.APPROVE_OPTION)

                           {

                             File file = chooser.getSelectedFile();

                             URL = file.getAbsolutePath();

                           }

                         File f = new File(URL);

                            FileReader inputF = null;

                        try {

                    inputF = new FileReader(f);

                           }
                            catch (FileNotFoundException e1) {
                                                                e1.printStackTrace();

                                                 }

                            BufferedReader in = new BufferedReader(inputF);

                             int[] a = new int[100];

                                       int[] b = new int[100];

                                        String s=null;
                    try {

                                       s = in.readLine();

                                    } 

                                 catch (IOException e1) {

                                     e1.printStackTrace();

                                    }

                               int i = 0;

                             while (s != null && i < 100)

                                            { // your arrays can only hold 1000 ints

                                        String pair[] = s.split(" ");

                                        a[i] = Integer.parseInt(pair[0]);

                                        b[i] = Integer.parseInt(pair[1]);

                                            i++; // don't forget to increment

                                            try {

                                                s = in.readLine();

                                            }

                                          catch (IOException e1) {
                                                                                  e1.printStackTrace();
                                    }
                                }

                            try {

                                in.close();

                                    } 

                                      catch (IOException e1) {
                                                                           e1.printStackTrace();

                                }

                        System.out.println("the output of the file is " + f);

                            XYSeries data = new XYSeries("Line Graph");

                            int n = a.length;

                            for (int j = 0; j < n; j++) {

                                    data.add(a[j], b[j]);

                            }

                            XYDataset xY = new XYSeriesCollection(data);

                                 JFreeChart chart=ChartFactory.createXYLineChart(

                                      "line graph",

                                            "Cycles",

                                           "Temperature",

                                                xY,

                                           PlotOrientation.VERTICAL,

                                           true,

                                          true,

                                           true);

                               ChartPanel p=new ChartPanel(chart);

                                p.setVisible(true);

                                p.setLocation(100,100);

                                p.setSize(500,500);

                              frame1.add(p,BorderLayout.EAST);

                                         }
                        });
            }

}
4

2 回答 2

4
  1. 什么都没有JTextArea,使用构造函数JTextArea(int rows, int columns)

  2. put JTextAreato the JScrollPane, this JScrollPaneput 或 the EASTorWEST区域

  3. 放在或区域JPanel_ JButton_SOUTHNORTH

  4. 如果ChartPanel不返回任何PrefferedSize(我怀疑),或者此值不正确,则设置为 own PrefferedSize,放入ChartPanelCENTER区域

  5. 如您所见,任何有关的代码行setSizeJFrameBorderLayoutAPI中实现

  6. 使用pack()而不是setSize()

  7. setLocation()如果需要

  8. (您的代码逻辑)中的最后一行ActionListener代码必须是

    框架.验证();frame.repaint(); 框架.pack();

  9. 以这种形式(您的代码逻辑)Swing GUIFileIO不负责任的情况下Mouse and Key Events,包装FileIORunnable#TreadSwingWorker用于将这个漫长而艰巨的任务重定向到后台任务

  10. 创建ChartPanel为局部变量 (as JFrame) 和ActionListener必须的第一行代码

.

frame.remove(ChartPanel);
于 2012-06-11T10:49:08.107 回答
3

两个显示两个组件,您可以使用 JSplitPane。

将数据加载到数组中时,将其附加到 JTextArea(示例代码中的 area1)

final JTextArea area1 = new JTextArea();
...
area1.append("X=" + a[i] + " Y=" + b[i] + "\n");

然后,不要将图表添加到框架中,而是将图表和 TextArea 都添加到 JSplitPane

ChartPanel p = new ChartPanel(chart);
JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
frame1.add( splitpane , BorderLayout.CENTER );
splitpane.add(p);
splitpane.add(area1);
pane.validate();
frame1.validate();
frame1.repaint();  

使用中的 JSplitPane

附加数据的 JTextArea 使用

  area1.append("Creating a line graph\n");
  area1.append("Selected file " + f + "\n");
  while (s != null && i < 100) { // your arrays can only hold 1000 ints
    String pair[] = s.split(" ");
    try{
      a[i] = Integer.parseInt(pair[0]);
      b[i] = Integer.parseInt(pair[1]);
      area1.append("X=" + a[i] + " Y=" + b[i] + "\n");
    } catch (NumberFormatException e) {
       area1.append(s + "\n");
    }
    try {
      s = in.readLine();
      i++; // don't forget to increment
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
  area1.append("Finished reading data\n");
  ...

在此示例中,我假设标头将失败parseInt并且(如果确实如此)附加 String s

文本区域

于 2012-06-11T10:59:28.010 回答