我创建了 2 个图表并将它们显示在一个框架中。现在我想添加图像图标。但是框架有问题。我知道我没有使用过任何框架。但是,有没有办法在没有 JFrame 变量的情况下添加图像或如何使用 JFrame 修改我的代码。它必须在一个窗口中显示 2 个图表
我的版本:xchart 3.8.7
*jfreechart 1-0.19*
*jserialcomm- 1.0.23*
错误如下:
Cannot resolve symbol frame
import org.knowm.xchart.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame {
public static void main(String[] args) {
// Create instances of your plotter classes
SerialPlotter plotter1 = new SerialPlotter();
SerialPlotterForSensor2 plotter2 = new SerialPlotterForSensor2();
// Get the charts from your plotters
XYChart chart1 = plotter1.getChart();
XYChart chart2 = plotter2.getChart();
// Set the same title for both charts
chart1.setTitle("Sensor 1");
chart2.setTitle("Sensor 2");
// Create a list of charts
List<XYChart> charts = new ArrayList<>();
charts.add(chart1);
charts.add(chart2);
// Display the list of charts in a matrix layout using the SwingWrapper class
SwingWrapper<XYChart> sw = new SwingWrapper<>(charts);
sw.displayChartMatrix();
ImageIcon image = new ImageIcon("img.png");
frame.setIconImage(image.getImage());//Error apperas here
// Create a timer to repaint the chart every second
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sw.repaintChart();
}
});
timer.start();
}
}
如果您想在一个窗口中显示两个图形,并且框架有问题,您只需在调用该方法的 SwingWrapper 类中添加 JFrame 即可
displayChartMatrix()
而不是:-
SwingWrapper<XYChart> sw = new SwingWrapper<>(charts);
sw.displayChartMatrix();
试试这个:-
SwingWrapper<XYChart> sw = new SwingWrapper<>(charts);
JFrame frame = sw.displayChartMatrix();
我们在其中创建 JFrame 变量
还在代码开头添加这些行:-
import org.knowm.xchart.XYChart;
import org.knowm.xchart.SwingWrapper;