之前编写过一个类似的软件,功能太少,最近又加了些功能,截取了来自纸金网的每日纸黄金操作建议和实时的汇率图,这次综合使用了SWT、AWT与SWING,需要的准备工作不再赘述,读者去之前的那篇文章好啦。软件效果图(一次会打开两个窗口):
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasChildFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
public class huilvzhuanhuan备份 {
public static String huilv() throws ParserException{
String RMB = null; //完整汇率
String tmp = null;
String RMB1 = null;//汇率前N位
Parser parser = new Parser(“http://www.google.cn/search?hl=zh-CN&newwindow=1&q=USD/RMB&btnG=Google+搜索&meta=&aq=f&oq=”);
parser.setEncoding(“GB2312″);
NodeFilter filter=new AndFilter(new TagNameFilter(“table”),new HasChildFilter(new TagNameFilter(“tr”)));
//TagNameFilter filter = new TagNameFilter(“td”);
NodeList nl =parser.parse(filter);
for(int i =1;i <nl.size();i++)
{
tmp = nl.elementAt(i).toPlainTextString();
if (tmp.indexOf(“1美元 /人民币”)>=0)
{
RMB=nl.elementAt(i).toPlainTextString();
}
}
RMB=RMB.replaceAll(” “,”");
RMB=RMB.replaceAll(“ ”,”");
RMB1 = (RMB.substring(8,18));
return RMB1;
}
public static String word() throws ParserException{
String words = null; //
String tmpwords = null;
//Parser parser = new Parser(“http://www.zhijinwang.cn/Article/Class21/21_Index.html”);
Parser parser = new Parser(“http://www.zhijinwang.cn/Article/Class21/21_Index.html”);
parser.setEncoding(“GB2312″);
//NodeFilter filter=new AndFilter(new TagNameFilter(“table”),new HasChildFilter(new TagNameFilter(“tr”)));
TagNameFilter filter = new TagNameFilter(“td”);
NodeList nl =parser.parse(filter);
for(int i =81;i <98;i++)
{
tmpwords = nl.elementAt(i).toPlainTextString(); //System.out.println(“”+i);
if (tmpwords.indexOf(“纸黄金介绍”)>=0)
{
words=nl.elementAt(i+2).toPlainTextString();
}
}
words=words.replaceAll(” “,”");
words=words.replaceAll(“ ”,”");
words=words.replaceAll(“今日整体观点”,”");
words=words.replaceAll(“,”,” “);
words=words.replaceAll(“:”,”");
//RMB1 = (RMB.substring(10,30));
System.out.println(“”+words);
return words;
}
public void first() {
Display display = new Display ();
final Shell shell= new Shell(display);
shell.setText(“炒金锅-shuil”);
try {
URL url = new URL(“http://www.kitco.cn/cn/metals/gold/t24_au_cny_gram_450x275g.gif “);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// Image image = new Image(display, url.openStream());
shell.setBounds(100,100,240,175);
Label label1=new Label(shell,SWT.SHADOW_NONE);
label1.setBounds(20, 20, 50, 20);
label1.setText(“人民币”);
Label label11=new Label(shell,SWT.SHADOW_NONE);
label11.setBounds(160, 20, 50, 20);
label11.setText(“/克”);
final Text t1=new Text(shell,SWT.BORDER);
t1.setBounds(80, 20, 80, 20);
Label label2=new Label(shell,SWT.SHADOW_NONE);
label2.setBounds(20, 60, 50, 20);
label2.setText(“美元”);
Label label21=new Label(shell,SWT.SHADOW_NONE);
label21.setBounds(160, 60, 50, 20);
label21.setText(“/盎司”);
final Text t2=new Text(shell,SWT.BORDER);
t2.setBounds(80, 60, 80, 20);
Label label3=new Label(shell,SWT.SHADOW_NONE);
label3.setBounds(20, 80, 50, 20);
label3.setText(“实时汇率”);
final Text t3=new Text(shell,SWT.BORDER);
t3.setBounds(80, 80, 80, 20);
t3.setBackground(new Color(display, 230, 230, 230));
try {
t3.setText(huilv());
} catch (ParserException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}//显示汇率
final StyledText t4=new StyledText(shell,SWT.BORDER|SWT.READ_ONLY); //自动换行
t4.setWordWrap(true);//自动换行
t4.setBounds(20, 100, 200, 30);
t4.setBackground(new Color(display, 230, 230, 230));
try {
t4.setText(“”+ word());
} catch (ParserException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Button button=new Button(shell,SWT.PUSH);
button.setBounds(90, 40, 15, 20);
button.setText(“♀”);
Button button2=new Button(shell,SWT.PUSH);
button2.setBounds(130, 40, 15, 20);
button2.setText(“♂”);
Button button3=new Button(shell,SWT.PUSH);
button3.setBounds(160, 80, 15, 20);
button3.setText(“R”);
button3.addListener(SWT.Selection,new Listener(){
public void handleEvent(Event ev)
{
try {
t3.setText(huilv());
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button.addListener(SWT.Selection,new Listener(){
public void handleEvent(Event ev)
{
float USDRMB1 = 0;
try {
USDRMB1 = Float.parseFloat(huilv());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
float m = 0;
String s=t1.getText();//输入rmb
try {
m=(float) (Float.parseFloat(s)/USDRMB1*31.1034768);
t2.setText(“”+m);
} catch(Exception e) {
t2.setText(“”);
}
}
});
button2.addListener(SWT.Selection,new Listener(){
public void handleEvent(Event ev2)
{
float USDRMB1 = 0;
try {
USDRMB1 = Float.parseFloat(huilv());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
float r = 0;
String s=t2.getText();//输入rmb
try {
r=(float) (Float.parseFloat(s)*USDRMB1/31.1034768);
t1.setText(“”+r);
} catch(Exception e) {
t1.setText(“”);
}
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
纸黄金实时图片部分:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Sql
{
static Timer timer=new Timer();
public static void main(String[] args) {
timer.schedule(new task(),0,10000);
huilvzhuanhuan备份 head = new huilvzhuanhuan备份();
head.first();
}
static class task extends TimerTask{
int i=0;
JFrame frame = new JFrame();
public void run()
{
//frame.setVisible(false);
URL url = null;
try {
url = new URL(“http://www.kitco.cn/cn/live_charts/goldcny.gif”);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage image = null;
try {
image = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label,JLabel.CENTER);
frame.pack();
frame.setVisible(true);
System.out.println(i++);
}
}
}




发表您的高见