最近我问了一个与此相似的问题,但没有给出足够的信息,我的代码太长了,无法进行代码审查。我写了一个新文件,其骨架与我上一篇文章相同。在这里,我再次遇到同样的问题。
当我尝试单击“显示图表”按钮时,出现以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.knowm.xchart.XChartPanel.<init>(XChartPanel.java:65)
at testMain$1.actionPerformed(testMain.java:78)"
这是我的代码:
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.style.PieStyler.AnnotationType;
import org.knowm.xchart.style.Styler.ChartTheme;
import org.knowm.xchart.style.Styler.LegendPosition;
public class testMain extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel gui, choiceBar,insertPanel;
private XChartPanel chartPanel;
private JButton showGraph;
private PieChart chart;
public testMain() {
this.setTitle("Test Frame");
this.setSize(500, 500);
JPanel gui = new JPanel(new BorderLayout());
//choice bar testing
choiceBar = new JPanel(new FlowLayout());
// Radio button group
JRadioButton halfhalf = new JRadioButton("Split half");
JRadioButton quarters = new JRadioButton("Split quarters");
ButtonGroup group1 = new ButtonGroup();
group1.add(halfhalf); group1.add(quarters);
//add to choice bar
choiceBar.add(halfhalf); choiceBar.add(quarters);
//Side panel for inserting user values
insertPanel = new JPanel();
GroupLayout groupLayout = new GroupLayout(insertPanel);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
//Display Button
JButton showGraph = new JButton("Show Graph");
JLabel showGraphLabel = new JLabel("Finish");
//JTextfield for inserting value
JTextField value = new JTextField("value");
JLabel valueLabel = new JLabel("Insert value here:");
// Grouping JTextFields
GroupLayout.SequentialGroup groupH = groupLayout.createSequentialGroup();
GroupLayout.SequentialGroup groupV = groupLayout.createSequentialGroup();
groupH.addGroup(groupLayout.createParallelGroup().addComponent(valueLabel).addComponent(showGraphLabel));
groupH.addGroup(groupLayout.createParallelGroup().addComponent(value).addComponent(showGraph));
groupLayout.setHorizontalGroup(groupH);
groupV.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(valueLabel).addComponent(value));
groupV.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(showGraphLabel).addComponent(showGraph));
groupLayout.setVerticalGroup(groupV);
showGraph.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String valueText = value.getText();
double valueAmount = Double.parseDouble(valueText);
if (halfhalf.isSelected()) {
chartPanel = new XChartPanel(chart);
chart = new PieChartBuilder().width(600).height(400).title("Split By").theme(ChartTheme.GGPlot2).build();
chart.getStyler().setLegendVisible(false);
chart.getStyler().setAnnotationType(AnnotationType.LabelAndPercentage);
chart.getStyler().setAnnotationDistance(1.15);
chart.getStyler().setPlotContentSize(.7);
chart.getStyler().setStartAngleInDegrees(90);
chart.addSeries("1", valueAmount/2);
chart.addSeries("2", valueAmount/2);
new SwingWrapper(chart).displayChart();
gui.add(chartPanel, BorderLayout.SOUTH);
gui.validate();
gui.repaint();
}
else {
chartPanel = new XChartPanel(chart);
chart = new PieChartBuilder().width(600).height(400).title("Split By").theme(ChartTheme.GGPlot2).build();
chart.getStyler().setLegendVisible(false);
chart.getStyler().setAnnotationType(AnnotationType.LabelAndPercentage);
chart.getStyler().setAnnotationDistance(1.15);
chart.getStyler().setPlotContentSize(.7);
chart.getStyler().setStartAngleInDegrees(90);
chart.addSeries("1",valueAmount/4);
chart.addSeries("2", valueAmount/4);
chart.addSeries("3", valueAmount/4);
chart.addSeries("4", valueAmount/4);
new SwingWrapper(chart).displayChart();
gui.add(chartPanel, BorderLayout.SOUTH);
gui.validate();
gui.repaint();
}
}
});
// add to gui
gui.add(choiceBar, BorderLayout.NORTH);
gui.add(insertPanel, BorderLayout.WEST);
this.add(gui);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
testMain frame = new testMain();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
}
`
我试图编写一个Java GUI,一旦用户点击该按钮,该图就显示在同一个GUI上。我仍然希望使用嵌套布局来实现它,因为我喜欢干净的外观。有人提到我可以使用cardLayout,但我觉得那太杂乱了。
谢谢,任何帮助都是适用的。