发展部件技术分享 http://blog.sciencenet.cn/u/求新 研究方向:数据库、MIS,教育部教指委计算机分委会专家工作组成员

博文

管理信息系统软件生产线源码101-103

已有 1506 次阅读 2018-7-10 13:29 |个人分类:生活点滴|系统分类:科研笔记| 软件生产线, 软部件, 数据库, 管理信息系统, JAVA

版本1

101.源码101,部件程序,带统计图数据交叉表报表格式文件生成程序。用于辅助生成打印带图表的表格式报表格式文件。交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系

/*

 * 程序文件名:printFormat4.java

 * 作者:程学先

 * 功能:打印数据交叉表格式文件生成程序。用于辅助生成打印带图表的表格式报表格式文件。

 * 交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系。

 * 所针对数据表的数据适宜生成统计图表:记录条数或分组个数在8个以下,

 * 其中有一个整型数据为图形数据,作为画柱形图、圆饼图的依据。列数以能在一行内打印为限.

 * 相对于本系统中统计分析处理程序大类中交叉表程序(程序1011

 * 图形分类字段名(X轴数据名)对应其中列序号字段、

 * 图形大类字段名(X轴分组数据名)对应其中行序号字段、

 * 图形数据字段名(Y轴数据名)对应其中交叉数据字段。

 * 本程序生成格式文件,同时设有打印预览和打印按钮,可以直接进行打印预览和打印

 * 格式文件格式一般为,

 * 第一行数据为:标题[]:(表格标题,字体,字号,统计图标题,字体字号,记录条数、记录列数),

 * 第二行数据为:表格字段名[]

 * 第三行以后为:每列宽度

 * 第四行数据为:图的记录分组字段名、标识字段名和数据字段名

 * 第五行数据为:统计图属性,包括图类型、宽度、高度

 */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.tree.DefaultMutableTreeNode;

import java.io.*;

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Vector;

public class printFormat4 extends JFrame {

    private static printFormat4 frame1 = new printFormat4();      //定义窗口

    private static JPanel panel1 = new JPanel();      //定义面板

    private static List list1 = new List();

    private static JLabel fl0,fl1,fl2,fl3,fl10;

    private static JTextField fa0,fa1,fa2,fa3,fa10;

     private static JLabel fla0,fla1,fla2;

    private static JTextField faa0,faa1,faa2;

    private static JLabel flb0,flb1,flb2;

    private static JTextField fab0,fab1,fab2;

    private static JLabel flc0,flc1,flc2,flc3,flc4,flc5,flc6;

    private static JTextField fac0,fac1,fac2,fac3,fac4,fac5,fac6;

    private static String 当前值;

    private static int 选中行号=-1,当前行号=0,表格行数=0,当前列号=0,列数=0;

    private static int 字段序号=0,记录条数=0;

    private static String[][] 表格数据=null;

    private static String[][] 表格数据1=null;

    private static ArrayList<String> 读入数据 = new ArrayList();

    private static String[] 列名1;

    private static String[] 图表;

    private static String[] 列数据类型;

    private static int[] 列数据宽度;

    private static String[] 一行数据=new String[12];

    private static int[] 一行数据1=new int[12];

    private static File file1;

    private static FileReader fr;

   static Connection con

   private static Statement sta;     //连接数据库

   static String s1="",s2="";   

   private static ResultSet rs

   private static ResultSetMetaData rsmd;

   private static ArrayList 表名1=new ArrayList();

