package my.homework;
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import my.homework.BarChart;
import my.homework.BrokenLineChart;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class myFrame extends Frame implements ActionListener,ItemListener
{
TextField text;
Button btn1,btn2,btn3,btn4,btn5;
TextArea ta1;
Label lab1;
//PopupMenu popM;
//Button but;
public myFrame()
{
super("读取excel数据及其统计图形");
setSize(800,600);
}
public void init()
{
MenuBar myB = new MenuBar();
setMenuBar(myB);
Menu m1 = new Menu("文件");
m1.add(new MenuItem("打开"));
MenuItem m11 = new MenuItem("保存");
m11.setEnabled(false);
m1.add(m11);
m1.add("打印");
m1.addSeparator();
m1.add("退出");
m1.addActionListener(this);
myB.add(m1);
Menu m2 = new Menu("统计图形");
m2.add("柱状图");
m2.addActionListener(this);
m2.add("饼状图");
m2.addActionListener(this);
m2.add("折线图");
m2.addActionListener(this);
myB.add(m2);
Menu m3 = new Menu("帮助");
m3.add("关于");
m3.addActionListener(this);
myB.setHelpMenu(m3);
text = new TextField();
add("North",text);
ta1 = new TextArea("读取的数据:\n",30,80);
Panel p1 = new Panel();
p1.setBackground(Color.lightGray);
p1.add(ta1);
//p1.setLayout(new FlowLayout());
btn1 = new Button("饼状图");
btn1.addActionListener(this);
btn2 = new Button("柱状图");
btn2.addActionListener(this);
btn3 = new Button("折线图");
btn3.addActionListener(this);
Panel p2 = new Panel();
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.setBackground(Color.LIGHT_GRAY);
//p2.setLayout(getLayout());
Button btn4 = new Button("读取数据");
btn4.addActionListener(this);
Button btn5 = new Button("清除");
btn5.addActionListener(this);
Panel p3 = new Panel();
p3.setBackground(Color.lightGray);
p3.add("Center",btn4);
p3.add("Center",btn5);
lab1 =new Label("制作人: luckyrocks-wsl");
p2.add("South",lab1);
//p3.setBounds(1, 2, 11, 12);
//add("Center",btn4);
//add("Center",btn5);
add("Center",p1);
add("South",p2);
add("East",p3);
}
public static void main(String args[])
{
myFrame myMenu = new myFrame();
myMenu.addWindowListener(new WindowAdapter() //为了关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
myMenu.init();
myMenu.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void itemStateChanged(ItemEvent e)
{
text.setText("状态改变");
}
public void actionPerformed(ActionEvent e)
{
text.setText(e.getActionCommand());
//ta1.setText((String) e.getSource());
if(e.getActionCommand()=="打开")
{
JFileChooser j1 = new JFileChooser();
//点击按钮
int n = j1.showOpenDialog(null);
//String filename = j1.getSelectedFile().toString();
if (n == JFileChooser.APPROVE_OPTION)
{
//选择文件后操作
}
}
/* File f=new File("D:"+File.separator+"test.xls");
//这是我自己建的,位于D文件夹中的test.xls
try {
Workbook book=Workbook.getWorkbook(f);//
Sheet sheet=book.getSheet(0); //获得第一个工作表对象
// String a = myContent +" ";
String myContent = "";
for(int i=0;i<sheet.getRows();i++)
{
//String myContent = text.getText()+"";
for(int j=0;j<sheet.getColumns();j++)
{
Cell cell=sheet.getCell(j, i); //获得单元格
System.out.print(cell.getContents()+" ");
//myFrame.ta.append(cell.getContents()+" ");不明白为什么不行,总是出现空指针?
myContent = myContent + cell.getContents()+" ";
text.setText(myContent);
//ta1.append(myContent);
}
//System.out.print("\n");
}
}catch(BiffException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
*/
if(e.getActionCommand()=="退出")
System.exit(0);
if(e.getActionCommand()=="读取数据")
{
File f=new File("test_excel/test.xls"); //设置的相对路径
//File f=new File("D:"+File.separator+"test.xls");
//这是我自己建的,位于D盘中的test.xls,为绝对路径,此法也可,但文件要放对位置
try {
Workbook book=Workbook.getWorkbook(f);//
Sheet sheet=book.getSheet(0); //获得第一个工作表对象
// String a = myContent +" ";
String myContent = "";
for(int i=0;i<sheet.getRows();i++)
{
//String myContent = text.getText()+"";
for(int j=0;j<sheet.getColumns();j++)
{
Cell cell=sheet.getCell(j, i); //获得单元格
ta1.append(cell.getContents()+" ");
}
ta1.append("\n");
}
}
catch(BiffException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getActionCommand()=="清除")
{
ta1.setText("读取的数据:\n");
}
if(e.getActionCommand()=="柱状图")
{
BarChart bar1 = new BarChart();
bar1.getChartFrame();
}
if(e.getActionCommand()=="饼状图")
{
PieChart pie1 = new PieChart();
pie1.getChartFrame();
}
if(e.getActionCommand()=="折线图")
{
BrokenLineChart t1 = new BrokenLineChart();
t1.getChartFrame();
}
}
}