⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stockcontrol.java

📁 股票信息采集源码
💻 JAVA
字号:
/**
 * 显示界面,用于程序入口
 */
package cn.com.lucre.stock;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import cn.com.lucre.properties.PropertySet;

import com.swtdesigner.SwingResourceManager;
import com.lucre.EncryptUtil;

/**
 * @author ABE
 *
 */
public class StockControl extends javax.swing.JFrame implements  WindowListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private PropertySet property;
    private StockGather gather;
    
	/**
	 * @param args
	 */
	public StockControl() {
		property=new PropertySet();
		property.loadProper();
		initComponents();
		gather=new StockGather(codeList);
	}

	private void initComponents() {//GEN-BEGIN:initComponents
		getContentPane().setLayout(new BorderLayout());
		this.setResizable(false);
		this.addWindowListener(this);
		setTitle("Lookbao股票雷达");
		
		 mainbar = new JToolBar();
		 mainbar.setRollover(true);
		 mainbar.setBorderPainted(false);
		 mainbar.setFloatable(false);
		
		 startbutton = new JButton("启动");
		 startbutton.setFont(new Font("新宋体", Font.PLAIN, 12));
		 mainbar.add(startbutton);
		 startbutton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					property.loadProper();
					startbutton.setVisible(false);
					stopbutton.setVisible(true);
					settingbutton.setEnabled(false);
					
					gather.tostart();
				}
		 });
		 
		 stopbutton = new JButton("停止");
		 stopbutton.setFont(new Font("新宋体", Font.PLAIN, 12));
		 stopbutton.setVisible(false);
		 mainbar.add(stopbutton);
		 stopbutton.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 
				gather.tostop();	 
				stopbutton.setVisible(false);
				startbutton.setVisible(true);
				settingbutton.setEnabled(true);
			
			 }

		 });
		 
		 settingbutton=new JButton("设置");
		 settingbutton.setFont(new Font("新宋体", Font.PLAIN, 12));