   private static String [] 字号={"5","6","7","8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};

   private static String [] 字体={"宋体","楷体","黑体","仿宋_GB2312"};

   private static String [] 类型={"柱形图","饼形图","折线图"};

   private static int 窗口宽=main1.窗口宽;

   private static String 数据表名;

   private static String 连接条件 = "";

   private static String 字段名表 = "";

 

    static void means(String [] parameter){

      frame1 = new printFormat4();      //定义窗口

      panel1 = new JPanel();      //定义面板

      list1 = new List();

      选中行号=-1;当前行号=0;表格行数=0;当前列号=0;列数=0;

      字段序号=0;记录条数=0;

      读入数据 = new ArrayList();

      一行数据=new String[12];

      一行数据1=new int[12];

      s1="";s2="";   

      表名1=new ArrayList();

      类型[0]="柱形图";类型[1]="饼形图";类型[2]="折线图";

      字号=new String[19];

      字体= new String[4];

      字号[0]="5";字号[1]="6";字号[2]="7";字号[3]="8";

      字号[4]="9";字号[5]="10";字号[6]="11";字号[7]="12";

      字号[8]="14";字号[9]="16";字号[10]="18";字号[11]="20";

      字号[12]="22";字号[13]="24";字号[14]="26";字号[15]="28";

      字号[16]="36";字号[17]="48";字号[18]="72";

      字体[0]="宋体";字体[1]="楷体";字体[2]="黑体";

      字体[3]="仿宋_GB2312";

     frame1.setTitle("图表类报表打印格式设计与打印程序。                       作者:程学先");

        frame1.setBounds(10, 10, 900, 580);    //窗口位置大小

        frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 设置窗体的默认关闭模式

        panel1.setBounds(10, 10, 880, 570);      //定义面板大小与位置

        panel1.setLayout(null);        //关闭面板布局管理器

        String[] 列名01  = { "标签名", "字段名", "宽度", "字体", "字号"};

        main1.driver1();

        数据表名=parameter[4] ;

        数据表查询结构(数据表名);

        fl10=new JLabel("文件名");

        fl10.setBounds(30,30,50, 20);

        panel1.add(fl10); 

        fa10 = new JTextField("",20); 

        fa10.setBounds(80,30, 600, 20);

        panel1.add(fa10);      

        fa10.addMouseListener(new MouseAdapter() {

                  public void mouseClicked(MouseEvent e) {

         图表=new String[37];

         if (fa10.getText().length()<1){      

            final JFileChooser fc = new JFileChooser(".\\"); 

          fc.showOpenDialog(null);    //读取文件名

           if (fc.getSelectedFile()!=null) { 

             fa10.setText(fc.getSelectedFile().toString());

                file1 = new File(fa10.getText());

               try{   

                  if ((fa10.getText().length()>0)&&(file1.exists())) {   //如果文件存在

                      fr = new FileReader(file1);

                   BufferedReader br = new BufferedReader(fr);

                   String tempStr = null;

                   int y=0,b=0;

                  for (int i = 0; (tempStr = br.readLine()) != null; i++){//读文件

                    图表[i]=tempStr;

                    y=i;

                  }

                  if (y>=16) {

                  if (y>=0) faa0.setText(图表[0]);

                  if (y>=1) faa1.setText(图表[1]);

                  if (y>=2) faa2.setText(图表[2]);

                  if (y>=3) fa0.setText(图表[3]);

                  if (y>=4) fa1.setText(图表[4]);

                  if (y>=5) fa2.setText(图表[5]);

                  if (y>=6) fa3.setText(图表[6]);

                  if (y>=7) fab0.setText(图表[7]);

                  if (y>=8) fab1.setText(图表[8]);

                  if (y>=9) fab2.setText(图表[9]);

                  if (y>=10) fac0.setText(图表[10]);

                  if (y>=11) fac1.setText(图表[11]);

                  if (y>=12) fac2.setText(图表[12]);

                  if (y>=13) fac3.setText(图表[13]);

                  if (y>=14) fac4.setText(图表[14]);

                  if (y>=15) fac5.setText(图表[15]);

                  if (y>=16) fac6.setText(图表[16]);

                  }

                 }

                 else {

                     if (fa10.getText().length()>0)

                          file1.createNewFile();// 创建文件

                       }

                    }catch(IOException e2) {   

                   JOptionPane.showMessageDialog( null, "打开文件错!");

               }

             }

         }

         }

        });

 

        fla0=new JLabel("表格标题");

        fla0.setBounds(30,70,80, 20);

         panel1.add(fla0); 

         faa0 = new JTextField("",20); 

         faa0.setBounds(110,70, 300, 20);

         panel1.add(faa0);      

         faa0.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=0;    

                    list1.removeAll();

             }

        });

         fla1=new JLabel("表题字体");

         fla1.setBounds(420,70,70, 20);

          panel1.add(fla1); 

          faa1 = new JTextField("",20); 

          faa1.setBounds(490,70, 60, 20);

          panel1.add(faa1);      

          faa1.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=1;    

                    list1.removeAll();

                    for (int i=0;i<字体.length;i++){

                       list1.add(字体[i]);

                    }

             }

         });

          fla2=new JLabel("表题字号");

          fla2.setBounds(560,70,70, 20);

           panel1.add(fla2); 

           faa2 = new JTextField("",20); 

           faa2.setBounds(630,70, 70, 20);

           panel1.add(faa2);      

           faa2.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=2;    

                    list1.removeAll();

                    for (int i=0;i<字号.length;i++){

                       list1.add(字号[i]);

                    }

              }

          });

 

           fl2=new JLabel("数据字体");

           panel1.add(fl2); 

           fa2 = new JTextField("",20); 

           panel1.add(fa2);      

           fl2.setBounds(30,110,70, 20);

           fa2.setBounds(100,110, 120, 20);

           fa2.addFocusListener(new FocusAdapter() {   

                public void focusGained(final FocusEvent arg0) {

                       当前列号=5;    

                       list1.removeAll();

                       for (int i=0;i<字体.length;i++){

                          list1.add(字体[i]);

                       }

                }

           });

 

           fl3=new JLabel("数据字号");

           panel1.add(fl3); 

           fa3 = new JTextField("",20); 

           panel1.add(fa3);      

           fl3.setBounds(230,110,70, 20);

           fa3.setBounds(300,110,110, 20);

           fa3.addFocusListener(new FocusAdapter() {   

                public void focusGained(final FocusEvent arg0) {

                       当前列号=6;    

                       list1.removeAll();

                          for (int i=0;i<字号.length;i++){

                             list1.add(字号[i]);

                          }

                       }

           });

       

        fl0=new JLabel("报表字段名(逗号分隔)");

        fl0.setBounds(30,140,150, 20);

         panel1.add(fl0); 

         fa0 = new JTextField("",20); 

         fa0.setBounds(30,160,670, 20);

         panel1.add(fa0);      

         fa0.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=3;    

                    list1.removeAll();

                           for (int i=0;i<列数;i++) {

                               list1.add(列名1[i]);

                           }

             }

        });

        fl1=new JLabel("宽度(逗号分隔)");

        fl1.setBounds(30,190,140, 20);

         panel1.add(fl1); 

         fa1 = new JTextField("",20); 

         fa1.setBounds(30,210, 670, 20);

         panel1.add(fa1);      

         fa1.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=4;    

                    list1.removeAll();

             }

        });

        

       flb0=new JLabel("统计图标题");

       flb0.setBounds(30,260,80, 20);

        panel1.add(flb0); 

        fab0 = new JTextField("",20); 

        fab0.setBounds(110,260, 300, 20);

        panel1.add(fab0);      

        fab0.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=7;    

                    list1.removeAll();

             }

       });

        flb1=new JLabel("图题字体");

        flb1.setBounds(420,260,70, 20);

         panel1.add(flb1); 

         fab1 = new JTextField("",20); 

         fab1.setBounds(490,260, 60, 20);

         panel1.add(fab1);      

         fab1.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=8;    

                    list1.removeAll();

                    for (int i=0;i<字体.length;i++){

                       list1.add(字体[i]);

                    }

             }

        });

         flb2=new JLabel("图题字号");

         flb2.setBounds(560,260,70, 20);

          panel1.add(flb2); 

          fab2 = new JTextField("",20); 

          fab2.setBounds(630,260, 70, 20);

          panel1.add(fab2);      

          fab2.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=9;    

                    list1.removeAll();

                    for (int i=0;i<字号.length;i++){

                       list1.add(字号[i]);

                    }

             }

         });

 

          flc0=new JLabel("图形分类字段名(X轴数据名)");

          flc0.setBounds(30,300,200, 20);

           panel1.add(flc0); 

           fac0 = new JTextField("",20); 

           fac0.setBounds(30,330, 200, 20);

           panel1.add(fac0);      

           fac0.addFocusListener(new FocusAdapter() {   

             public void focusGained(final FocusEvent arg0) {

                    当前列号=10;    

                    list1.removeAll();

                           for (int i=0;i<列数;i++) {

                               list1.add(列名1[i]);

                           }

             }

          });

           flc1=new JLabel("图形大类字段名(X轴分组数据名)");

           flc1.setBounds(240,300,200, 20);

            panel1.add(flc1); 

            fac1 = new JTextField("",20); 

            fac1.setBounds(240,330,200, 20);

            panel1.add(fac1);      

            fac1.addFocusListener(new FocusAdapter() {   

              public void focusGained(final FocusEvent arg0) {

                     当前列号=11;    

                     list1.removeAll();

                            for (int i=0;i<列数;i++) {

                                list1.add(列名1[i]);

                            }

              }

           });

            flc2=new JLabel("图形数据字段名(Y轴数据名)");

            flc2.setBounds(450,300,200, 20);

             panel1.add(flc2); 

             fac2 = new JTextField("",20); 

             fac2.setBounds(450,330,200, 20);

             panel1.add(fac2);      

             fac2.addFocusListener(new FocusAdapter() {   

                public void focusGained(final FocusEvent arg0) {

                       当前列号=12;    

                       list1.removeAll();

                              for (int i=0;i<列数;i++) {

                                  list1.add(列名1[i]);

                              }

                }

            });

             flc3=new JLabel("图形类型");

             flc3.setBounds(30,370,70, 20);

              panel1.add(flc3); 

              fac3 = new JTextField("",20); 

              fac3.setBounds(100,370,100, 20);

              panel1.add(fac3);      

              fac3.addFocusListener(new FocusAdapter() {   

                public void focusGained(final FocusEvent arg0) {

                       当前列号=13;    

                       list1.removeAll();

                              for (int i=0;i<类型.length;i++) {

                                  list1.add(类型[i]);

                              }

                }

             });

              flc4=new JLabel("图形宽度");

              flc4.setBounds(210,370,60, 20);

               panel1.add(flc4); 

               fac4 = new JTextField("",20); 

               fac4.setBounds(270,370, 80, 20);

               panel1.add(fac4);      

               fac4.addFocusListener(new FocusAdapter() {   

                 public void focusGained(final FocusEvent arg0) {

                   当前列号=14;    

                   list1.removeAll();

                   String sk[]=fa1.getText().split(",");

                    int wh4=0;

                   for (int i=0;i<sk.length;i++)

                     wh4=wh4+Integer.parseInt(sk[i]);

                   fac4.setText(wh4+"");

                   }

              });

               flc5=new JLabel("图形高度");

               flc5.setBounds(360,370,60, 20);

                panel1.add(flc5); 

                fac5 = new JTextField("",20); 

                fac5.setBounds(420,370, 80, 20);

                panel1.add(fac5);      

                fac5.addFocusListener(new FocusAdapter() {   

                   public void focusGained(final FocusEvent arg0) {

                     当前列号=15;    

                     list1.removeAll();

                      int wh4=0;

                      if ((faa2.getText().length()>0)&&(faa2.getText().compareTo("0")>0))

                        wh4=Integer.parseInt(faa2.getText());//表标题字号

                      if ((fa3.getText().length()>0)&&(fa3.getText().compareTo("0")>0))

                      wh4=wh4+记录条数*Integer.parseInt(fa3.getText());  //表格高度

                      else wh4=wh4+记录条数*20;

                      if ((fab2.getText().length()>0)&&(fab2.getText().compareTo("0")>0))

                        wh4=wh4+Integer.parseInt(fab2.getText());  //图标题字号

                      wh4=800-wh4;  //估计A4打印每页高度为800象素点

                  fac5.setText(wh4+"");

                 }

               });

                flc6=new JLabel("统计函数序号");

                flc6.setBounds(510,370,90, 20);

                 panel1.add(flc6); 

                 fac6 = new JTextField("",20); 

                 fac6.setBounds(600,370, 100, 20);

                 panel1.add(fac6);      

                 fac6.addFocusListener(new FocusAdapter() {   

                   public void focusGained(final FocusEvent arg0) {

                     当前列号=16;    

                     list1.removeAll();

                     list1.add("按记录");

                     list1.add("分组求和");

                     list1.add("分组平均");

                   }

                });

         list1.setBounds(730, 30,120,410); 

        panel1.add(list1);

        list1.addMouseListener(new MouseAdapter() {

           public void mouseClicked(MouseEvent e) {

            int w1=0,w2=0;

            if (list1.getSelectedIndex()>=0){

               int wh1,wh2,h1,h2;

             String s0="";

             switch (当前列号) {

             case 0: // 表名

                if (fa0.getText().length()==0)

                    s0=list1.getSelectedItem();

                else s0=fa0.getText()+","+list1.getSelectedItem();

                fa0.setText(s0);

                数据表查询结构(s0);

                break;

                  case 1:   

                    faa1.setText(list1.getSelectedItem());

                    break;

                  case 2:   

                    faa2.setText(list1.getSelectedItem());

                    break;

                  case 3:

                    wh1=list1.getSelectedIndex();

                    wh2=列数据宽度[wh1];

                    if (列名1[wh1].length()>wh2)

                       wh2=列名1[wh1].length();

                    if (fa3.getText().length()>0)   //乘字号

                       wh2=wh2*Integer.parseInt(fa3.getText());

                    else wh2=wh2*12;

                    

                    if (fa0.getText().length()==0){

                    fa0.setText(list1.getSelectedItem());

                    fa1.setText(wh2+"");

                    }

                    else{

                    fa0.setText(fa0.getText()+","+list1.getSelectedItem());

                    fa1.setText(fa1.getText()+","+wh2);

                    }

                    break;

                  case 5:   

                    fa2.setText(list1.getSelectedItem());

                    break;

                  case 6:   

                    fa3.setText(list1.getSelectedItem());

                    break;

                  case 8:   

                    fab1.setText(list1.getSelectedItem());

                    break;

                  case 9:   

                    fab2.setText(list1.getSelectedItem());

                    break;

                  case 10:   

                    fac0.setText(list1.getSelectedItem());

                    break;

                  case 11:   

                    fac1.setText(list1.getSelectedItem());

                    break;

                  case 12:   

                    fac2.setText(list1.getSelectedItem());

                    break;

                  case 13:   

                    fac3.setText(list1.getSelectedIndex()+"");

                    break;

                  case 16:   

                    fac6.setText(list1.getSelectedIndex()+"");

                    break;

                }

            }

             }

        });

              

        final JButton creButton = new JButton("格式内容存盘"); 

        creButton.setBounds(30,440,170, 20);

        creButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) { 

            char x1=10,x2=13,x3='"',x4=','

                file1 = new File(fa10.getText());

             FileOutputStream fs;

             try{

             fs = new  FileOutputStream(file1);

               fs.write((faa0.getText()+x1).getBytes());

               fs.write((faa1.getText()+x1).getBytes());

               fs.write((faa2.getText()+x1).getBytes());

               fs.write((fa0.getText()+x1).getBytes());

               fs.write((fa1.getText()+x1).getBytes());

               fs.write((fa2.getText()+x1).getBytes());

               fs.write((fa3.getText()+x1).getBytes());

               fs.write((fab0.getText()+x1).getBytes());

               fs.write((fab1.getText()+x1).getBytes());

               fs.write((fab2.getText()+x1).getBytes());

               fs.write((fac0.getText()+x1).getBytes());

               fs.write((fac1.getText()+x1).getBytes());

               fs.write((fac2.getText()+x1).getBytes());

               fs.write((fac3.getText()+x1).getBytes());

               fs.write((fac4.getText()+x1).getBytes());

               fs.write((fac5.getText()+x1).getBytes());

               fs.write((fac6.getText()+x1).getBytes());

             }catch(IOException e2) {

                   JOptionPane.showMessageDialog( null,"写文件错。");

             }

            }

        });

        panel1.add(creButton);    

 

        final JButton clearButton = new JButton("清参数文本框"); 

        clearButton.setBounds(200,440,170, 20);

        clearButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                fa1.setText("");fa2.setText("0");

             }

        });

        panel1.add(clearButton);   

 

        final JButton printButton = new JButton("打印预览"); 

        printButton.setBounds(370,440,170, 20);

        printButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

             try

             {

               con=main1.getConn();// 连接数据库

               sta = con.createStatement(

                     ResultSet.TYPE_SCROLL_INSENSITIVE,

                     ResultSet.CONCUR_UPDATABLE);

                  s1 = "select "+fa0.getText()+" from " + 数据表名; // 查取全表数据

               if (数据表名.indexOf(",")>0)

                  s1=s1+连接条件;

               rs = sta.executeQuery(s1);

               rs.last(); // 移动到最后一行

               rsmd = rs.getMetaData();

               记录条数=rs.getRow();

               列数 = rsmd.getColumnCount(); // 获取列数

               列名1 = new String[列数]; // 定义列名数组

               列数据类型 = new String[列数];

               列数据宽度 = new int[列数];

               表格数据=new String[记录条数][列数];

               当前列号=1;     

               for (int i=0;i<列数;i++) {

                    列名1[i] = rsmd.getColumnName(i + 1);

                  列数据类型[i] = rsmd.getColumnTypeName(i+1);

                  列数据宽度[i] = rsmd.getColumnDisplaySize(i+1);

               }

               for (int i=0;i<记录条数;i++){

                  rs.absolute(i+1);

                  for (int j=0;j<列数;j++){

                     表格数据[i][j]=rs.getString(j+1);

                   if ((表格数据[i][j] == null)

                         || (表格数据[i][j].trim().length() == 0))

                      表格数据[i][j] = "0";

                  }

               }

               rs.close();

               sta.close();

               con.close();

               } catch (SQLException e1)

             {  

               JOptionPane.showMessageDialog( null, "获取数据结构出错!"+s1);

             }

             String sc0=fac0.getText()+","+fac1.getText()+

                  ","+fac2.getText();

                if ((fac6.getText()==null)||(fac6.getText().length()==0))

                   fac6.setText("0");

             if (Integer.parseInt(fac6.getText())==0){   //求明细

               sc0="select "+sc0+" from "+数据表名;

               if (数据表名.indexOf(",")>0)

                  sc0=sc0+连接条件;

             }

                else if (Integer.parseInt(fac6.getText())==1){   //求小计

               sc0="select "+fac0.getText()+" ' ' as x "+

               "sum("+fac2.getText()+") from "+数据表名;

               if (数据表名.indexOf(",")>0)

                  sc0=sc0+连接条件;

             }

                else if (Integer.parseInt(fac6.getText())==2){   //求平均

               sc0="select "+fac0.getText()+" ' ' as x "+

               "avg("+fac2.getText()+") from "+数据表名;

               if (数据表名.indexOf(",")>0)

                  sc0=sc0+连接条件;

             }

                else{

                  JOptionPane.showMessageDialog( null, "统计类型输入错误!");

                  return;

                }

            try

             {

               con=main1.getConn();// 连接数据库

               sta = con.createStatement(

                     ResultSet.TYPE_SCROLL_INSENSITIVE,

                     ResultSet.CONCUR_UPDATABLE);

                  rs = sta.executeQuery(sc0);

                  rs.last(); // 移动到最后一行

               rsmd = rs.getMetaData();

               int 记录条数1=rs.getRow();

               int 列数1 = rsmd.getColumnCount(); // 获取列数

               String [] 列名2 = new String[列数1]; // 定义列名数组

               表格数据1=new String[记录条数1][列数1];

               for (int i=0;i<记录条数1;i++){

                  rs.absolute(i+1);

                  for (int j=0;j<列数1;j++){

                     表格数据1[i][j]=rs.getString(j+1);

                   if ((表格数据1[i][j] == null)

                        || (表格数据1[i][j].trim().length() == 0))

                      表格数据[i][j] = "0";

                  }

               }

               rs.close();

               sta.close();

               con.close();

               } catch (SQLException e1)

             {

               JOptionPane.showMessageDialog( null, "获取数据结构出错!"+s1);

             }

             statisticalChart3.means(fa10.getText(),表格数据,表格数据1,fac0.getText(),fac2.getText());

             //文件名、表格数据、图形数据、图形数据字段名称、图形分组名称

            }

        });

        panel1.add(printButton);   

 

        final JButton exitButton = new JButton("退出"); 

        exitButton.setBounds(540,440,170, 20);

        exitButton.addActionListener(new ActionListener() { 

            public void actionPerformed(ActionEvent e) {

             panel1.setVisible(false);

             frame1.setVisible(false);

                 frame1.dispose();

          }

        });

        panel1.add(exitButton); 

        frame1.add(panel1); // 把面板添加到窗体

        frame1.setVisible(true);

 }

   static void 数据表查询结构(String 数据表名) {

     String[] s10 = null;

     String[] sk = new String[300];

     int n1 = 0, n2 = 0;

     字段名表 = "";

     连接条件 = "";

     if (数据表名.indexOf(",") < 0) { // 单表

        try {

           con = main1.getConn();

           sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                ResultSet.CONCUR_UPDATABLE);

           String s1 = "select * from " + 数据表名;

           rs = sta.executeQuery(s1);

           rsmd = rs.getMetaData();

           列数 = rsmd.getColumnCount();

           列名1 = new String[列数];

           字段名表 = "";

           for (int i = 0; i < 列数; i++) {

             列名1[i] = rsmd.getColumnName(i + 1);

             sk[i] = 列名1[i];

             n1++;

             if (字段名表.length() == 0)

                字段名表 = 列名1[i];

             else

                字段名表 = 字段名表 + "," + 列名1[i];

           }

           rs.close();

           sta.close();

           con.close();

        } catch (SQLException e1) {

           JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

        }

     } else { // 多表

        s10 = 数据表名.split(",");

        String s0 = "", s1 = "", s2 = "";

        int k = 0;

        for (int i = 0; i < s10.length; i++) {

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             s1 = "select * from " + s10[i];

             rs = sta.executeQuery(s1);

             rsmd = rs.getMetaData();

             列数 = rsmd.getColumnCount();

             for (int j = 0; j < 列数; j++) { // 读取所有表的列名,同名的加表名

                s2 = rsmd.getColumnName(j + 1).toLowerCase(); // 列名

                n2 = n1;

                if (i > 0) { // 加入第2个表之后字段出现同名字段

                   k = 0;

                   for (int j1 = 0; j1 < n2; j1++) {

                      if (sk[j1].equals(s2)) {

                        sk[j1] = s10[i - 1] + "." + sk[j1];

                        if (连接条件.length() == 0) // 连接条件

                           连接条件 = " where " + s10[i - 1] + "."

                                + s2 + " = " + s10[i] + "."

                                + s2;

                        else

                           连接条件 = 连接条件 + " and " + s10[i - 1]

                                + "." + s2 + " = " + s10[i]

                                + "." + s2;

                        sk[n1] = s10[i] + "." + s2 + " as "

                              + (s2 + i);

                        n1++;

                        k = 1;

                        break;

                      }

                   }

                   if (k == 0) {

                      sk[n1] = s2;

                      n1++;

                   }

                } else {

                   sk[n1] = s2;

                   n1++;

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

           }

        }

        字段名表 = "";

        for (int j1 = 0; j1 < n1; j1++)

           if (字段名表.length() == 0)

             字段名表 = sk[j1];

           else

             字段名表 = 字段名表 + "," + sk[j1];

     }

     int c=0,b=0;

     try {

        con = main1.getConn();

        sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

             ResultSet.CONCUR_UPDATABLE);

        s1 = "select " + 字段名表 + "  from " + 数据表名 + 连接条件;

        rs = sta.executeQuery(s1);

        rsmd = rs.getMetaData();

        列数 = rsmd.getColumnCount();

        列名1 = new String[列数];

        列数据类型 = new String[列数];

        列数据宽度 = new int[列数];

        for (int i = 0; i < 列数; i++) {

           列名1[i] = sk[i];

           列数据类型[i] = rsmd.getColumnTypeName(i + 1);

           列数据宽度[i] = rsmd.getColumnDisplaySize(i + 1);

        }

        rs.last();

        记录条数 = rs.getRow();

        表格数据 = new String[记录条数][列数];

        while (c < 记录条数) {

           rs.absolute(c + 1);

           while (b < 列数) {

             表格数据[c][b] = rs.getString(b + 1);

             if ((表格数据[c][b] == null)

                   || (表格数据[c][b].trim().length() == 0))

                表格数据[c][b] = "0";

             else if (表格数据[c][b] == null)

                表格数据[c][b] = "";

           b++;

           }

           c++;

           b = 0;

        }

        rs.close();

        sta.close();

        con.close();

     } catch (SQLException e1) {

        JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

     }

   }

   }

