Java tutorial
/* * Copyright 2014 dxp.alibaba-inc.com All right reserved. This software is the * confidential and proprietary information of dxp.alibaba-inc.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with dxp.alibaba-inc.com. */ package com.smart.aqimonitor.client; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputMethodEvent; import java.awt.event.InputMethodListener; import java.io.File; import java.io.IOException; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.border.TitledBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.StyledDocument; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.smart.aqimonitor.job.Pm25InDetailJob; import com.smart.aqimonitor.swing.AqiTextPane; import com.smart.aqimonitor.util.FileUtil; /** * AqiMonitor.java * * @author ShenDayong 2014-1-3 4:26:56 */ @SuppressWarnings("serial") public class AqiMonitor extends JFrame { private JPanel contentPane; private AqiTextPane textPane; private StyledDocument document; private Pm25InDetailJob pm25InDetailJob; private AqiMonitor refSelf; private boolean isRetrieving = false; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { AqiMonitor frame = new AqiMonitor(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } private void init() { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml", "job.spring.xml" }); BeanFactory factory = (BeanFactory) context; pm25InDetailJob = (Pm25InDetailJob) factory.getBean("pm25InDetailJob"); pm25InDetailJob.setDocument(document); Thread firstRun = new Thread(new Runnable() { @Override public void run() { pm25InDetailJob.refresh(); } }); firstRun.start(); } /** * Create the frame. */ public AqiMonitor() { refSelf = this; setPreferredSize(new Dimension(640, 480)); setTitle("\u7A7A\u6C14\u8D28\u91CF\u76D1\u6D4B"); setIconImage(Toolkit.getDefaultToolkit() .getImage(AqiMonitor.class.getResource("/lombok/installer/eclipse/STS.png"))); setMinimumSize(new Dimension(640, 480)); setMaximumSize(new Dimension(1024, 768)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 636, 412); contentPane = new JPanel(); contentPane.setPreferredSize(new Dimension(640, 480)); contentPane.setMinimumSize(new Dimension(640, 480)); contentPane.setMaximumSize(new Dimension(1024, 768)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel mainPanel = new JPanel(); contentPane.add(mainPanel, BorderLayout.CENTER); mainPanel.setLayout(new BorderLayout(0, 0)); JPanel contentPanel = new JPanel(); mainPanel.add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(new BorderLayout(0, 0)); JScrollPane scrollPane = new JScrollPane(); scrollPane .setViewportBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null)); contentPanel.add(scrollPane, BorderLayout.CENTER); textPane = new AqiTextPane(); textPane.addInputMethodListener(new InputMethodListener() { public void caretPositionChanged(InputMethodEvent event) { } public void inputMethodTextChanged(InputMethodEvent event) { textPane.setCaretPosition(document.getLength() + 1); } }); textPane.setEditable(false); textPane.setOpaque(false); textPane.setForeground(Color.BLACK); scrollPane.setViewportView(textPane); document = textPane.getStyledDocument(); document.addDocumentListener(new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { changedUpdate(e); } @Override public void insertUpdate(DocumentEvent e) { changedUpdate(e); } @Override public void changedUpdate(DocumentEvent e) { if (e.getDocument() == document) { textPane.setCaretPosition(document.getLength()); } } }); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel, BorderLayout.SOUTH); buttonPanel.setLayout(new BorderLayout(0, 0)); JLabel lblTipsLabel = new JLabel( "Tips\uFF1A\u6587\u4EF6\u4FDD\u5B58\u683C\u5F0Fcsv\u53EF\u7528Excel\u6253\u5F00"); lblTipsLabel.setForeground(Color.BLUE); buttonPanel.add(lblTipsLabel, BorderLayout.WEST); JPanel panel = new JPanel(); buttonPanel.add(panel, BorderLayout.CENTER); panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); JButton btnRetrieve = new JButton("\u624B\u52A8\u83B7\u53D6\u6570\u636E"); panel.add(btnRetrieve); btnRetrieve.setVerticalAlignment(SwingConstants.BOTTOM); JButton btnNewButton = new JButton("\u5173\u4E8E"); btnNewButton.setToolTipText("\u5173\u4E8E"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextArea textArea = new JTextArea( "\n csv\n\n smartstudio@foxmail.com"); textArea.setColumns(35); textArea.setRows(6); textArea.setLineWrap(true);// textArea.setEditable(false);// textArea.setOpaque(false); JOptionPane.showMessageDialog(contentPane, textArea, "", JOptionPane.PLAIN_MESSAGE); } }); JButton btnSetting = new JButton("\u8BBE\u7F6E"); btnSetting.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Point parentPos = refSelf.getLocation(); AqiSettingDialog settingDialog = new AqiSettingDialog(refSelf, pm25InDetailJob.getAqiParser()); settingDialog.setModal(true); settingDialog.setLocation(parentPos.x + 100, parentPos.y + 150); settingDialog.init(); settingDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingDialog.setVisible(true); } }); JButton btnExportDir = new JButton("\u67E5\u770B\u6570\u636E"); btnExportDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String[] cmd = new String[5]; String filePath = pm25InDetailJob.getAqiParser().getFilePath(); File file = new File(filePath); if (!file.exists()) { FileUtil.makeDir(file); } if (!file.isDirectory()) { JOptionPane.showMessageDialog(contentPane, "", "", JOptionPane.ERROR_MESSAGE); return; } cmd[0] = "cmd"; cmd[1] = "/c"; cmd[2] = "start"; cmd[3] = " "; cmd[4] = pm25InDetailJob.getAqiParser().getFilePath(); Runtime.getRuntime().exec(cmd); } catch (IOException ex) { ex.printStackTrace(); } } }); panel.add(btnExportDir); panel.add(btnSetting); panel.add(btnNewButton); btnRetrieve.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!isRetrieving) { isRetrieving = true; Thread firstRun = new Thread(new Runnable() { @Override public void run() { pm25InDetailJob.refresh(); isRetrieving = false; } }); firstRun.start(); } } }); init(); } /** * @return the document */ public StyledDocument getDocument() { return document; } }