这是一个显示饼图的程序。我也有一个按钮。但是我编写的程序创建了两个单独的框架,一个框架用于饼图,另一个用于按钮。如何将按钮和饼图放在同一个框架上?你能纠正我的程序吗?
import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class Pie{
public static void getFile(File f)
{
System.out.println("File is: "+f.getName());
}
public static void main(String[] args)
{
JPanel panel=new JPanel();
panel.setLayout(null);
JButton b=new JButton("Open File");
b.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();
getFile(file);
}
}
}
);
b.setBounds(50,40,100,35);
panel.add(b);
DefaultPieDataset pieDataset = new DefaultPieDataset();
ieDataset.setValue("A", new Integer(10));
pieDataset.setValue("B", new Integer(20));
pieDataset.setValue("C", new Integer(30));
pieDataset.setValue("D", new Integer(10));
pieDataset.setValue("E", new Integer(20));
pieDataset.setValue("F", new Integer(10));
FreeChart chart = ChartFactory.createPieChart("Pie Chart using JFreeChart", pieDataset, true,true,true);
ChartFrame frame1=new ChartFrame("Pie Chart",chart);
frame1.add(panel);
frame1.setVisible(true);
frame1.setSize(600,600);
}
}