102.源码102,部件程序,根据printFormat4.java生成的格式文件打印或打印预览带直方图统计图与数据表格的数据交叉表报表。

/**

 * 程序文件名:dataPrint5.java

 * 作者:程学先

 * 用途:根据printFormat4.java生成的格式文件打印或打印预览带直方图统计图表格的数据交叉表报表。

 * 交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系。

 * 完成时间:20131127

 * 调用语句:dataPrint5.means(parameter);

 * 必须提供的参数:数据表表名格式文件名

 */

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

 

public class dataPrint5 extends JComponent {

   private static JFrame frame1;

   static Connection con;

   private static ResultSetMetaData rsmd;

   private static Statement sta; // 连接数据库

   private static Connection cont4;

   private static Statement stat4;

   private static ResultSetMetaData rsmdt4;

   private static ResultSet rs;

   private static String 表名;

   private static String s1 = "";

   static int 最大值 = 0, 最小值 = 0, 纵向分段数 = 20;

   private static int 记录条数 = 0, 列数 = 0, 记录号 = 0;

   private static String[] 列名;

   private static String[] 数据列名;

   private static String[] 列数据类型;

   private static int[] 列数据宽度;

   private static String[][] 表格数据;

   private static String[] 标签数据;

   static String[] 关键字;

   static int[][] 交叉数据2;

   static int[] 交叉数据1;

   static int 窗口宽度 = 1000, 窗口高度 = 600;

   static int 标签左边 = 100, 标签到顶 = 窗口高度;

   private static ArrayList<struct1> title = new ArrayList<struct1>();

   private static ArrayList<struct2> tableTitle = new ArrayList<struct2>();

   private static ArrayList<struct3> table1 = new ArrayList<struct3>();

   private static ArrayList<struct4> table2 = new ArrayList<struct4>();

   private static int 页高 = 600;

   private static int x1 = 0, y1 = 0, w1 = 0, h1 = 0, 左边 = 0, 到顶 = 0;

   private static int 图顶点 = 0;

   private static Graphics2D g2;

   private static Graphics g;

   private static String faa0, faa1, faa2;

   private static String fab0, fab1, fab2;

   private static String fac0, fac1, fac2, fac3, fac4, fac5, fac6;

   private static String fa0, fa1,fa2,fa3,fa10;

   private static String[][] 表格数据1 = null;

   private static String 数据表名;

   private static String 连接条件;

   private static String 字段名表 = "";

   private static String 打印格式文件名;

   private static String[] 图表;

   private static int 记录条数1;

   private static int 列数1; // 获取列数

 

   static void title() {

     struct2[] 标签 = new struct2[tableTitle.size()];

     左边 = Integer.valueOf(tableTitle.get(0).tableTitle[3]);

     左边 = 左边 / 2;

     for (int i = 0; i < tableTitle.size(); i++)

        标签[i] = tableTitle.get(i);

    g2.setFont(new Font(标签[0].tableTitle[6], Font.PLAIN, Integer

           .valueOf(标签[0].tableTitle[7])));// 设置字体

     for (int i = 0; i < tableTitle.size(); i++) {

        x1 = 左边;

        y1 = 到顶;

        w1 = Integer.valueOf(标签[i].tableTitle[3]);

        h1 = Integer.valueOf(标签[i].tableTitle[4]);

        g2.drawLine(x1, y1, x1 + w1, y1);

        g2.drawLine(x1, y1, x1, y1 + h1);

        g2.drawString(标签[i].tableTitle[0], 左边 + 2, 到顶 + h1);

        if (标签[i].tableTitle[8].equals(""))

           g2.drawLine(x1, y1 + h1, x1 + w1, y1 + h1);

        if (标签[i].tableTitle[9].equals(""))

           g2.drawLine(x1 + w1, y1, x1 + w1, y1 + h1);

        左边 = 左边 + w1;

        if ((i < tableTitle.size() - 1)

             && (Integer.valueOf(标签[i].tableTitle[1]) < Integer

                   .valueOf(标签[i + 1].tableTitle[1])))

           到顶 = 到顶 + h1;

     }

     到顶 = 到顶 + h1;

   }

 

   protected void paintComponent(Graphics g1) {

     g = g1;

     g2 = (Graphics2D) g;

     struct1[] 标题 = new struct1[title.size()];

     到顶 = 0;

     左边 = 0;

     for (int i = 0; i < title.size(); i++) {

        标题[i] = title.get(i);

        g2.setFont(new Font(标题[i].title[8], Font.PLAIN, Integer

             .valueOf(标题[i].title[9])));// 设置字体

        x1 = Integer.valueOf(标题[i].title[6]);

        y1 = Integer.valueOf(标题[i].title[7]);

        g2.drawString(标题[i].title[0], x1, y1);

        到顶 = y1 + Integer.valueOf(标题[i].title[5]);

     }

     title();

     struct3[] 表体 = new struct3[table1.size()];

     int[] w10 = new int[table1.size()];

     int[] h10 = new int[table1.size()];

     int[] h20 = new int[table1.size()];

     for (int i = 0; i < table1.size(); i++) {

        表体[i] = table1.get(i);

        w10[i] = Integer.valueOf(表体[i].table1[2]);

        h10[i] = Integer.valueOf(表体[i].table1[3]);

        h20[i] = Integer.valueOf(表体[i].table1[5]);

     }

     y1 = 到顶;

     g2.setFont(new Font(表体[0].table1[4], Font.PLAIN, Integer

           .valueOf(表体[0].table1[5])));// 设置字体

     for (int j = 0; j < 记录条数; j++) {

        x1 = Integer.valueOf(tableTitle.get(0).tableTitle[3]) / 2;

        g2.drawLine(x1, y1, x1, y1 + h1);

        for (int i = 0; i < table1.size(); i++) {

           if (表格数据[j][i] == null)

             表格数据[j][i] = " ";

           g2.drawString(表格数据[j][i], x1 + 2, y1 + h20[i]);

           g2.drawLine(x1, y1 + h10[i], x1 + w10[i], y1 + h10[i]);

           g2.drawLine(x1 + w10[i], y1, x1 + w10[i], y1 + h10[i]);

           x1 = x1 + w10[i];

        }

        y1 = y1 + h10[table1.size() - 1];

        if (y1 >= 页高) {

           struct4[] 页尾 = new struct4[table2.size()];

           for (int i1 = 0; i1 < table2.size(); i1++) {

             页尾[i1] = table2.get(i1);

             g2.setFont(new Font(页尾[i1].table2[8], Font.PLAIN, Integer

                   .valueOf(页尾[i1].table2[9])));// 设置字体

             x1 = Integer.valueOf(页尾[i1].table2[6]);

             y1 = Integer.valueOf(页尾[i1].table2[7]);

             g2.drawString(页尾[i1].table2[0], x1, y1);

             到顶 = y1 + Integer.valueOf(页尾[i1].table2[5]);

           }

           y1 = 640;

           break;

        }

     }

     y1 = y1 + h10[table1.size() - 1];

   }

 

   static void means(String[] parameter) {

     frame1 = new JFrame(); // 定义窗口

     frame1.setLayout(null);

     frame1.setTitle("图表式报表打印与打印预览程序                                                                                                                                                                                              作者:程学先");

     frame1.setBounds(10, 10, 1000, 660);

     frame1.getContentPane().setLayout(null);

     final JPanel panel1 = new JPanel(); // 定义面板

     panel1.setBounds(10, 10, 1000, 660);

     panel1.setLayout(null);

     frame1.getContentPane().add(panel1);

     表名 = parameter[4];

     连接条件 = parameter[20];

     打印格式文件名 = parameter[15];

     图表 = new String[37];

     if (打印格式文件名.length() < 1) {

        final JFileChooser fc = new JFileChooser(".\\");

        fc.showOpenDialog(null); // 读取文件名

        if (fc.getSelectedFile() != null) {

           打印格式文件名 = fc.getSelectedFile().toString();

        }

     }

     File file1 = new File(打印格式文件名);

     fa10 = 打印格式文件名;

     try {

        if ((打印格式文件名.length() > 0) && (file1.exists())) { // 如果文件存在

           FileReader fr = new FileReader(file1);

           BufferedReader br = new BufferedReader(fr);

           String tempStr = null;

           int y = 0, b = 0;

           for (int i = 0; (tempStr = br.readLine()) != null; i++) {// 读文件

             图表[i] = tempStr;

             y = i;

           }

           if (y >= 16) {

             if (y >= 0)

                faa0 = 图表[0];

             if (y >= 1)

                faa1 = 图表[1];

             if (y >= 2)

                faa2 = 图表[2];

             if (y >= 3)

                fa0 = 图表[3];

             if (y >= 4)

                fa1 = 图表[4];

             if (y >= 5)

                fa2 = 图表[5];

             if (y >= 6)

                fa3 = 图表[6];

             if (y >= 7)

                fab0 = 图表[7];

             if (y >= 8)

                fab1 = 图表[8];

             if (y >= 9)

                fab2 = 图表[9];

             if (y >= 10)

                fac0 = 图表[10];

             if (y >= 11)

                fac1 = 图表[11];

             if (y >= 12)

                fac2 = 图表[12];

             if (y >= 13)

                fac3 = 图表[13];

             if (y >= 14)

                fac4 = 图表[14];

             if (y >= 15)

                fac5 = 图表[15];

             if (y >= 16)

                fac6 = 图表[16];

           }

        }

     } catch (IOException e2) {

        JOptionPane.showMessageDialog(null, "打开文件错!");

     }

     字段名表 = fa0;

     数据表名 = 表名;

     数据表查询结构(表名);

     数据表查询数据(表名);

 

     final JButton inButton = new JButton("预览图表");

     inButton.setBounds(340, 520, 170, 20);

     inButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           String sc0 = fac0 + "," + fac1 + "," + fac2;

           if ((fac6==null)||(fac6.length()==0)) fac6="0";

           if (Integer.parseInt(fac6) == 0) { // 求明细

             sc0 = "select " + sc0 + " from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0 + 连接条件;

           } else if (Integer.parseInt(fac6) == 1) { // 求小计

             sc0 = "select " + fac0 + " ' ' as x " + "sum(" + fac2

                   + ") from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0 + 连接条件;

           } else if (Integer.parseInt(fac6) == 2) { // 求平均

             sc0 = "select " + fac0 + " ' ' as x " + "avg(" + fac2

                   + ") from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0 + 连接条件;

           } else {

             JOptionPane.showMessageDialog(null, "统计类型输入错误!");

             return;

          }

           try {

             con = main1.getConn();// 连接数据库

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             rs = sta.executeQuery(sc0);

             rs.last(); // 移动到最后一行

             rsmd = rs.getMetaData();

             记录条数1 = rs.getRow();

             列数1 = rsmd.getColumnCount(); // 获取列数

             表格数据1 = new String[记录条数1][列数1];

             for (int i = 0; i < 记录条数1; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < 列数1; j++) {

                   表格数据1[i][j] = rs.getString(j + 1);

                }

              }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据出错!" + sc0);

           }

           main1.n = 0; // 打印预览标志

           statisticalChart3.means(fa10, 表格数据, 表格数据1, fac2 + "", fac0);

        }

     });

     panel1.add(inButton);

     final JButton inButton1 = new JButton("打印图表");

     inButton1.setBounds(530, 520, 170, 20);

     inButton1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           String sc0 = fac0 + "," + fac1 + "," + fac2;

           if (Integer.parseInt(fac6) == 0) { // 求明细

             sc0 = "select " + sc0 + " from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0 + 连接条件;

           } else if (Integer.parseInt(fac6) == 1) { // 求小计

             sc0 = "select " + fac0 + " ' ' as x " + "sum(" + fac2

                   + ") from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0 + 连接条件;

           } else if (Integer.parseInt(fac6) == 2) { // 求平均

             sc0 = "select " + fac0 + " ' ' as x " + "avg(" + fac2

                   + ") from " + 数据表名;

             if (数据表名.indexOf(",") > 0)

                sc0 = sc0+ 连接条件;

           } else {

             JOptionPane.showMessageDialog(null, "统计类型输入错误!");

             return;

           }

           try {

             con = main1.getConn();// 连接数据库

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             rs = sta.executeQuery(sc0);

             rs.last(); // 移动到最后一行

             rsmd = rs.getMetaData();

             记录条数1 = rs.getRow();

             列数1 = rsmd.getColumnCount(); // 获取列数

             表格数据1 = new String[记录条数1][列数1];

             for (int i = 0; i < 记录条数1; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < 列数1; j++) {

                   表格数据1[i][j] = rs.getString(j + 1);

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据出错!" + sc0);

           }

           main1.n = 1; // 打印标志

           statisticalChart3.means(fa10, 表格数据, 表格数据1, fac2 + "", fac0);

        }

     });

     panel1.add(inButton1);

 

     final JButton exitButton = new JButton("退出");

     exitButton.setBounds(720, 520, 170, 20);

     exitButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           panel1.setVisible(false);

           frame1.setVisible(false);

           frame1.dispose();

        }

     });

     panel1.add(exitButton);

     frame1.setVisible(true);

   }

 

   static void color1(int c, Graphics g1) {

     switch (c) {

     case 1:

        g1.setColor(Color.red);

        break;

     case 2:

        g1.setColor(Color.blue);

        break;

     case 3:

        g1.setColor(Color.orange);

        break;

     case 4:

        g1.setColor(Color.green);

        break;

     case 5:

        g1.setColor(Color.black);

        break;

     case 6:

        g1.setColor(Color.cyan);

        break;

     case 7:

        g1.setColor(Color.yellow);

        break;

     case 8:

        g1.setColor(Color.lightGray);

        break;

     case 9:

        g1.setColor(Color.red);

        break;

     case 10:

        g1.setColor(Color.blue);

        break;

     case 11:

        g1.setColor(Color.orange);

        break;

     case 12:

        g1.setColor(Color.green);

        break;

     case 13:

        g1.setColor(Color.black);

        break;

     case 14:

        g1.setColor(Color.cyan);

        break;

     case 15:

        g1.setColor(Color.yellow);

        break;

     case 16:

        g1.setColor(Color.lightGray);

        break;

     case 17:

        g1.setColor(Color.white);

        break;

     }

   }

 

   static void 数据表查询结构(String 数据表名) {

     String[] s10 = null;

     String[] sk = new String[300];

     int n1 = 0, n2 = 0;

     连接条件 = "";

     main1.driver1();

     if (数据表名.indexOf(",") < 0) { // 单表

        try {

           cont4 = main1.getConn();

           stat4 = cont4.createStatement(

                ResultSet.TYPE_SCROLL_INSENSITIVE,

                ResultSet.CONCUR_UPDATABLE);

           String s1 = "select * from " + 数据表名;

           rs = stat4.executeQuery(s1);

           rsmdt4 = rs.getMetaData();

           列数 = rsmdt4.getColumnCount();

           列名 = new String[列数];

           列数据类型 = new String[列数];

           列数据宽度 = new int[列数];

           for (int i = 0; i < 列数; i++) {

             列名[i] = rsmdt4.getColumnName(i + 1);

           }

           rs.close();

           stat4.close();

           cont4.close();

        } catch (SQLException e1) {

           JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

        }

     } else { // 多表

        s10 = 数据表名.split(",");

        String s0 = "", s1 = "", s2 = "";

        int k = 0;

        for (int i = 0; i < s10.length; i++) {

           try {

             cont4 = main1.getConn();

             stat4 = cont4.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             s1 = "select * from " + s10[i];

             rs = stat4.executeQuery(s1);

             rsmdt4 = rs.getMetaData();

             列数 = rsmdt4.getColumnCount();

             for (int j = 0; j < 列数; j++) { // 读取所有表的列名,同名的加表名

                s2 = rsmdt4.getColumnName(j + 1).toLowerCase(); // 列名

                n2 = n1;

                if (i > 0) { // 加入第2个表之后字段出现同名字段

                   k = 0;

                   for (int j1 = 0; j1 < n2; j1++) {

                     if (sk[j1].equals(s2)) {

                        sk[j1] = s10[i - 1] + "." + sk[j1];

                        if (连接条件.length() == 0) // 连接条件

                           连接条件 = " where " + s10[i - 1] + "."

                                + s2 + " = " + s10[i] + "."

                                + s2;

                        else

                           连接条件 = 连接条件 + " and " + s10[i - 1]

                                + "." + s2 + " = " + s10[i]

                                + "." + s2;

                        sk[n1] = s10[i] + "." + s2 + " as "

                              + (s2 + i);

                        n1++;

                        k = 1;

                        break;

                      }

                   }

                   if (k == 0) {

                      sk[n1] = s2;

                      n1++;

                   }

                } else {

                   sk[n1] = s2;

                   n1++;

                }

             }

             rs.close();

             stat4.close();

             cont4.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

           }

        }

     }

   }

 

   static void 数据表查询数据(String 数据表名) {

     int c = 0;

     int b = 0;

     try {

        cont4 = main1.getConn();

        stat4 = cont4.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

             ResultSet.CONCUR_UPDATABLE);

        s1 = "select " + 字段名表 + "  from " + 数据表名 + 连接条件;

        rs = stat4.executeQuery(s1);

        rsmdt4 = rs.getMetaData();

        列数 = rsmdt4.getColumnCount();

        列名 = new String[列数];

        列数据类型 = new String[列数];

        列数据宽度 = new int[列数];

        for (int i1 = 0; i1 < 列数; i1++) {

           列名[i1] = rsmdt4.getColumnName(i1 + 1);

           列数据类型[i1] = rsmdt4.getColumnTypeName(i1 + 1);

           列数据宽度[i1] = rsmdt4.getColumnDisplaySize(i1 + 1);

        }

        rs.last();

        记录条数 = rs.getRow();

        表格数据 = new String[记录条数][列数];

        while (c < 记录条数) {

           rs.absolute(c + 1);

           while (b < 列数) {

             表格数据[c][b] = rs.getString(b + 1);

             if ((main1.数字数据类型.lastIndexOf("," + 列数据类型[b] + ",")) > 0)

                if ((表格数据[c][b] == null)

                      || (表格数据[c][b].trim().length() == 0))

                   表格数据[c][b] = "0";

             b++;

           }

           c++;

           b = 0;

        }

        rs.close();

        stat4.close();

        cont4.close();

     } catch (SQLException e1) {

        JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

     }

   }

}

103.源码103,公共程序,支持dataPrint5程序打印或打印预览带直方图统计图与数据表格的数据交叉表报表。

/**

 * 程序文件名:statisticalChart3.java

 * 作者:程学先

 * 用途:公共程序,支持dataPrint5程序打印统计图与报表表格。

 * 本程序使用到了jcommon-1.0.16.jarjfreechart-1.0.13.jar包,

 * 需要到官方网站自行下载,并放到P1文件夹下COM文件夹中。

 * 调用语句:statisticalChart3(打印格式文件名,打印表格之数据,打印统计图之数据,

 * 统计图图形X轴字段名,统计图图形Y轴数据字段名)

 * 本程序对数据要求较高,请保证表格中数据无空值。

 * 本程序在学习李钟尉、陈丹丹等老师所著“java开发实战1200例(第2卷)之后设计,特此感谢。

 * 相关饼图、折线图程序请参考该书用例自行设计。

 */

import java.awt.BasicStroke;

import java.awt.BorderLayout;

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.sql.ResultSet;

import javax.imageio.ImageIO;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JFrame;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.BarRenderer;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.HorizontalAlignment;

 

public class statisticalChart3 extends JFrame implements Printable {

   private static statisticalChart3 frame;

   private PageFormat pf;

   private static PreviewCanvas3 canvas;

   private boolean isPreview = false; //是否可以打印

   private boolean previewFlag = false;

   static String 表名;

   private static String s1 = "";

   private static int 记录条数, 记录条数1;

   private static int 表格列数, 表格列数1; //获取列数

   private static String[][] 表格数据;

   private static String[][] 表格数据1;

   private static String path;

   private static int 窗口宽 = 0;

   private static String 文件名;

   private static FileReader fr;

   private static String[] 图表;

   private static Graphics2D g2;

   private static Graphics g;

   private static String 数据字段名;

   private static String 分组字段名;

 

   public static void gainReport() {

     图表 = new String[17];

     if (文件名.length() < 1) {

        JOptionPane.showMessageDialog(null, "缺文件名!");

        return;

     }

     File file1 = new File(文件名);

     try {

        if (file1.exists())

           fr = new FileReader(file1);

        BufferedReader br = new BufferedReader(fr);

        String tempStr = null;

        for (int i = 0; (tempStr = br.readLine()) != null; i++) {//读文件

           图表[i] = tempStr;

        }

     } catch (IOException e2) {

        JOptionPane.showMessageDialog(null, "打开文件错!");

     }

   }

 

   static void means(String 文件名0, String[][] 表格数据2, String[][] 表格数据3,

        String ss1, String ss2) {

     窗口宽 = main1.窗口宽;

     s1 = "";

     图表 = new String[17];

     文件名 = 文件名0;

     数据字段名 = ss1;

     分组字段名 = ss2;

     gainReport();

     记录条数 = 表格数据2.length;

     表格列数 = 表格数据2[0].length;

     表格数据 = new String[记录条数][表格列数];

     for (int i = 0; i < 记录条数; i++)

        for (int j = 0; j < 表格列数; j++)

           表格数据[i][j] = 表格数据2[i][j];

     记录条数1 = 表格数据3.length;

     表格列数1 = 表格数据3[0].length;

     表格数据1 = new String[记录条数1][表格列数1];

     for (int i = 0; i < 记录条数1; i++)

        for (int j = 0; j < 表格列数1; j++)

           表格数据1[i][j] = 表格数据3[i][j];

     frame = new statisticalChart3(); //建立表单,执行构造方法

     frame.setVisible(true);

   }

 

   public statisticalChart3() {

     super();

     JFreeChart chart = createChart();//图表对象

     path = "a1.jpg";//图表存储位置

     int we1 = 窗口宽;

     int we2 = Integer.parseInt(图表[15]);

     try {

        ChartUtilities.writeChartAsJPEG(new FileOutputStream(path), chart,

             we1, we2);//将图表存储为图片,调节数字,可调节所生成图片大小

     } catch (FileNotFoundException e1) {

        e1.printStackTrace();

     } catch (IOException e1) {

        e1.printStackTrace();

     }

     setTitle(图表[0]);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     pf = new PageFormat(); //打印页面

     canvas = new PreviewCanvas3();

     int we3 = Integer.parseInt(图表[6]) + 2; //数据表字号

     int 窗口宽 = we1;

     int 窗口高 = we2 + 记录条数 * we3 + Integer.parseInt(图表[2])

           + Integer.parseInt(图表[9]);

     this.setSize(new Dimension(窗口宽, 窗口高));

     BorderLayout borderLayout = new BorderLayout();

     borderLayout.setHgap(10);

     setLayout(borderLayout);

     canvas.setSize(new Dimension(窗口宽, 窗口高)); //画布大小

     add(canvas, BorderLayout.CENTER);

     final JPanel panel = new JPanel();

     panel.setOpaque(false);

     getContentPane().add(panel, BorderLayout.SOUTH);

     if (main1.n == 1) {

        PrinterJob job = PrinterJob.getPrinterJob();

        if (!job.printDialog()) {//打开打印对话框

           return;

        }

        job.setPrintable(new Printable() { //设置打印内容

           public int print(Graphics graphics, PageFormat pageFormat,

                int pageIndex) throws PrinterException {

             isPreview = true;

             if (pageIndex < 1) {

                statisticalChart3.g = graphics;

                g2 = (Graphics2D) g;

                g2.setStroke(new BasicStroke(1f));

                printPicture(graphics, pageFormat, pageIndex); //绘制打印内容

                return Printable.PAGE_EXISTS;

             } else {

                return Printable.NO_SUCH_PAGE;

             }

           }

        });

        job.setJobName("打印报表");

        try {

           job.print();//调用print()方法,实现打印

        } catch (PrinterException e1) {

           e1.printStackTrace();

        }

     }

   }

   private static JFreeChart createChart() {

     DefaultCategoryDataset dataset = new DefaultCategoryDataset();

     for (int i = 0; i < 记录条数1; i++) {

        if ((表格数据1[i][2] == null) || (表格数据1[i][2].length() == 0))

           表格数据1[i][2] = 0 + "";

        int value = (int) Double.parseDouble(表格数据1[i][2]);

        String sf1 = 表格数据1[i][1];

        String sf0 = 表格数据1[i][0];

        dataset.addValue(value, sf1, sf0);//在数据集中添加数据信息

     }

     JFreeChart chart = ChartFactory.createBarChart("\n\n" + 图表[7], //标题

           分组字段名,数据字段名,dataset, PlotOrientation.VERTICAL,true,

           true,false );

     chart.setBackgroundPaint(Color.white);//背景色

     CategoryPlot plot = (CategoryPlot) chart.getPlot();

     NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();//y轴实例

     rangeAxis.setTickLabelFont(new Font("Sans-serif", Font.PLAIN, 12));//y轴字体

     rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

      rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

     BarRenderer renderer = (BarRenderer) plot.getRenderer();//柱形对象

     GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,

           0.0f, new Color(0, 0, 64));

     GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,

           0.0f, new Color(0, 64, 0));

     renderer.setSeriesPaint(0, gp0);//柱的颜色

     renderer.setSeriesPaint(1, gp1);

     CategoryAxis domainAxis = plot.getDomainAxis();//x轴实例

     domainAxis.setTickLabelFont(new Font("Sans-serif", Font.PLAIN, 20));

     domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 20));

      domainAxis.setCategoryLabelPositions(CategoryLabelPositions

           .createUpRotationLabelPositions(Math.PI / 6.0));

     TextTitle textTitle = chart.getTitle();

     textTitle.setFont(new Font(图表[8], Font.PLAIN, Integer.parseInt(图表[9])));

     chart.getLegend().setItemFont(

           new Font(图表[8], Font.PLAIN, Integer.parseInt(图表[9])));

      chart.getLegend().setHorizontalAlignment(HorizontalAlignment.CENTER);

     return chart;

   }

 

  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)

        throws PrinterException {

     printPicture(graphics, pageFormat, pageIndex);//打印内容

     return Printable.PAGE_EXISTS;

   }

 

   class PreviewCanvas3 extends Canvas {

     public void paint(Graphics g) {

        try {

           super.paint(g);

           g2 = (Graphics2D) g;

           g2.translate(10, 10);

           int x = (int) (pf.getImageableX() - 1);

           int y = (int) (pf.getImageableY() - 1);

           int width = (int) (pf.getImageableWidth() + 1);

           int height = (int) (pf.getImageableHeight() + 1);

           int mw = (int) pf.getWidth();

           int mh = (int) pf.getHeight();

           g2.drawRect(0, 0, mw, mh);

           g2.setColor(new Color(255, 253, 234));

           g2.fillRect(1, 1, mw - 1 + 300, mh - 1);

           g2.setStroke(new BasicStroke(1f, BasicStroke.CAP_ROUND,

               BasicStroke.JOIN_ROUND, 10f, new float[] { 5, 5 }, 0f));

           g2.setColor(Color.BLACK);

           g2.setColor(Color.WHITE);

           g2.fillRect(x + 1, y + 1, width - 1 + 300, height - 1);

           statisticalChart3.this.print(g, pf, 0);

        } catch (PrinterException e) {

           e.printStackTrace();

        }

     }

   }

   public void printPicture(Graphics graphics, PageFormat pageFormat,

        int pageIndex) {

     int x = (int) pageFormat.getImageableX() - 20;

     int y = (int) pageFormat.getImageableY()-50;  //标题顶部高度

     int w = (int) pageFormat.getImageableWidth();

     int w2=(int) (Integer.parseInt(图表[6]) * 1.5);

     g2 = (Graphics2D) graphics;

     g2.setColor(Color.BLUE);

     g2.setFont(new Font(图表[1], Font.BOLD, Integer.parseInt(图表[2])));

     g2.drawString(图表[0], x + 60, y + 30);

     g2.setColor(Color.BLACK);

     g2.setFont(new Font(图表[5], Font.PLAIN, Integer.parseInt(图表[6])));

     g2.setStroke(new BasicStroke(1f));

     String[] we5 = 图表[3].split(",");

     String[] we6 = 图表[4].split(",");

     int[] we7 = new int[we6.length];

     int we8 = Integer.parseInt(图表[6]);

     for (int i = 0; i < we6.length; i++)

        we7[i] = Integer.parseInt(we6[i]);

     try {

        y = y + 30 + Integer.parseInt(图表[2]);

        int y1 = y;

        int xe1 = x;

        g2.drawLine(xe1, y1 - 20, xe1, y + (记录条数-1)*w2);//左边

        for (int i = 0; i < we6.length; i++) {

           g2.drawLine(xe1, y - 20, xe1 + we7[i], y - 20);//水平表格线

           g2.drawString(we5[i], xe1 + 5, y - 5);//标签

           g2.drawLine(xe1 + we7[i], y1 - 20, xe1 + we7[i], y + 10);//右边线

           xe1 = xe1 + we7[i];

        }

        for (int i = 0; i < 记录条数; i++) {

           y1 = y;

           y = y + (int) (Integer.parseInt(图表[6]) * 1.5);

           xe1 = x;

           g2.drawLine(xe1, y1, xe1, y);

           for (int j = 0; j < we6.length; j++) {

             g2.drawLine(xe1, y1, xe1 + we7[j], y1);

             g2.drawString(表格数据[i][j], xe1 + 5, y - 5);

             g2.drawLine(xe1 + we7[j], y1, xe1 + we7[j], y);

             if (i == 记录条数 - 1)

                g2.drawLine(xe1, y, xe1 + we7[j], y);

             xe1 = xe1 + we7[j];

           }

        }

        File file = new File("a1.jpg"); //将生成的图表形成一个jpg文件

        BufferedImage image1 = ImageIO.read(file);

        g2.drawImage(image1, 10, y + 20, 820, 300, this);

     } catch (Exception ex) {

        ex.printStackTrace();

     }

     isPreview = true;

     previewFlag = true;

   }

}

 

版本2

101.源码101,部件程序,带统计图数据交叉表报表格式文件生成程序。用于辅助生成打印带图表的表格式报表格式文件。交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系

/*

 * 程序文件名:printFormat4.java

 * 作者:程学先

 * 功能:打印数据交叉表格式文件生成程序。用于辅助生成打印带图表的表格式报表格式文件。

 * 交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系。

 */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.tree.DefaultMutableTreeNode;

import java.io.*;

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Vector;

 

public class printFormat4 extends JFrame {

   private static printFormat4 frame1 = new printFormat4();

   private static JPanel panel1 = new JPanel();

   private static List list1 = new List();

   private static JLabel fl0, fl1, fl2, fl3, fl10;

   private static JTextField fa0, fa1, fa2, fa3, fa10;

   private static JLabel fla0, fla1, fla2;

   private static JTextField faa0, faa1, faa2;

   private static JLabel flb0, flb1, flb2;

   private static JTextField fab0, fab1, fab2;

   private static JLabel flc0, flc1, flc2, flc3, flc4, flc5, flc6;

   private static JTextField fac0, fac1, fac2, fac3, fac4, fac5, fac6;

   private static String variabl2327;

   private static int variabl1823 = -1, variabl1947 = 0, variabl2193 = 0,

        variabl1739 = 0, variabl2651 = 0;

   private static int variabl2127 = 0, variabl1853 = 0;

   private static String[][] variabl2197 = null;

   private static String[][] variabl21971 = null;

   private static ArrayList<String> variabl1665 = new ArrayList();

   private static String[] variabl25171;

   private static String[] variabl2663;

   private static String[] variabl1501;

   private static int[] variabl1489;

   private static String[] variabl2157 = new String[12];

   private static int[] variabl21571 = new int[12];

   private static File file1;

   private static FileReader fr;

   static Connection con;

   private static Statement sta;

   static String s1 = "", s2 = "";

   private static ResultSet rs;

   private static ResultSetMetaData rsmd;

   private static ArrayList variabl26031 = new ArrayList();

   private static String[] variabl2607 = { "5", "6", "7", "8", "9", "10",

        "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36",

        "48", "72" };

   private static String[] variabl2629 = { "宋体", "楷体", "黑体", "仿宋_GB2312" };

   private static String[] variabl2511 = { "柱形图", "饼形图", "折线图" };

   private static int variabl2483 = main1.variabl2483;

   private static String variabl2217;

   private static String variabl2049 = "";

   private static String variabl1633 = "";

 

