java - 代码中的小错误

标签 java swing jfreechart actionlistener jfilechooser

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

下面给出了文本文件的屏幕截图。

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

enter image description here

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);

                                         }
                        });
            }

}

最佳答案

  1. 没有关于JTextArea的内容,使用构造函数JTextArea(int rows, int columns)

  2. JTextArea 放置到 JScrollPane 中,此 JScrollPane 放置或 EAST WEST地区

  3. JPanelJButton放在SOUTHNORTH区域

    <
  4. 如果 ChartPanel 未返回任何 PrefferedSize(我对此表示怀疑),或者该值不正确,则设置自己的 PrefferedSize ,将ChartPanel放到CENTER区域

  5. 正如您所看到的有关 setSize 的代码行,JFrame 已在 API 中实现了 BorderLayout

    <
  6. 使用pack()代替setSize()

  7. setLocation()(如果需要)

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

    frame.validate(); 框架.重绘(); 框架.pack();

  9. 在此形式(您的代码逻辑)中,FileIO 期间的 Swing GUI 不负责鼠标和按键事件,请包装 FileIORunnable#Tread 或使用 SwingWorker 将这个漫长而艰巨的任务重定向到后台任务

  10. 创建 ChartPanel 作为局部变量(如 JFrame),并且 ActionListener 中的第一个代码行必须为

.

frame.remove(ChartPanel);

关于java - 代码中的小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978264/

相关文章:

java - setSize() 不起作用?

java - JFreeChart XYPlot XYImageAnnotation 鼠标点击监听器

java - Hibernate ManyToMany - 只返回唯一元素

java - jms 队列上多个监听器的接收行为

java - 按钮未出现在框架中

java - 监听jtable变化

java - 更改 ListView 中单个 View 的外观

java - 使用Maven时如何获取资源的路径?

javascript - 如何使用开源图表 API 在 jsp 页面中创建可钻的条形图

image - 在 JFreeChart 的条形图上的 X 轴标签旁边添加图像