//		 settingbutton.setIcon(SwingResourceManager.getIcon(StockControl.class,"/resources/images/bbs.gif"));
		 mainbar.add(settingbutton);
		 settingbutton.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 SetConfigure dlg = new SetConfigure();
					dlg.setSize(325, 200);
					dlg.setResizable(false);
					Dimension dlgSize = dlg.getPreferredSize();
					Dimension frmSize = getSize();
					Point loc = getLocation();
					dlg.setLocation((frmSize.width - dlgSize.width) / 4 + loc.x,
							(frmSize.height - dlgSize.height) / 4 + loc.y);

					dlg.setVisible(true);

			 }
		 });
		 
		 helpbutton=new JButton("帮助");
		 helpbutton.setFont(new Font("新宋体", Font.PLAIN, 12));
		 mainbar.add(helpbutton);
		 helpbutton.setVisible(false);
		 
		 getContentPane().add(mainbar, BorderLayout.NORTH);
		 
		 scrollPane = new JScrollPane();
		 viewPanel = new JPanel();
		 viewPanel.setLayout(new BorderLayout());
		 
		 codeList = new JTextArea(" ");
		 codeList.setEditable(false);
		 codeList.setForeground(Color.GREEN);
		 codeList.setFont(new Font("Dialog", Font.BOLD, 12));
		 codeList.setBackground(new Color(0, 0, 0));
		 
		 viewPanel.add(codeList, BorderLayout.CENTER);
		 scrollPane.setViewportView(viewPanel);
		 getContentPane().add(scrollPane, BorderLayout.CENTER);
		 
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(new Dimension(520, 360));
		setLocation((screenSize.width - 640) / 2, (screenSize.height - 480) / 2);
	}

	private JScrollPane scrollPane;

	private JPanel viewPanel;

	private JTextArea codeList;

	private JToolBar mainbar;

	private JButton startbutton;

	private JButton stopbutton;
	
	private JButton settingbutton;
	
	private JButton helpbutton;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	if((new EncryptUtil()).validate(0)){
		StockControl control=new StockControl();
		control.setVisible(true);
		}

	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	class SetConfigure extends JDialog {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		JPanel panel1 = new JPanel();

		BorderLayout borderLayout1 = new BorderLayout();

		JPanel jPanel1 = new JPanel();

		JButton jButton1 = new JButton();

		JLabel jLabel1 = new JLabel();

		ImageIcon image1 = new ImageIcon();

		JTextField jTextField1 = new JTextField();

		JLabel jLabel2 = new JLabel();

		JTextField jTextField2 = new JTextField();

		JLabel jLabel3 = new JLabel();

		JPasswordField jTextField3 = new JPasswordField();

		JButton jButton2 = new JButton();

		JButton jButton3 = new JButton();

		public SetConfigure(StockControl owner, String title, boolean modal) {
			super(owner, title, modal);
			try {
				setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				setSize(350,200);
				jbInit();
				getDataInfo();		
				pack();
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		}
		private boolean setDataInfo() {
			if (jTextField1.getText().equals("")) {
				JOptionPane.showMessageDialog(SetConfigure.this, "数据库地址不能为空!",
						"数据库地址不能为空", JOptionPane.ERROR_MESSAGE);
				return false;
			}
	        if (jTextField2.getText().equals("")) {
				JOptionPane.showMessageDialog(SetConfigure.this, "数据库用户名不能为空!",
						"用户名为填写", JOptionPane.ERROR_MESSAGE);
				return false;
			}

	        PropertySet.properties.setProperty("host", jTextField1.getText());
	        PropertySet.properties.setProperty("user", jTextField2.getText());
	        PropertySet.properties.setProperty("password", jTextField3.getText());
	        (new PropertySet()).saveProper();
			return true;

		}
		/**
		 * 取得数据库连接信息
		 */
		
		private void getDataInfo()
		{
			
	      
			jTextField1.setText(PropertySet.properties.getProperty("host"));		
			jTextField2.setText(PropertySet.properties.getProperty("user"));
			jTextField3.setText(PropertySet.properties.getProperty("password"));		
		}

		/**
		 * 连接数据库测试*
		 */
	@SuppressWarnings("deprecation")
	private void Test() {
	        
			String serverName=jTextField1.getText();//proper.getProperty("ServerName");
			String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
			String user =jTextField2.getText(); //proper.getProperty("UserName");
			String pwd = jTextField3.getText();//proper.getProperty("PassWord");
			@SuppressWarnings("unused") String database = "mynewssearch";
			String url = "jdbc:microsoft:sqlserver://"+serverName+":1433;DatabaseName="+PropertySet.properties.getProperty("db_name");
			Connection dBConnection=null;
	        try {
				Class.forName(driverName);
				dBConnection = DriverManager.getConnection(url, user, pwd);
				if (dBConnection != null) {
					JOptionPane.showMessageDialog(this,"数据库连接测试成功!","连接数据库测试",JOptionPane.INFORMATION_MESSAGE);
					System.out.println("数据库连接成功!");
				}
			
			} catch (ClassNotFoundException ex) {
				JOptionPane.showMessageDialog(this,"数据库连接测试失败!\n请检查连接后重试!","连接数据库测试",JOptionPane.INFORMATION_MESSAGE);
			} catch (SQLException ex) {
				JOptionPane.showMessageDialog(this,"数据库连接测试失败!\n请检查连接后重试!","连接数据库测试",JOptionPane.INFORMATION_MESSAGE);
			}finally{
				try{
				dBConnection.close();
				}catch(Exception exp){
					System.err.println(exp);
				}
			}
		}

		public SetConfigure() {
			this(new StockControl(), "股票雷达配置", true);
		}

		private void jbInit() throws Exception {
			panel1.setLayout(borderLayout1);
			jPanel1.setLayout(null);
			jLabel1.setFont(new java.awt.Font("股票雷达配置", Font.PLAIN, 12));
			jLabel1.setText("数据库名称或IP:");
			jLabel1.setBounds(new Rectangle(3, 6, 105, 30));
			jTextField1.setBounds(new Rectangle(103, 7, 193, 23));
			panel1.setFont(new java.awt.Font("DialogInput", Font.PLAIN, 12));
			jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
			jLabel2.setText("用户名:");
			jLabel2.setBounds(new Rectangle(47, 38, 49, 26));
			jTextField2.setText("");
			jTextField2.setBounds(new Rectangle(103, 39, 192, 25));
			jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
			jLabel3.setText("密码:");
			jLabel3.setBounds(new Rectangle(51, 73, 41, 17));
			jTextField3.setText("");
			jTextField3.setBounds(new Rectangle(100, 69, 196, 24));
			jButton1.setBounds(new Rectangle(126, 118, 74, 22));
			jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
			jButton1.setText("保存");
			jButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					if (setDataInfo()){
						JOptionPane.showMessageDialog(SetConfigure.this, "数据库信息以保存。",
								"保存成功", JOptionPane.CLOSED_OPTION);
						setVisible(false);
					}
					
				}
			});
			jButton2.setBounds(new Rectangle(229, 118, 74, 22));
			jButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
			jButton2.setText("取消");
			jButton3.setBounds(new Rectangle(25, 118, 74, 22));
			jButton3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
			jButton3.setText("测试");
			jButton2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					setVisible(false);
				}
			});
			jButton3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Test();
				}
			});
			getContentPane().add(panel1);
			panel1.add(jPanel1, java.awt.BorderLayout.CENTER);
			jPanel1.add(jLabel1);
			jPanel1.add(jTextField1);
			jPanel1.add(jLabel2);
			jPanel1.add(jTextField2);
			jPanel1.add(jLabel3);
			jPanel1.add(jTextField3);
			jPanel1.add(jButton1);
			jPanel1.add(jButton2);
			jPanel1.add(jButton3);
		}
	}

	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -