marketjpanel.java

来自「JStock是一个免费股市软件」· Java 代码 · 共 171 行

JAVA
171
字号
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Copyright (C) 2008 Yan Cheng Cheok <yccheok@yahoo.com>
 */

package org.yccheok.jstock.gui;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import org.yccheok.jstock.engine.Country;
import org.yccheok.jstock.engine.Index;
import org.yccheok.jstock.engine.Market;

/**
 *
 * @author  yccheok
 */
public class MarketJPanel extends javax.swing.JPanel {

    /** Creates new form MarketJPanel */
    public MarketJPanel(Country country) {
        initComponents();
        
        this.country = country;        
        initAccordingToCountry(country);
    }

    /** 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        leftPanel = new javax.swing.JPanel();
        rightPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());
        add(leftPanel, java.awt.BorderLayout.WEST);
        add(rightPanel, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    public void update(Market market) {
        List<Index> indices = org.yccheok.jstock.engine.Utils.getStockIndices(country);
        final java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
        
        map.get("volume").setText(numberFormat.format(market.getVolume()));
        map.get("up").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Up)));
        map.get("down").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Down)));
        map.get("unchanged").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Unchange)));

        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);

        for(Index index : indices) {
            if(ignoreIndices.contains(index)) continue;
            
            final double _index = market.getIndex(index);
            final double change = market.getChange(index);
            final Color color = Utils.getColor(change, 0.0);
            
            JLabel label = map.get(index.name());
            
            if(label == null) continue;
            
            label.setText(numberFormat.format(_index) + " (" + numberFormat.format(change) + ")");
            label.setForeground(color);
        }                
        
        map.get("value").setText(numberFormat.format(market.getValue()));
    }
    
    private void initAccordingToCountry(Country country) {
        List<Index> indices = org.yccheok.jstock.engine.Utils.getStockIndices(country);
        for(Index index : indices) {
            if(ignoreIndices.contains(index)) continue;
            
            JLabel name = new JLabel(index.toString() + " : ");
            leftPanel.add(name);
            JLabel value = new JLabel();
            value.setName(index.name());
            map.put(index.name(), value);
            value.setFont(new java.awt.Font("Tahoma", 1, 11));
            leftPanel.add(value);
        }
        
        JLabel volume = new JLabel("Volume (Lots) : ");
        leftPanel.add(volume);
        JLabel volume_value = new JLabel();
        volume_value.setName("volume");
        map.put("volume", volume_value);
        volume_value.setFont(new java.awt.Font("Tahoma", 1, 11));
        volume_value.setForeground(new java.awt.Color(153, 102, 0));
        leftPanel.add(volume_value);

        JLabel value = new JLabel("Value ($) : ");
        leftPanel.add(value);
        JLabel value_value = new JLabel();
        value_value.setName("value");
        map.put("value", value_value);
        value_value.setFont(new java.awt.Font("Tahoma", 1, 11));
        value_value.setForeground(new java.awt.Color(153, 102, 0));
        leftPanel.add(value_value);
        
        JLabel up = new JLabel("Up : ");
        rightPanel.add(up);
        JLabel up_value = new JLabel();
        up_value.setName("up");
        map.put("up", up_value);
        up_value.setFont(new java.awt.Font("Tahoma", 1, 11));
        up_value.setForeground(new java.awt.Color(50, 150, 0));
        rightPanel.add(up_value);

        JLabel down = new JLabel("Down : ");
        rightPanel.add(down);
        JLabel down_value = new JLabel();
        down_value.setName("down");
        map.put("down", down_value);        
        down_value.setFont(new java.awt.Font("Tahoma", 1, 11));
        down_value.setForeground(new java.awt.Color(200, 0, 50));
        rightPanel.add(down_value);

        JLabel unchanged = new JLabel("Unchaged : ");
        rightPanel.add(unchanged);
        JLabel unchanged_value = new JLabel();
        unchanged_value.setName("unchanged");
        map.put("unchanged", unchanged_value);        
        unchanged_value.setFont(new java.awt.Font("Tahoma", 1, 11));
        unchanged_value.setForeground(new java.awt.Color(0, 0, 0));
        rightPanel.add(unchanged_value);      
    }
    
    public Country getCountry() {
        return this.country;
    }
    
    private Country country = null;
    private Map<String, JLabel> map = new HashMap<String, JLabel>();
    
    private static final List<Index> ignoreIndices = new ArrayList<Index>();
    
    static {
        ignoreIndices.add(Index.Mesdaq);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel rightPanel;
    // End of variables declaration//GEN-END:variables

}

⌨️ 快捷键说明

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