HTML5技术

Java Swing快速构建窗体应用程序 - JackWang-CUMT(3)

字号+ 作者:H5之家 来源:博客园 2015-11-22 19:07 我要评论( )

1 package com.mkmis.db; java.io.FileInputStream; 4 import java.io.IOException; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.Re

1 package com.mkmis.db; java.io.FileInputStream; 4 import java.io.IOException; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.util.Properties; DBConnection { Connection getConnection() { 17 Properties props = new Properties(); 18 FileInputStream fis = null; 19 Connection con = null; 20 try { 21 fis = new FileInputStream("db.properties"); 22 props.load(fis); 23 // 24 Class.forName(props.getProperty("DB_DRIVER_CLASS")); con = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USERNAME"), props.getProperty("DB_PASSWORD")); 27 } catch (IOException | SQLException | ClassNotFoundException e) { 28 e.printStackTrace(); 29 } 30 return con; 31 } closeResultSet(ResultSet rs) { 35 if (rs != null) { 36 try { 37 rs.close(); 38 rs = null; 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 } closeStatement(Statement stm) { 47 if (stm != null) { 48 try { 49 stm.close(); 50 stm = null; 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 } 55 } closePreparedStatement(PreparedStatement pstm) { 59 if (pstm != null) { 60 try { 61 pstm.close(); 62 pstm = null; 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 } 66 } 67 } closeConnection(Connection con) { 71 if (con != null) { 72 try { 73 con.close(); 74 con = null; 75 } catch (SQLException e) { 76 e.printStackTrace(); 77 } 78 con = null; 79 } 80 } 81 82 }

View Code

 6 图表窗体代码

* To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. com.mkmis.forms; java.awt.Color; 9 import java.awt.Dimension; 10 import java.awt.Font; org.jfree.chart.ChartFactory; 13 import org.jfree.chart.ChartPanel; 14 import org.jfree.chart.JFreeChart; 15 import org.jfree.chart.StandardChartTheme; 16 import org.jfree.chart.axis.CategoryAxis; 17 import org.jfree.chart.axis.NumberAxis; 18 import org.jfree.chart.block.BlockBorder; 19 import org.jfree.chart.plot.CategoryPlot; 20 import org.jfree.chart.renderer.category.BarRenderer; 21 import org.jfree.chart.title.TextTitle; 22 import org.jfree.data.category.CategoryDataset; 23 import org.jfree.data.category.DefaultCategoryDataset; 24 //import org.jfree.ui.ApplicationFrame; * wangming JIFrame1 extends javax.swing.JInternalFrame { * Creates new form JIFrame1 JIFrame1() { 36 initComponents(); ((javax.swing.plaf.basic.BasicInternalFrameUI)this.getUI()).setNorthPane(null); 39 this.setBackground(Color.white); 40 41 CategoryDataset dataset = createDataset(); 42 JFreeChart chart = createChart(dataset); 43 ChartPanel chartPanel = new ChartPanel(chart); 44 chartPanel.setFillZoomRectangle(true); 45 chartPanel.setMouseWheelEnabled(true); 46 chartPanel.setPreferredSize(new Dimension(500, 270)); 47 setContentPane(chartPanel); 48 } * This method is called from within the constructor to initialize the form. 52 * WARNING: Do NOT modify this code. The content of this method is always 53 * regenerated by the Form Editor. @SuppressWarnings("unchecked") initComponents() { 58 59 setBorder(null); 60 setClosable(true); 61 setMaximizable(true); 62 setResizable(true); 63 64 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 65 getContentPane().setLayout(layout); 66 layout.setHorizontalGroup( 67 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 68 .addGap(0, 410, Short.MAX_VALUE) 69 ); 70 layout.setVerticalGroup( 71 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 72 .addGap(0, 285, Short.MAX_VALUE) 73 ); 74 75 pack(); serialVersionUID = 1L; { 81 // set a theme using the new shadow generator feature available in ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", 84 true)); 85 } * Creates a new demo instance. 89 * title the frame title. JIFrame1(String title) { 93 super(title); 94 CategoryDataset dataset = createDataset(); 95 JFreeChart chart = createChart(dataset); 96 ChartPanel chartPanel = new ChartPanel(chart); 97 chartPanel.setFillZoomRectangle(true); 98 chartPanel.setMouseWheelEnabled(true); 99 chartPanel.setPreferredSize(new Dimension(500, 270)); 100 setContentPane(chartPanel); 101 } * Returns a sample dataset. 105 * The dataset. CategoryDataset createDataset() { 109 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 110 dataset.addValue(7445, "JFreeSVG", "Warm-up"); 111 dataset.addValue(24448, "Batik", "Warm-up"); 112 dataset.addValue(4297, "JFreeSVG", "Test"); 113 dataset.addValue(21022, "Batik", "Test"); 114 return dataset; 115 } * Creates a sample chart. 119 * dataset the dataset. 121 * The chart. JFreeChart createChart(CategoryDataset dataset) { Font font=JFreeChart chart = ChartFactory.createBarChart("性能: JFreeSVG 对比 Batik", , , dataset); chart.getTitle().setFont(font); 135 136 chart.addSubtitle(new TextTitle("在SVG中产生1000个图表")); 137 chart.setBackgroundPaint(Color.white); 138 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 139 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 140 rangeAxis.setLabelFont(font); 141 142 CategoryAxis domainAxis = plot.getDomainAxis(); 143 domainAxis.setLabelFont(font); 144 // ****************************************************************** 145 // More than 150 demo applications are included with the JFreeChart 146 // Developer Guide...for more information, see: rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 154 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 155 renderer.setDrawBarOutline(false); 156 chart.getLegend().setFrame(BlockBorder.NONE); 157 return chart; 158 } * Starting point for the demonstration application. 162 * args ignored. public static void main(String[] args) { 166 // BarChartDemo1 demo = new BarChartDemo1("JFreeChart: BarChartDemo1.java"); 167 // demo.pack(); 168 // RefineryUtilities.centerFrameOnScreen(demo); 169 // demo.setVisible(true); 170 // } Variables declaration - do not modify }

View Code

 运行此app,一定要引入需要的库,mysql jdbc驱动和jfreechart库

 
 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 如何快速处理线上故障 - 倒骑的驴

    如何快速处理线上故障 - 倒骑的驴

    2017-05-02 12:01

  • Java 8 Lambda 表达式 - Felix_ICanFixIt

    Java 8 Lambda 表达式 - Felix_ICanFixIt

    2017-04-22 17:04

  • C# 快速高效率复制对象另一种方式 表达式树 - Emrys5

    C# 快速高效率复制对象另一种方式 表达式树 - Emrys5

    2017-04-06 14:00

  • Omi v1.0.2发布 - 正式支持传递javascript表达式 - 【当耐特】

    Omi v1.0.2发布 - 正式支持传递javascript表达式 - 【当耐特】

    2017-03-22 11:03

网友点评