这是一个显示饼图的程序。我也有一个按钮。但是我编写的程序创建了两个独立的框架,一个框架用于饼图,另一个框架用于按钮。如何将按钮和饼图放在同一帧上?你能帮我整顿一下我的计划吗?
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);
}
}
答案 0 :(得分:1)
static
,而是使用new Something()
new Integer()
而是使用Integer.valueOf()
(这适用于所有原始包装类,布尔,短,长,......)我不确定你是如何获得两个帧的,但是我通过这种方式修改了代码。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class Pie {
private static void initUI() {
JPanel panel = new JPanel(new FlowLayout());
final JButton b = new JButton("Open File");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int ret = chooser.showDialog(b, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
// Don't know what you want to do
}
}
});
panel.add(b);
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("A", Integer.valueOf(10));
pieDataset.setValue("B", Integer.valueOf(20));
pieDataset.setValue("C", Integer.valueOf(30));
pieDataset.setValue("D", Integer.valueOf(10));
pieDataset.setValue("E", Integer.valueOf(20));
pieDataset.setValue("F", Integer.valueOf(10));
JFreeChart chart = ChartFactory.createPieChart("Pie Chart using JFreeChart", pieDataset, true, true, true);
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartPanel chartPanel = new ChartPanel(chart);
// chartPanel.setPreferredSize(new Dimension(600, 600));
frame1.getContentPane().add(chartPanel, BorderLayout.CENTER);
frame1.getContentPane().add(panel, BorderLayout.SOUTH);
frame1.pack();
frame1.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}
答案 1 :(得分:1)
有一个名为ChartPanel
的班级。它是JPanel
类的子类。
JPanel chartPanel = new ChartPanel(chart);
然后您可以将此面板添加到框架中。希望这有助于........
答案 2 :(得分:0)
替换此行
ChartFrame frame1=new ChartFrame("Pie Chart",chart);
与
panel.add(chart)
请告诉我结果