📄 example8_6.java
字号:
/*
* Example8_6.java
*
* Created on 2006年10月19日, 上午11:02
*/
package example8_6;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
*
* @author Administrator
*/
public class Example8_6 extends Applet implements ActionListener
{
/** Initializes the applet Example8_6 */
/* public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}*/
private static final File INFO_FILE =new File("hqsj.txt");
private Hashtable stockInfo;
TextField stockID;
Button button1;
private String quoteid,quotename;
public void init(){
add(new Label("股票代码"));
stockID = new TextField(6);
add(stockID);
button1 = new Button("查询");
button1.addActionListener(this);
add(button1);
resize(500, 300);
}
public void start()
{
loadinfo();
}
protected boolean loadinfo()
{
String fileLine;StringTokenizer tokenize;
String id;StringBuffer name;
try {
// 创建一个访问数据文件的stream
BufferedReader stockInput = new
BufferedReader(new FileReader(INFO_FILE));
// 创建Hashtable对象
stockInfo = new Hashtable();
// 每次从文件中读一行数据
while ((fileLine = stockInput.readLine()) != null)
{
// 将每一行数据分解为tokens.
tokenize = new StringTokenizer(fileLine);
try {
id = tokenize.nextToken();
// 创建一个放置股票信息的buffer
name = new StringBuffer();
while(tokenize.hasMoreTokens())
{
name.append(tokenize.nextToken());
if (tokenize.hasMoreTokens()){name.append("");}
}
// 向Hashtable中充填记录
stockInfo.put(id,name.toString());
}
catch(NullPointerException excpt){System.err.println("充填数据时出错: " + excpt);}
catch(NoSuchElementException excpt){System.err.println("无效的数据 记录 " +"in file: " + excpt);}
}
stockInput.close();
}
catch(FileNotFoundException excpt)
{
System.err.println("不能发现文件: " + excpt);
return false;
}
catch(IOException excpt)
{
System.err.println("I/O故障: " + excpt);
return false;
}
return true;
}
protected String getQuote(String StockID)
{
String info;
// 从Hashtable得到数据
info = (String)stockInfo.get(StockID);
if (info != null) return info;
else return "股票代码错误!";
}
public void paint(Graphics g)
{
g.drawString("股票代码"+quoteid+":" ,10,60);
g.drawString("股票名称"+"前收"+"今开"+"最高"+"最低"+"收盘"+"交易量"+" 交易金额", 10, 90);
g.drawString(quotename, 10, 120);
}
public void actionPerformed(ActionEvent ev)
{
String label = ev.getActionCommand();
if (label.equals("查询"))
{
quoteid = stockID.getText();
if(quoteid != null) quotename = getQuote(quoteid);
else quotename = "请输入股票代码!";
repaint();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}
// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -