GUI图书馆访问者统计器

  1. 案例6.17——图书馆访问者统计器
    本案例中的程序将展示2004年至2016的某省图书馆访客的统计数据。当单击“显示统计数据”按钮时,程序将以表格和图形的形式显示这些数据。以下是2004-2016年图书馆参观人数统计,如图6-3-4所示。

图6-3-4 图书馆参观人数统计
以下是创建程序的步骤。
① 使用LibraryVisitor创建项目。
② 添加名为VisitorForm的JFrame Form。
③ 添加一个变量名为tablevisitor的表。该表有两栏,标题如下:年份和访问人数。
④ 添加一个变量名为panelChart的面板。这个面板将被用作显示访问者统计图表的地方。
⑤ 添加一个变量名为DisplayButton的按钮,文本内容为“显示统计数据”。
⑥ 添加两个标签作为Form窗口的标题,并修改文本内容为“图书馆访问者统计器”和“------------------------------------------”。
以下是程序第一次运行时的显示效果如图6-3-5所示。
在这里插入图片描述

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.libraryvisitor;

/**
 *
 * @author 1
 */
import java.awt.Font;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.plot.PlotOrientation;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.StandardChartTheme;
public class VisitorForm extends javax.swing.JFrame {

    /**
     * Creates new form VisitorForm
     */
    public VisitorForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablevisitor = new javax.swing.JTable();
        panelChart = new javax.swing.JPanel();
        DisplayButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tablevisitor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "年份", "访问人数"
            }
        ));
        jScrollPane1.setViewportView(tablevisitor);

        javax.swing.GroupLayout panelChartLayout = new javax.swing.GroupLayout(panelChart);
        panelChart.setLayout(panelChartLayout);
        panelChartLayout.setHorizontalGroup(
            panelChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelChartLayout.setVerticalGroup(
            panelChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        DisplayButton.setText("显示数据统计");
        DisplayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DisplayButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("图书馆访问统计器");

        jLabel2.setText("----------------------------------------------------------------------------------------------------------------------------------------");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(217, 217, 217)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(DisplayButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelChart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(DisplayButton))
                    .addComponent(panelChart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(10, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    ***private void DisplayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    Integer [][]visitordata={{2004,1500},{2005,1450},
        {2006,2000},{2007,2050},{2008,2500},{2009,2000},
        {2010,1600},{2011,3000},{2012,3200},{2013,4000},
        {2014,3500},{2015,2000},{2016,2300}};
    String[] title={"年份","访问人数"};
    
    //显示数据到表格中
    DefaultTableModel model=new DefaultTableModel();
    model.setDataVector(visitordata, title);
    tablevisitor.setModel(model);
    
    //创建访问人数的图表
    DefaultCategoryDataset chart_dataset=new DefaultCategoryDataset();
    for(int i=0;i<12;i++){
        chart_dataset.addValue(visitordata[i][1], "图书馆访问人员统计数据",visitordata[i][0]);
    }
    //创建主题样式,避免中文乱码
    StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        // 设置标题字体
    standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20));
        // 设置图例的字体
    standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
        // 设置轴向的字体
    standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));
        // 应用主题样式
    ChartFactory.setChartTheme(standardChartTheme);
    JFreeChart lineChart = ChartFactory.createLineChart("图书馆访问人员统计表图","nianfen","访问人数"
            ,chart_dataset,PlotOrientation.VERTICAL, true, true, false);
    ChartPanel chartPanel=new ChartPanel(lineChart);
    chartPanel.setSize(panelChart.getSize());
    panelChart.removeAll();
    panelChart.add(chartPanel);
    panelChart.validate();
    
    }***                                             

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VisitorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VisitorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VisitorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VisitorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VisitorForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton DisplayButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelChart;
    private javax.swing.JTable tablevisitor;
    // End of variables declaration                   
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值