   static void means(String[] parameter) {

     frame1 = new printFormat4();

     panel1 = new JPanel();

     list1 = new List();

     variabl1823 = -1;

     variabl1947 = 0;

     variabl2193 = 0;

     variabl1739 = 0;

     variabl2651 = 0;

     variabl2127 = 0;

     variabl1853 = 0;

     variabl1665 = new ArrayList();

     variabl2157 = new String[12];

     variabl21571 = new int[12];

     s1 = "";

     s2 = "";

     variabl26031 = new ArrayList();

     variabl2511[0] = "柱形图";

     variabl2511[1] = "饼形图";

     variabl2511[2] = "折线图";

     variabl2607 = new String[19];

     variabl2629 = new String[4];

     variabl2607[0] = "5";

     variabl2607[1] = "6";

     variabl2607[2] = "7";

     variabl2607[3] = "8";

     variabl2607[4] = "9";

     variabl2607[5] = "10";

     variabl2607[6] = "11";

     variabl2607[7] = "12";

     variabl2607[8] = "14";

     variabl2607[9] = "16";

     variabl2607[10] = "18";

     variabl2607[11] = "20";

     variabl2607[12] = "22";

     variabl2607[13] = "24";

     variabl2607[14] = "26";

     variabl2607[15] = "28";

     variabl2607[16] = "36";

     variabl2607[17] = "48";

     variabl2607[18] = "72";

     variabl2629[0] = "宋体";

     variabl2629[1] = "楷体";

     variabl2629[2] = "黑体";

     variabl2629[3] = "仿宋_GB2312";

     frame1.setTitle("图表类报表打印格式设计与打印程序。                       作者:程学先");

     frame1.setBounds(10, 10, 900, 580);

      frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

     panel1.setBounds(10, 10, 880, 570);

     panel1.setLayout(null);

     String[] variabl251701 = { "标签名", "字段名", "宽度", "字体", "字号" };

     main1.driver1();

     variabl2217 = parameter[4];

     数据表查询结构(variabl2217);

     fl10 = new JLabel("文件名");

     fl10.setBounds(30, 30, 50, 20);

     panel1.add(fl10);

     fa10 = new JTextField("", 20);

     fa10.setBounds(80, 30, 600, 20);

     panel1.add(fa10);

     fa10.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {

           variabl2663 = new String[37];

           if (fa10.getText().length() < 1) {

             final JFileChooser fc = new JFileChooser(".\\");

             fc.showOpenDialog(null);

             if (fc.getSelectedFile() != null) {

                fa10.setText(fc.getSelectedFile().toString());

                file1 = new File(fa10.getText());

                try {

                   if ((fa10.getText().length() > 0)

                        && (file1.exists())) {

                      fr = new FileReader(file1);

                      BufferedReader br = new BufferedReader(fr);

                      String tempStr = null;

                      int y = 0, b = 0;

                      for (int i = 0; (tempStr = br.readLine()) != null; i++) {

                        variabl2663[i] = tempStr;

                        y = i;

                      }

                      if (y >= 16) {

                        if (y >= 0)

                           faa0.setText(variabl2663[0]);

                        if (y >= 1)

                           faa1.setText(variabl2663[1]);

                        if (y >= 2)

                           faa2.setText(variabl2663[2]);

                        if (y >= 3)

                           fa0.setText(variabl2663[3]);

                        if (y >= 4)

                           fa1.setText(variabl2663[4]);

                        if (y >= 5)

                           fa2.setText(variabl2663[5]);

                        if (y >= 6)

                           fa3.setText(variabl2663[6]);

                        if (y >= 7)

                           fab0.setText(variabl2663[7]);

                        if (y >= 8)

                           fab1.setText(variabl2663[8]);

                        if (y >= 9)

                           fab2.setText(variabl2663[9]);

                        if (y >= 10)

                           fac0.setText(variabl2663[10]);

                        if (y >= 11)

                           fac1.setText(variabl2663[11]);

                        if (y >= 12)

                           fac2.setText(variabl2663[12]);

                        if (y >= 13)

                           fac3.setText(variabl2663[13]);

                        if (y >= 14)

                           fac4.setText(variabl2663[14]);

                        if (y >= 15)

                           fac5.setText(variabl2663[15]);

                        if (y >= 16)

                           fac6.setText(variabl2663[16]);

                      }

                   } else {

                      if (fa10.getText().length() > 0)

                        file1.createNewFile();

                   }

                } catch (IOException e2) {

                   JOptionPane.showMessageDialog(null, "打开文件错!");

                }

             }

           }

        }

     });

 

     fla0 = new JLabel("表格标题");

     fla0.setBounds(30, 70, 80, 20);

     panel1.add(fla0);

     faa0 = new JTextField("", 20);

     faa0.setBounds(110, 70, 300, 20);

     panel1.add(faa0);

     faa0.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 0;

           list1.removeAll();

        }

     });

     fla1 = new JLabel("表题字体");

     fla1.setBounds(420, 70, 70, 20);

     panel1.add(fla1);

     faa1 = new JTextField("", 20);

     faa1.setBounds(490, 70, 60, 20);

     panel1.add(faa1);

     faa1.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 1;

           list1.removeAll();

           for (int i = 0; i < variabl2629.length; i++) {

             list1.add(variabl2629[i]);

           }

        }

     });

     fla2 = new JLabel("表题字号");

     fla2.setBounds(560, 70, 70, 20);

     panel1.add(fla2);

     faa2 = new JTextField("", 20);

     faa2.setBounds(630, 70, 70, 20);

     panel1.add(faa2);

     faa2.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 2;

           list1.removeAll();

           for (int i = 0; i < variabl2607.length; i++) {

             list1.add(variabl2607[i]);

           }

        }

     });

 

     fl2 = new JLabel("数据字体");

     panel1.add(fl2);

     fa2 = new JTextField("", 20);

     panel1.add(fa2);

     fl2.setBounds(30, 110, 70, 20);

     fa2.setBounds(100, 110, 120, 20);

     fa2.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 5;

           list1.removeAll();

           for (int i = 0; i < variabl2629.length; i++) {

             list1.add(variabl2629[i]);

           }

        }

     });

 

     fl3 = new JLabel("数据字号");

     panel1.add(fl3);

     fa3 = new JTextField("", 20);

     panel1.add(fa3);

     fl3.setBounds(230, 110, 70, 20);

     fa3.setBounds(300, 110, 110, 20);

     fa3.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 6;

           list1.removeAll();

           for (int i = 0; i < variabl2607.length; i++) {

             list1.add(variabl2607[i]);

           }

        }

     });

 

     fl0 = new JLabel("报表字段名逗号分隔");

     fl0.setBounds(30, 140, 150, 20);

     panel1.add(fl0);

     fa0 = new JTextField("", 20);

     fa0.setBounds(30, 160, 670, 20);

     panel1.add(fa0);

     fa0.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 3;

           list1.removeAll();

           for (int i = 0; i < variabl2651; i++) {

             list1.add(variabl25171[i]);

           }

        }

     });

     fl1 = new JLabel("宽度逗号分隔");

     fl1.setBounds(30, 190, 140, 20);

     panel1.add(fl1);

     fa1 = new JTextField("", 20);

     fa1.setBounds(30, 210, 670, 20);

     panel1.add(fa1);

     fa1.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 4;

           list1.removeAll();

        }

     });

 

     flb0 = new JLabel("统计图标题");

     flb0.setBounds(30, 260, 80, 20);

     panel1.add(flb0);

     fab0 = new JTextField("", 20);

     fab0.setBounds(110, 260, 300, 20);

     panel1.add(fab0);

     fab0.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 7;

           list1.removeAll();

        }

     });

     flb1 = new JLabel("图题字体");

     flb1.setBounds(420, 260, 70, 20);

     panel1.add(flb1);

     fab1 = new JTextField("", 20);

     fab1.setBounds(490, 260, 60, 20);

     panel1.add(fab1);

     fab1.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 8;

           list1.removeAll();

           for (int i = 0; i < variabl2629.length; i++) {

             list1.add(variabl2629[i]);

           }

        }

     });

     flb2 = new JLabel("图题字号");

     flb2.setBounds(560, 260, 70, 20);

     panel1.add(flb2);

     fab2 = new JTextField("", 20);

     fab2.setBounds(630, 260, 70, 20);

     panel1.add(fab2);

     fab2.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 9;

           list1.removeAll();

           for (int i = 0; i < variabl2607.length; i++) {

             list1.add(variabl2607[i]);

           }

        }

     });

 

     flc0 = new JLabel("图形分类字段名(X轴数据名)");

     flc0.setBounds(30, 300, 200, 20);

     panel1.add(flc0);

     fac0 = new JTextField("", 20);

     fac0.setBounds(30, 330, 200, 20);

     panel1.add(fac0);

     fac0.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 10;

           list1.removeAll();

           for (int i = 0; i < variabl2651; i++) {

             list1.add(variabl25171[i]);

           }

        }

     });

     flc1 = new JLabel("图形大类字段名(X轴分组数据名)");

     flc1.setBounds(240, 300, 200, 20);

     panel1.add(flc1);

     fac1 = new JTextField("", 20);

     fac1.setBounds(240, 330, 200, 20);

     panel1.add(fac1);

     fac1.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 11;

           list1.removeAll();

           for (int i = 0; i < variabl2651; i++) {

             list1.add(variabl25171[i]);

           }

        }

     });

     flc2 = new JLabel("图形数据字段名(Y轴数据名)");

     flc2.setBounds(450, 300, 200, 20);

     panel1.add(flc2);

     fac2 = new JTextField("", 20);

     fac2.setBounds(450, 330, 200, 20);

     panel1.add(fac2);

     fac2.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 12;

           list1.removeAll();

           for (int i = 0; i < variabl2651; i++) {

             list1.add(variabl25171[i]);

           }

        }

     });

     flc3 = new JLabel("图形类型");

     flc3.setBounds(30, 370, 70, 20);

     panel1.add(flc3);

     fac3 = new JTextField("", 20);

     fac3.setBounds(100, 370, 100, 20);

     panel1.add(fac3);

     fac3.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 13;

           list1.removeAll();

           for (int i = 0; i < variabl2511.length; i++) {

             list1.add(variabl2511[i]);

           }

        }

     });

     flc4 = new JLabel("图形宽度");

     flc4.setBounds(210, 370, 60, 20);

     panel1.add(flc4);

     fac4 = new JTextField("", 20);

     fac4.setBounds(270, 370, 80, 20);

     panel1.add(fac4);

     fac4.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 14;

           list1.removeAll();

           String sk[] = fa1.getText().split(",");

           int wh4 = 0;

           for (int i = 0; i < sk.length; i++)

             wh4 = wh4 + Integer.parseInt(sk[i]);

           fac4.setText(wh4 + "");

        }

     });

     flc5 = new JLabel("图形高度");

     flc5.setBounds(360, 370, 60, 20);

     panel1.add(flc5);

     fac5 = new JTextField("", 20);

     fac5.setBounds(420, 370, 80, 20);

     panel1.add(fac5);

     fac5.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 15;

           list1.removeAll();

           int wh4 = 0;

           if ((faa2.getText().length() > 0)

                && (faa2.getText().compareTo("0") > 0))

             wh4 = Integer.parseInt(faa2.getText());

           if ((fa3.getText().length() > 0)

                && (fa3.getText().compareTo("0") > 0))

             wh4 = wh4 + variabl1853 * Integer.parseInt(fa3.getText());

           else

             wh4 = wh4 + variabl1853 * 20;

           if ((fab2.getText().length() > 0)

                && (fab2.getText().compareTo("0") > 0))

             wh4 = wh4 + Integer.parseInt(fab2.getText());

           wh4 = 800 - wh4;

           fac5.setText(wh4 + "");

        }

     });

     flc6 = new JLabel("统计函数序号");

     flc6.setBounds(510, 370, 90, 20);

     panel1.add(flc6);

     fac6 = new JTextField("", 20);

     fac6.setBounds(600, 370, 100, 20);

     panel1.add(fac6);

     fac6.addFocusListener(new FocusAdapter() {

        public void focusGained(final FocusEvent arg0) {

           variabl1739 = 16;

           list1.removeAll();

           list1.add("按记录");

           list1.add("分组求和");

           list1.add("分组平均");

        }

     });

     list1.setBounds(730, 30, 120, 410);

      panel1.add(list1);

     list1.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {

           int w1 = 0, w2 = 0;

           if (list1.getSelectedIndex() >= 0) {

             int wh1, wh2, h1, h2;

             String s0 = "";

             switch (variabl1739) {

             case 0:

                if (fa0.getText().length() == 0)

                   s0 = list1.getSelectedItem();

                else

                   s0 = fa0.getText() + "," + list1.getSelectedItem();

                fa0.setText(s0);

                数据表查询结构(s0);

                break;

             case 1:

                faa1.setText(list1.getSelectedItem());

                break;

             case 2:

                faa2.setText(list1.getSelectedItem());

                break;

             case 3:

                wh1 = list1.getSelectedIndex();

                wh2 = variabl1489[wh1];

                if (variabl25171[wh1].length() > wh2)

                   wh2 = variabl25171[wh1].length();

                if (fa3.getText().length() > 0)

                   wh2 = wh2 * Integer.parseInt(fa3.getText());

                else

                   wh2 = wh2 * 12;

 

                if (fa0.getText().length() == 0) {

                   fa0.setText(list1.getSelectedItem());

                   fa1.setText(wh2 + "");

                } else {

                   fa0.setText(fa0.getText() + ","

                        + list1.getSelectedItem());

                   fa1.setText(fa1.getText() + "," + wh2);

                }

                break;

             case 5:

                fa2.setText(list1.getSelectedItem());

                break;

             case 6:

                fa3.setText(list1.getSelectedItem());

                break;

             case 8:

                fab1.setText(list1.getSelectedItem());

                break;

             case 9:

                fab2.setText(list1.getSelectedItem());

                break;

             case 10:

                fac0.setText(list1.getSelectedItem());

                break;

             case 11:

                fac1.setText(list1.getSelectedItem());

                break;

             case 12:

                fac2.setText(list1.getSelectedItem());

                break;

             case 13:

                fac3.setText(list1.getSelectedIndex() + "");

                break;

             case 16:

                fac6.setText(list1.getSelectedIndex() + "");

                break;

             }

           }

        }

     });

 

     final JButton creButton = new JButton("格式内容存盘");

     creButton.setBounds(30, 440, 170, 20);

     creButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           char x1 = 10, x2 = 13, x3 = '"', x4 = ',';

           file1 = new File(fa10.getText());

           FileOutputStream fs;

           try {

             fs = new FileOutputStream(file1);

             fs.write((faa0.getText() + x1).getBytes());

             fs.write((faa1.getText() + x1).getBytes());

             fs.write((faa2.getText() + x1).getBytes());

             fs.write((fa0.getText() + x1).getBytes());

             fs.write((fa1.getText() + x1).getBytes());

             fs.write((fa2.getText() + x1).getBytes());

             fs.write((fa3.getText() + x1).getBytes());

             fs.write((fab0.getText() + x1).getBytes());

             fs.write((fab1.getText() + x1).getBytes());

             fs.write((fab2.getText() + x1).getBytes());

             fs.write((fac0.getText() + x1).getBytes());

             fs.write((fac1.getText() + x1).getBytes());

             fs.write((fac2.getText() + x1).getBytes());

             fs.write((fac3.getText() + x1).getBytes());

             fs.write((fac4.getText() + x1).getBytes());

             fs.write((fac5.getText() + x1).getBytes());

             fs.write((fac6.getText() + x1).getBytes());

           } catch (IOException e2) {

             JOptionPane.showMessageDialog(null, "写文件错。");

           }

        }

     });

     panel1.add(creButton);

 

     final JButton clearButton = new JButton("清参数文本框");

     clearButton.setBounds(200, 440, 170, 20);

     clearButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           fa1.setText("");

           fa2.setText("0");

        }

     });

     panel1.add(clearButton);

 

     final JButton printButton = new JButton("打印预览");

     printButton.setBounds(370, 440, 170, 20);

     printButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             s1 = "select " + fa0.getText() + " from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                s1 = s1 + variabl2049;

             rs = sta.executeQuery(s1);

             rs.last();

             rsmd = rs.getMetaData();

             variabl1853 = rs.getRow();

             variabl2651 = rsmd.getColumnCount();

             variabl25171 = new String[variabl2651];

             variabl1501 = new String[variabl2651];

             variabl1489 = new int[variabl2651];

             variabl2197 = new String[variabl1853][variabl2651];

             variabl1739 = 1;

             for (int i = 0; i < variabl2651; i++) {

                variabl25171[i] = rsmd.getColumnName(i + 1);

                variabl1501[i] = rsmd.getColumnTypeName(i + 1);

                variabl1489[i] = rsmd.getColumnDisplaySize(i + 1);

             }

             for (int i = 0; i < variabl1853; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < variabl2651; j++) {

                   variabl2197[i][j] = rs.getString(j + 1);

                   if ((variabl2197[i][j] == null)

                        || (variabl2197[i][j].trim().length() == 0))

                      variabl2197[i][j] = "0";

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据结构出错!" + s1);

           }

           String sc0 = fac0.getText() + "," + fac1.getText() + ","

                + fac2.getText();

           if ((fac6.getText() == null) || (fac6.getText().length() == 0))

             fac6.setText("0");

           if (Integer.parseInt(fac6.getText()) == 0) {

             sc0 = "select " + sc0 + " from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6.getText()) == 1) {

             sc0 = "select " + fac0.getText() + " ' ' as x " + "sum("

                   + fac2.getText() + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6.getText()) == 2) {

             sc0 = "select " + fac0.getText() + " ' ' as x " + "avg("

                   + fac2.getText() + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else {

             JOptionPane.showMessageDialog(null, "统计类型输入错误!");

             return;

           }

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             rs = sta.executeQuery(sc0);

             rs.last();

             rsmd = rs.getMetaData();

             int variabl18531 = rs.getRow();

             int variabl26511 = rsmd.getColumnCount();

             String[] variabl25172 = new String[variabl26511];

             variabl21971 = new String[variabl18531][variabl26511];

             for (int i = 0; i < variabl18531; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < variabl26511; j++) {

                   variabl21971[i][j] = rs.getString(j + 1);

                   if ((variabl21971[i][j] == null)

                        || (variabl21971[i][j].trim().length() == 0))

                      variabl2197[i][j] = "0";

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据结构出错!" + s1);

           }

           statisticalChart3.means(fa10.getText(), variabl2197,

                variabl21971, fac0.getText(), fac2.getText());

 

        }

     });

     panel1.add(printButton);

 

     final JButton exitButton = new JButton("退出");

     exitButton.setBounds(540, 440, 170, 20);

     exitButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           panel1.setVisible(false);

           frame1.setVisible(false);

           frame1.dispose();

        }

     });

     panel1.add(exitButton);

     frame1.add(panel1);

     frame1.setVisible(true);

   }

 

   static void 数据表查询结构(String variabl2217) {

     String[] s10 = null;

     String[] sk = new String[300];

     int n1 = 0, n2 = 0;

     variabl1633 = "";

     variabl2049 = "";

     if (variabl2217.indexOf(",") < 0) {

        try {

           con = main1.getConn();

           sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                ResultSet.CONCUR_UPDATABLE);

           String s1 = "select * from " + variabl2217;

           rs = sta.executeQuery(s1);

           rsmd = rs.getMetaData();

           variabl2651 = rsmd.getColumnCount();

           variabl25171 = new String[variabl2651];

           variabl1633 = "";

           for (int i = 0; i < variabl2651; i++) {

             variabl25171[i] = rsmd.getColumnName(i + 1);

             sk[i] = variabl25171[i];

             n1++;

             if (variabl1633.length() == 0)

                variabl1633 = variabl25171[i];

             else

                variabl1633 = variabl1633 + "," + variabl25171[i];

           }

           rs.close();

           sta.close();

           con.close();

        } catch (SQLException e1) {

           JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

        }

     } else {

        s10 = variabl2217.split(",");

        String s0 = "", s1 = "", s2 = "";

        int k = 0;

        for (int i = 0; i < s10.length; i++) {

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             s1 = "select * from " + s10[i];

             rs = sta.executeQuery(s1);

             rsmd = rs.getMetaData();

             variabl2651 = rsmd.getColumnCount();

             for (int j = 0; j < variabl2651; j++) {

                s2 = rsmd.getColumnName(j + 1).toLowerCase();

                n2 = n1;

                if (i > 0) {

                   k = 0;

                   for (int j1 = 0; j1 < n2; j1++) {

                      if (sk[j1].equals(s2)) {

                        sk[j1] = s10[i - 1] + "." + sk[j1];

                        if (variabl2049.length() == 0)

                           variabl2049 = " where " + s10[i - 1]

                                + "." + s2 + " = " + s10[i]

                                + "." + s2;

                        else

                           variabl2049 = variabl2049 + " and "

                                + s10[i - 1] + "." + s2 + " = "

                                + s10[i] + "." + s2;

                         sk[n1] = s10[i] + "." + s2 + " as "

                              + (s2 + i);

                        n1++;

                        k = 1;

                        break;

                      }

                   }

                   if (k == 0) {

                      sk[n1] = s2;

                      n1++;

                   }

                } else {

                   sk[n1] = s2;

                   n1++;

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

           }

        }

        variabl1633 = "";

        for (int j1 = 0; j1 < n1; j1++)

           if (variabl1633.length() == 0)

             variabl1633 = sk[j1];

           else

             variabl1633 = variabl1633 + "," + sk[j1];

     }

     int c = 0, b = 0;

     try {

        con = main1.getConn();

        sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

             ResultSet.CONCUR_UPDATABLE);

        s1 = "select " + variabl1633 + "  from " + variabl2217

             + variabl2049;

        rs = sta.executeQuery(s1);

        rsmd = rs.getMetaData();

        variabl2651 = rsmd.getColumnCount();

        variabl25171 = new String[variabl2651];

        variabl1501 = new String[variabl2651];

        variabl1489 = new int[variabl2651];

        for (int i = 0; i < variabl2651; i++) {

           variabl25171[i] = sk[i];

           variabl1501[i] = rsmd.getColumnTypeName(i + 1);

           variabl1489[i] = rsmd.getColumnDisplaySize(i + 1);

        }

        rs.last();

        variabl1853 = rs.getRow();

        variabl2197 = new String[variabl1853][variabl2651];

        while (c < variabl1853) {

           rs.absolute(c + 1);

           while (b < variabl2651) {

             variabl2197[c][b] = rs.getString(b + 1);

             if ((variabl2197[c][b] == null)

                   || (variabl2197[c][b].trim().length() == 0))

                variabl2197[c][b] = "0";

             else if (variabl2197[c][b] == null)

                variabl2197[c][b] = "";

             b++;

           }

           c++;

           b = 0;

        }

        rs.close();

        sta.close();

        con.close();

     } catch (SQLException e1) {

        JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

     }

   }

}

 

102.源码102,部件程序,根据printFormat4.java生成的格式文件打印或打印预览带直方图统计图与数据表格的数据交叉表报表。

/**

 * 程序文件名:dataPrint5.java

 * 作者:程学先 

 * 用途:根据printFormat4.java生成的格式文件打印或打印预览带统计图表格的数据交叉表报表。 

 * 交叉表分析数据表中三列数据(其中之一为数字数据类型)之间关系。

 */

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

 

public class dataPrint5 extends JComponent {

   private static JFrame frame1;

   static Connection con;

   private static ResultSetMetaData rsmd;

   private static Statement sta;

   private static Connection cont4;

   private static Statement stat4;

   private static ResultSetMetaData rsmdt4;

   private static ResultSet rs;

   private static String variabl2603;

   private static String s1 = "";

   static int variabl2281 = 0, variabl2371 = 0, variabl1415 = 20;

   private static int variabl1853 = 0, variabl2651 = 0, variabl2305 = 0;

   private static String[] variabl2517;

   private static String[] variabl1997;

   private static String[] variabl1501;

   private static int[] variabl1489;

   private static String[][] variabl2197;

   private static String[] variabl2231;

   static String[] variabl2253;

   static int[][] variabl20232;

   static int[] variabl20231;

