以下代码用于创建折线图。我能够在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);
}
});
}
}
答案 0 :(得分:4)
没有JTextArea
,使用构造函数JTextArea(int rows, int columns)
将JTextArea
放入JScrollPane
,此JScrollPane
看法或EAST
或WEST
区域
将JPanel
与JButton
放在SOUTH
或NORTH
区域
如果ChartPanel
没有返回任何PrefferedSize
(我怀疑),或者此值不正确,则设置为自己的PrefferedSize
,放{{1}到ChartPanel
区域
如您所见,CENTER
的任何代码行,setSize
已在API中实施JFrame
使用BorderLayout
代替pack()
setSize()
setLocation()
(您的代码逻辑)中的最后一行代码必须是
frame.validate(); frame.repaint(); frame.pack();
ActionListener
Swing GUI
FileIO
期间Mouse and Key Events
对FileIO
不负责任,将Runnable#Tread
包裹到SwingWorker
或使用{{li> {1}}用于将此长而艰巨的任务重定向到后台任务
创建ChartPanel
作为本地变量(JFram
e),ActionListener
中的第一个代码行必须
frame.remove(ChartPanel);
答案 1 :(得分:3)
两个显示两个组件,您可以使用JSplitPane。
将数据加载到数组中时会将其附加到JTextArea(示例代码中的area1)
final JTextArea area1 = new JTextArea();
...
area1.append("X=" + a[i] + " Y=" + b[i] + "\n");
然后,不是将图表添加到框架中,而是将Chart和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
。