代码中的小故障 - java

以下代码用于创建折线图。我可以在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);

                                         }
                        });
            }

}

参考方案

两个显示两个componenet,您可以使用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();  

要附加数据,请使用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并(如果是的话)附加字符串s

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…