gpt4 book ai didi

java - jfree图表未加载

转载 作者:行者123 更新时间:2023-12-01 13:18:17 25 4
gpt4 key购买 nike

大家好

我有一项作业,需要制作一个条形图。我已经遵循了 youtube 上的教程。一切似乎都在编译,但我不知道为什么该图不会与我的 GUI 一起出现。这是代码:

public class NewjFrame extends javax.swing.JFrame {private JButton jButton1; 
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public NewJFrame() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("jButton1");
jButton1.setBounds(90, 19, 151, 28);
}
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
// create a dataset...
// Create a simple Bar chart
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "Profit", "Jane");
dataset.setValue(7, "Profit", "Tom");
dataset.setValue(8, "Profit", "Jill");
dataset.setValue(5, "Profit", "John");
dataset.setValue(12, "Profit", "Fred");
JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman",
"Salesman", "Profit", dataset, PlotOrientation.VERTICAL, false, true, false);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.red);
ChartFrame frame = new ChartFrame("Bar Chart", chart);
frame.setVisible(true);
frame.setSize(400,350);
}
}

PS,我是 java 新手,所以请放轻松

最佳答案

您需要向jButton1添加一个ActionListener

jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

您可以在实例化 jButton1 之后将其添加到 initGui() 中。

关于java - jfree图表未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com