   static int variabl1727 = 1000, variabl1783 = 600;

   static int variabl1757 = 100, variabl2055 = variabl1783;

   private static ArrayList<struct1> title = new ArrayList<struct1>();

   private static ArrayList<struct2> tableTitle = new ArrayList<struct2>();

   private static ArrayList<struct3> table1 = new ArrayList<struct3>();

   private static ArrayList<struct4> table2 = new ArrayList<struct4>();

   private static int variabl2681 = 600;

   private static int x1 = 0, y1 = 0, w1 = 0, h1 = 0, variabl2661 = 0,

        variabl2657 = 0;

   private static int variabl2347 = 0;

   private static Graphics2D g2;

   private static Graphics g;

   private static String faa0, faa1, faa2;

   private static String fab0, fab1, fab2;

   private static String fac0, fac1, fac2, fac3, fac4, fac5, fac6;

   private static String fa0, fa1, fa2, fa3, fa10;

   private static String[][] variabl21971 = null;

   private static String variabl2217;

   private static String variabl2049;

   private static String variabl1633 = "";

   private static String variabl1079;

   private static String[] variabl2663;

   private static int variabl18531;

   private static int variabl26511;

 

   static void title() {

     struct2[] variabl2533 = new struct2[tableTitle.size()];

     variabl2661 = Integer.valueOf(tableTitle.get(0).tableTitle[3]);

     variabl2661 = variabl2661 / 2;

     for (int i = 0; i < tableTitle.size(); i++)

        variabl2533[i] = tableTitle.get(i);

     g2.setFont(new Font(variabl2533[0].tableTitle[6], Font.PLAIN, Integer

           .valueOf(variabl2533[0].tableTitle[7])));

     for (int i = 0; i < tableTitle.size(); i++) {

        x1 = variabl2661;

        y1 = variabl2657;

        w1 = Integer.valueOf(variabl2533[i].tableTitle[3]);

        h1 = Integer.valueOf(variabl2533[i].tableTitle[4]);

        g2.drawLine(x1, y1, x1 + w1, y1);

        g2.drawLine(x1, y1, x1, y1 + h1);

        g2.drawString(variabl2533[i].tableTitle[0], variabl2661 + 2,

             variabl2657 + h1);

        if (variabl2533[i].tableTitle[8].equals(""))

           g2.drawLine(x1, y1 + h1, x1 + w1, y1 + h1);

        if (variabl2533[i].tableTitle[9].equals(""))

           g2.drawLine(x1 + w1, y1, x1 + w1, y1 + h1);

        variabl2661 = variabl2661 + w1;

        if ((i < tableTitle.size() - 1)

             && (Integer.valueOf(variabl2533[i].tableTitle[1]) < Integer

                   .valueOf(variabl2533[i + 1].tableTitle[1])))

           variabl2657 = variabl2657 + h1;

     }

     variabl2657 = variabl2657 + h1;

   }

 

   protected void paintComponent(Graphics g1) {

     g = g1;

     g2 = (Graphics2D) g;

     struct1[] variabl2535 = new struct1[title.size()];

     variabl2657 = 0;

     variabl2661 = 0;

     for (int i = 0; i < title.size(); i++) {

        variabl2535[i] = title.get(i);

        g2.setFont(new Font(variabl2535[i].title[8], Font.PLAIN, Integer

             .valueOf(variabl2535[i].title[9])));

        x1 = Integer.valueOf(variabl2535[i].title[6]);

        y1 = Integer.valueOf(variabl2535[i].title[7]);

        g2.drawString(variabl2535[i].title[0], x1, y1);

        variabl2657 = y1 + Integer.valueOf(variabl2535[i].title[5]);

     }

     title();

     struct3[] variabl2685 = new struct3[table1.size()];

     int[] w10 = new int[table1.size()];

     int[] h10 = new int[table1.size()];

     int[] h20 = new int[table1.size()];

     for (int i = 0; i < table1.size(); i++) {

        variabl2685[i] = table1.get(i);

        w10[i] = Integer.valueOf(variabl2685[i].table1[2]);

        h10[i] = Integer.valueOf(variabl2685[i].table1[3]);

        h20[i] = Integer.valueOf(variabl2685[i].table1[5]);

     }

     y1 = variabl2657;

     g2.setFont(new Font(variabl2685[0].table1[4], Font.PLAIN, Integer

           .valueOf(variabl2685[0].table1[5])));

     for (int j = 0; j < variabl1853; j++) {

        x1 = Integer.valueOf(tableTitle.get(0).tableTitle[3]) / 2;

        g2.drawLine(x1, y1, x1, y1 + h1);

        for (int i = 0; i < table1.size(); i++) {

           if (variabl2197[j][i] == null)

             variabl2197[j][i] = " ";

           g2.drawString(variabl2197[j][i], x1 + 2, y1 + h20[i]);

           g2.drawLine(x1, y1 + h10[i], x1 + w10[i], y1 + h10[i]);

           g2.drawLine(x1 + w10[i], y1, x1 + w10[i], y1 + h10[i]);

           x1 = x1 + w10[i];

        }

        y1 = y1 + h10[table1.size() - 1];

        if (y1 >= variabl2681) {

           struct4[] variabl2653 = new struct4[table2.size()];

           for (int i1 = 0; i1 < table2.size(); i1++) {

             variabl2653[i1] = table2.get(i1);

             g2.setFont(new Font(variabl2653[i1].table2[8], Font.PLAIN,

                   Integer.valueOf(variabl2653[i1].table2[9])));

             x1 = Integer.valueOf(variabl2653[i1].table2[6]);

             y1 = Integer.valueOf(variabl2653[i1].table2[7]);

             g2.drawString(variabl2653[i1].table2[0], x1, y1);

             variabl2657 = y1

                   + Integer.valueOf(variabl2653[i1].table2[5]);

           }

           y1 = 640;

           break;

        }

     }

     y1 = y1 + h10[table1.size() - 1];

   }

 

   static void means(String[] parameter) {

     frame1 = new JFrame();

     frame1.setLayout(null);

     frame1.setTitle("图表式报表打印与打印预览程序                                                                                                                                                                                              作者:程学先");

     frame1.setBounds(10, 10, 1000, 660);

     frame1.getContentPane().setLayout(null);

     final JPanel panel1 = new JPanel();

     panel1.setBounds(10, 10, 1000, 660);

     panel1.setLayout(null);

     frame1.getContentPane().add(panel1);

     variabl2603 = parameter[4];

     variabl2049 = parameter[20];

     variabl1079 = parameter[15];

     variabl2663 = new String[37];

     if (variabl1079.length() < 1) {

        final JFileChooser fc = new JFileChooser(".\\");

        fc.showOpenDialog(null);

        if (fc.getSelectedFile() != null) {

           variabl1079 = fc.getSelectedFile().toString();

        }

     }

     File file1 = new File(variabl1079);

     fa10 = variabl1079;

     try {

        if ((variabl1079.length() > 0) && (file1.exists())) {

           FileReader fr = new FileReader(file1);

           BufferedReader br = new BufferedReader(fr);

           String tempStr = null;

           int y = 0, b = 0;

           for (int i = 0; (tempStr = br.readLine()) != null; i++) {

             variabl2663[i] = tempStr;

             y = i;

           }

           if (y >= 16) {

             if (y >= 0)

                faa0 = variabl2663[0];

             if (y >= 1)

                faa1 = variabl2663[1];

             if (y >= 2)

                faa2 = variabl2663[2];

             if (y >= 3)

                fa0 = variabl2663[3];

             if (y >= 4)

                fa1 = variabl2663[4];

             if (y >= 5)

                fa2 = variabl2663[5];

             if (y >= 6)

                fa3 = variabl2663[6];

             if (y >= 7)

                fab0 = variabl2663[7];

              if (y >= 8)

                fab1 = variabl2663[8];

             if (y >= 9)

                fab2 = variabl2663[9];

             if (y >= 10)

                fac0 = variabl2663[10];

             if (y >= 11)

                fac1 = variabl2663[11];

             if (y >= 12)

                fac2 = variabl2663[12];

             if (y >= 13)

                fac3 = variabl2663[13];

             if (y >= 14)

                fac4 = variabl2663[14];

             if (y >= 15)

                fac5 = variabl2663[15];

             if (y >= 16)

                fac6 = variabl2663[16];

           }

        }

     } catch (IOException e2) {

        JOptionPane.showMessageDialog(null, "打开文件错!");

     }

     variabl1633 = fa0;

     variabl2217 = variabl2603;

     数据表查询结构(variabl2603);

     数据表查询数据(variabl2603);

 

     final JButton inButton = new JButton("预览图表");

     inButton.setBounds(340, 520, 170, 20);

     inButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           String sc0 = fac0 + "," + fac1 + "," + fac2;

           if ((fac6 == null) || (fac6.length() == 0))

             fac6 = "0";

           if (Integer.parseInt(fac6) == 0) {

             sc0 = "select " + sc0 + " from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6) == 1) {

             sc0 = "select " + fac0 + " ' ' as x " + "sum(" + fac2

                   + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6) == 2) {

             sc0 = "select " + fac0 + " ' ' as x " + "avg(" + fac2

                   + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else {

             JOptionPane.showMessageDialog(null, "统计类型输入错误!");

             return;

           }

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             rs = sta.executeQuery(sc0);

             rs.last();

             rsmd = rs.getMetaData();

             variabl18531 = rs.getRow();

             variabl26511 = rsmd.getColumnCount();

             variabl21971 = new String[variabl18531][variabl26511];

             for (int i = 0; i < variabl18531; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < variabl26511; j++) {

                   variabl21971[i][j] = rs.getString(j + 1);

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据出错!" + sc0);

           }

           main1.n = 0;

           statisticalChart3.means(fa10, variabl2197, variabl21971, fac2

                + "", fac0);

        }

     });

     panel1.add(inButton);

     final JButton inButton1 = new JButton("打印图表");

     inButton1.setBounds(530, 520, 170, 20);

     inButton1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           String sc0 = fac0 + "," + fac1 + "," + fac2;

           if (Integer.parseInt(fac6) == 0) {

             sc0 = "select " + sc0 + " from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6) == 1) {

             sc0 = "select " + fac0 + " ' ' as x " + "sum(" + fac2

                   + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else if (Integer.parseInt(fac6) == 2) {

             sc0 = "select " + fac0 + " ' ' as x " + "avg(" + fac2

                   + ") from " + variabl2217;

             if (variabl2217.indexOf(",") > 0)

                sc0 = sc0 + variabl2049;

           } else {

             JOptionPane.showMessageDialog(null, "统计类型输入错误!");

             return;

           }

           try {

             con = main1.getConn();

             sta = con.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             rs = sta.executeQuery(sc0);

             rs.last();

             rsmd = rs.getMetaData();

             variabl18531 = rs.getRow();

             variabl26511 = rsmd.getColumnCount();

             variabl21971 = new String[variabl18531][variabl26511];

             for (int i = 0; i < variabl18531; i++) {

                rs.absolute(i + 1);

                for (int j = 0; j < variabl26511; j++) {

                   variabl21971[i][j] = rs.getString(j + 1);

                }

             }

             rs.close();

             sta.close();

             con.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "获取数据出错!" + sc0);

           }

           main1.n = 1;

           statisticalChart3.means(fa10, variabl2197, variabl21971, fac2

                + "", fac0);

        }

     });

     panel1.add(inButton1);

 

     final JButton exitButton = new JButton("退出");

     exitButton.setBounds(720, 520, 170, 20);

     exitButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

           panel1.setVisible(false);

           frame1.setVisible(false);

           frame1.dispose();

        }

     });

     panel1.add(exitButton);

     frame1.setVisible(true);

   }

 

   static void color1(int c, Graphics g1) {

     switch (c) {

     case 1:

        g1.setColor(Color.red);

        break;

     case 2:

        g1.setColor(Color.blue);

        break;

     case 3:

        g1.setColor(Color.orange);

        break;

     case 4:

        g1.setColor(Color.green);

        break;

     case 5:

        g1.setColor(Color.black);

        break;

     case 6:

        g1.setColor(Color.cyan);

        break;

     case 7:

        g1.setColor(Color.yellow);

        break;

     case 8:

        g1.setColor(Color.lightGray);

        break;

     case 9:

        g1.setColor(Color.red);

        break;

     case 10:

        g1.setColor(Color.blue);

        break;

     case 11:

        g1.setColor(Color.orange);

        break;

     case 12:

        g1.setColor(Color.green);

        break;

     case 13:

        g1.setColor(Color.black);

        break;

     case 14:

        g1.setColor(Color.cyan);

        break;

     case 15:

        g1.setColor(Color.yellow);

        break;

     case 16:

        g1.setColor(Color.lightGray);

        break;

     case 17:

        g1.setColor(Color.white);

        break;

     }

   }

 

   static void 数据表查询结构(String variabl2217) {

     String[] s10 = null;

     String[] sk = new String[300];

     int n1 = 0, n2 = 0;

     variabl2049 = "";

     main1.driver1();

     if (variabl2217.indexOf(",") < 0) {

        try {

           cont4 = main1.getConn();

           stat4 = cont4.createStatement(

                ResultSet.TYPE_SCROLL_INSENSITIVE,

                ResultSet.CONCUR_UPDATABLE);

           String s1 = "select * from " + variabl2217;

           rs = stat4.executeQuery(s1);

           rsmdt4 = rs.getMetaData();

           variabl2651 = rsmdt4.getColumnCount();

           variabl2517 = new String[variabl2651];

           variabl1501 = new String[variabl2651];

           variabl1489 = new int[variabl2651];

           for (int i = 0; i < variabl2651; i++) {

             variabl2517[i] = rsmdt4.getColumnName(i + 1);

          }

           rs.close();

           stat4.close();

           cont4.close();

        } catch (SQLException e1) {

           JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

        }

     } else {

        s10 = variabl2217.split(",");

        String s0 = "", s1 = "", s2 = "";

        int k = 0;

        for (int i = 0; i < s10.length; i++) {

           try {

             cont4 = main1.getConn();

             stat4 = cont4.createStatement(

                   ResultSet.TYPE_SCROLL_INSENSITIVE,

                   ResultSet.CONCUR_UPDATABLE);

             s1 = "select * from " + s10[i];

             rs = stat4.executeQuery(s1);

              rsmdt4 = rs.getMetaData();

             variabl2651 = rsmdt4.getColumnCount();

             for (int j = 0; j < variabl2651; j++) {

                s2 = rsmdt4.getColumnName(j + 1).toLowerCase();

                n2 = n1;

                if (i > 0) {

                   k = 0;

                   for (int j1 = 0; j1 < n2; j1++) {

                      if (sk[j1].equals(s2)) {

                        sk[j1] = s10[i - 1] + "." + sk[j1];

                        if (variabl2049.length() == 0)

                           variabl2049 = " where " + s10[i - 1]

                                + "." + s2 + " = " + s10[i]

                                + "." + s2;

                        else

                           variabl2049 = variabl2049 + " and "

                                + s10[i - 1] + "." + s2 + " = "

                                + s10[i] + "." + s2;

                        sk[n1] = s10[i] + "." + s2 + " as "

                              + (s2 + i);

                        n1++;

                        k = 1;

                        break;

                      }

                   }

                  if (k == 0) {

                      sk[n1] = s2;

                      n1++;

                   }

                } else {

                   sk[n1] = s2;

                   n1++;

                }

             }

             rs.close();

             stat4.close();

             cont4.close();

           } catch (SQLException e1) {

             JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

           }

        }

     }

   }

 

   static void 数据表查询数据(String variabl2217) {

     int c = 0;

     int b = 0;

     try {

        cont4 = main1.getConn();

        stat4 = cont4.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

             ResultSet.CONCUR_UPDATABLE);

        s1 = "select " + variabl1633 + "  from " + variabl2217

             + variabl2049;

        rs = stat4.executeQuery(s1);

        rsmdt4 = rs.getMetaData();

        variabl2651 = rsmdt4.getColumnCount();

        variabl2517 = new String[variabl2651];

        variabl1501 = new String[variabl2651];

        variabl1489 = new int[variabl2651];

        for (int i1 = 0; i1 < variabl2651; i1++) {

           variabl2517[i1] = rsmdt4.getColumnName(i1 + 1);

           variabl1501[i1] = rsmdt4.getColumnTypeName(i1 + 1);

           variabl1489[i1] = rsmdt4.getColumnDisplaySize(i1 + 1);

        }

        rs.last();

        variabl1853 = rs.getRow();

        variabl2197 = new String[variabl1853][variabl2651];

        while (c < variabl1853) {

           rs.absolute(c + 1);

           while (b < variabl2651) {

             variabl2197[c][b] = rs.getString(b + 1);

             if ((main1.variabl1545.lastIndexOf("," + variabl1501[b]

                   + ",")) > 0)

                if ((variabl2197[c][b] == null)

                     || (variabl2197[c][b].trim().length() == 0))

                   variabl2197[c][b] = "0";

             b++;

           }

           c++;

           b = 0;

        }

        rs.close();

        stat4.close();

        cont4.close();

     } catch (SQLException e1) {

        JOptionPane.showMessageDialog(null, "读取数据库数据出错!");

     }

   }

}

103.源码103,公共程序,支持dataPrint5程序打印或打印预览带直方图统计图与数据表格的数据交叉表报表。

/**

 * 程序文件名:statisticalChart3.java

 * 作者:程学先 

 * 用途:公共程序,支持dataPrint5程序打印带数据交叉表的统计图与报表表格。

 * 本程序使用到了jcommon-1.0.16.jarjfreechart-1.0.13.jar包,

 * 需要到官方网站自行下载,并放到P1文件夹下COM文件夹中。

 */

import java.awt.BasicStroke;

import java.awt.BorderLayout;

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.sql.ResultSet;

import javax.imageio.ImageIO;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JFrame;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.BarRenderer;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.HorizontalAlignment;

 

public class statisticalChart3 extends JFrame implements Printable {

   private static statisticalChart3 frame;

   private PageFormat pf;

   private static PreviewCanvas3 canvas;

   private boolean isPreview = false;

   private boolean previewFlag = false;

   static String variabl2603;

   private static String s1 = "";

   private static int variabl1853, variabl18531;

   private static int variabl2113, variabl21131;

   private static String[][] variabl2197;

   private static String[][] variabl21971;

   private static String path;

   private static int variabl2483 = 0;

   private static String variabl2447;

   private static FileReader fr;

   private static String[] variabl2663;

   private static Graphics2D g2;

   private static Graphics g;

   private static String 数据字段名;

   private static String 分组字段名;

 

   public static void gainReport() {

     variabl2663 = new String[17];

     if (variabl2447.length() < 1) {

        JOptionPane.showMessageDialog(null, "缺文件名!");

        return;

     }

     File file1 = new File(variabl2447);

     try {

        if (file1.exists())

           fr = new FileReader(file1);

        BufferedReader br = new BufferedReader(fr);

        String tempStr = null;

        for (int i = 0; (tempStr = br.readLine()) != null; i++) {

           variabl2663[i] = tempStr;

        }

     } catch (IOException e2) {

        JOptionPane.showMessageDialog(null, "打开文件错!");

     }

   }

 

   static void means(String variabl24470, String[][] variabl21972,

        String[][] variabl21973, String ss1, String ss2) {

     variabl2483 = main1.variabl2483;

     s1 = "";

     variabl2663 = new String[17];

     variabl2447 = variabl24470;

     数据字段名 = ss1;

     分组字段名 = ss2;

     gainReport();

     variabl1853 = variabl21972.length;

     variabl2113 = variabl21972[0].length;

     variabl2197 = new String[variabl1853][variabl2113];

     for (int i = 0; i < variabl1853; i++)

        for (int j = 0; j < variabl2113; j++)

           variabl2197[i][j] = variabl21972[i][j];

     variabl18531 = variabl21973.length;

     variabl21131 = variabl21973[0].length;

     variabl21971 = new String[variabl18531][variabl21131];

     for (int i = 0; i < variabl18531; i++)

        for (int j = 0; j < variabl21131; j++)

           variabl21971[i][j] = variabl21973[i][j];

     frame = new statisticalChart3();

     frame.setVisible(true);

   }

 

   public statisticalChart3() {

     super();

     JFreeChart chart = createChart();

     path = "a1.jpg";

     int we1 = variabl2483;

     int we2 = Integer.parseInt(variabl2663[15]);

     try {

        ChartUtilities.writeChartAsJPEG(new FileOutputStream(path), chart,

             we1, we2);

     } catch (FileNotFoundException e1) {

        e1.printStackTrace();

     } catch (IOException e1) {

        e1.printStackTrace();

     }

     setTitle(variabl2663[0]);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     pf = new PageFormat();

     canvas = new PreviewCanvas3();

     int we3 = Integer.parseInt(variabl2663[6]) + 2;

     int variabl2483 = we1;

     int variabl2503 = we2 + variabl1853 * we3

           + Integer.parseInt(variabl2663[2])

           + Integer.parseInt(variabl2663[9]);

     this.setSize(new Dimension(variabl2483, variabl2503));

     BorderLayout borderLayout = new BorderLayout();

     borderLayout.setHgap(10);

     setLayout(borderLayout);

     canvas.setSize(new Dimension(variabl2483, variabl2503));

     add(canvas, BorderLayout.CENTER);

     final JPanel panel = new JPanel();

     panel.setOpaque(false);

     getContentPane().add(panel, BorderLayout.SOUTH);

     if (main1.n == 1) {

        PrinterJob job = PrinterJob.getPrinterJob();

        if (!job.printDialog()) {

           return;

        }

        job.setPrintable(new Printable() {

           public int print(Graphics graphics, PageFormat pageFormat,

                int pageIndex) throws PrinterException {

             isPreview = true;

             if (pageIndex < 1) {

                statisticalChart3.g = graphics;

                g2 = (Graphics2D) g;

                g2.setStroke(new BasicStroke(1f));

                printPicture(graphics, pageFormat, pageIndex);

                return Printable.PAGE_EXISTS;

             } else {

                return Printable.NO_SUCH_PAGE;

             }

           }

        });

        job.setJobName("打印报表");

        try {

           job.print();

        } catch (PrinterException e1) {

           e1.printStackTrace();

        }

     }

   }

 

   private static JFreeChart createChart() {

     DefaultCategoryDataset dataset = new DefaultCategoryDataset();

     for (int i = 0; i < variabl18531; i++) {

        if ((variabl21971[i][2] == null)

             || (variabl21971[i][2].length() == 0))

           variabl21971[i][2] = 0 + "";

        int value = (int) Double.parseDouble(variabl21971[i][2]);

        String sf1 = variabl21971[i][1];

        String sf0 = variabl21971[i][0];

        dataset.addValue(value, sf1, sf0);

     }

     JFreeChart chart = ChartFactory.createBarChart("\n\n" + variabl2663[7],

           分组字段名, 数据字段名, dataset, PlotOrientation.VERTICAL, true, true,

           false);

      chart.setBackgroundPaint(Color.white);

     CategoryPlot plot = (CategoryPlot) chart.getPlot();

     NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

     rangeAxis.setTickLabelFont(new Font("Sans-serif", Font.PLAIN, 12));

     rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

      rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

     BarRenderer renderer = (BarRenderer) plot.getRenderer();

     GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,

           0.0f, new Color(0, 0, 64));

     GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,

           0.0f, new Color(0, 64, 0));

     renderer.setSeriesPaint(0, gp0);

     renderer.setSeriesPaint(1, gp1);

     CategoryAxis domainAxis = plot.getDomainAxis();

     domainAxis.setTickLabelFont(new Font("Sans-serif", Font.PLAIN, 20));

     domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 20));

      domainAxis.setCategoryLabelPositions(CategoryLabelPositions

           .createUpRotationLabelPositions(Math.PI / 6.0));

     TextTitle textTitle = chart.getTitle();

     textTitle.setFont(new Font(variabl2663[8], Font.PLAIN, Integer

           .parseInt(variabl2663[9])));

     chart.getLegend().setItemFont(

           new Font(variabl2663[8], Font.PLAIN, Integer

                .parseInt(variabl2663[9])));

      chart.getLegend().setHorizontalAlignment(HorizontalAlignment.CENTER);

     return chart;

   }

 

  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)

        throws PrinterException {

     printPicture(graphics, pageFormat, pageIndex);

     return Printable.PAGE_EXISTS;

   }

 

   class PreviewCanvas3 extends Canvas {

     public void paint(Graphics g) {

        try {

           super.paint(g);

           g2 = (Graphics2D) g;

           g2.translate(10, 10);

           int x = (int) (pf.getImageableX() - 1);

           int y = (int) (pf.getImageableY() - 1);

           int width = (int) (pf.getImageableWidth() + 1);

           int height = (int) (pf.getImageableHeight() + 1);

           int mw = (int) pf.getWidth();

           int mh = (int) pf.getHeight();

           g2.drawRect(0, 0, mw, mh);

           g2.setColor(new Color(255, 253, 234));

           g2.fillRect(1, 1, mw - 1 + 300, mh - 1);

           g2.setStroke(new BasicStroke(1f, BasicStroke.CAP_ROUND,

               BasicStroke.JOIN_ROUND, 10f, new float[] { 5, 5 }, 0f));

           g2.setColor(Color.BLACK);

           g2.setColor(Color.WHITE);

           g2.fillRect(x + 1, y + 1, width - 1 + 300, height - 1);

           statisticalChart3.this.print(g, pf, 0);

        } catch (PrinterException e) {

           e.printStackTrace();

        }

     }

   }

 

   public void printPicture(Graphics graphics, PageFormat pageFormat,

        int pageIndex) {

     int x = (int) pageFormat.getImageableX() - 20;

     int y = (int) pageFormat.getImageableY() - 50;

     int w = (int) pageFormat.getImageableWidth();

     int w2 = (int) (Integer.parseInt(variabl2663[6]) * 1.5);

     g2 = (Graphics2D) graphics;

     g2.setColor(Color.BLUE);

     g2.setFont(new Font(variabl2663[1], Font.BOLD, Integer

           .parseInt(variabl2663[2])));

     g2.drawString(variabl2663[0], x + 60, y + 30);

     g2.setColor(Color.BLACK);

     g2.setFont(new Font(variabl2663[5], Font.PLAIN, Integer

           .parseInt(variabl2663[6])));

     g2.setStroke(new BasicStroke(1f));

     String[] we5 = variabl2663[3].split(",");

     String[] we6 = variabl2663[4].split(",");

     int[] we7 = new int[we6.length];

     int we8 = Integer.parseInt(variabl2663[6]);

     for (int i = 0; i < we6.length; i++)

        we7[i] = Integer.parseInt(we6[i]);

     try {

        y = y + 30 + Integer.parseInt(variabl2663[2]);

        int y1 = y;

        int xe1 = x;

        g2.drawLine(xe1, y1 - 20, xe1, y + (variabl1853 - 1) * w2);

        for (int i = 0; i < we6.length; i++) {

           g2.drawLine(xe1, y - 20, xe1 + we7[i], y - 20);

           g2.drawString(we5[i], xe1 + 5, y - 5);

           g2.drawLine(xe1 + we7[i], y1 - 20, xe1 + we7[i], y + 10);

           xe1 = xe1 + we7[i];

        }

        for (int i = 0; i < variabl1853; i++) {

           y1 = y;

           y = y + (int) (Integer.parseInt(variabl2663[6]) * 1.5);

           xe1 = x;

           g2.drawLine(xe1, y1, xe1, y);

           for (int j = 0; j < we6.length; j++) {

             g2.drawLine(xe1, y1, xe1 + we7[j], y1);

             g2.drawString(variabl2197[i][j], xe1 + 5, y - 5);

             g2.drawLine(xe1 + we7[j], y1, xe1 + we7[j], y);

             if (i == variabl1853 - 1)

                g2.drawLine(xe1, y, xe1 + we7[j], y);

             xe1 = xe1 + we7[j];

           }

        }

        File file = new File("a1.jpg");

        BufferedImage image1 = ImageIO.read(file);

        g2.drawImage(image1, 10, y + 20, 820, 300, this);

     } catch (Exception ex) {

        ex.printStackTrace();

     }

     isPreview = true;

     previewFlag = true;

   }

}




https://blog.sciencenet.cn/blog-2551-1123241.html

上一篇:管理信息系统软件生产线源码98-100
下一篇:管理信息系统软件生产线源码104-106
收藏 IP: 183.94.47.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-3 05:01

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部