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

📄 testprog.java

📁 一个出售卡片信息的源码 是我的一个作业
💻 JAVA
字号:
import java.io.*;
import java.net.URL;
import java.awt.BorderLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

import java.util.regex.*;

import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.StringTokenizer;
import java.util.regex.Matcher;

public class TestProg {

	private static TestProg testProg = new TestProg();
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setBounds(50, 150, 520, 400);
		// frame.getContentPane().add(
		frame.getContentPane().setLayout(new BorderLayout());
		XmasSalesFrame jP = new XmasSalesFrame();
		frame.getContentPane().add(jP);
		frame.setVisible(true);
		frame.setTitle("Xmas");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				File f = new File("n.html");
				f.delete();
				System.exit(1);
			}
		});
		try {
			testProg.readCard();
		} catch (IOException ex) {
			ex.printStackTrace();
		}	
	}

	/**
	 * read the card information from the specific file
	 */
	private void readCard() throws IOException {		
	URL url = this.getClass().getResource("cards.txt");		
	
	JEditorPane je = new JEditorPane();
	
	je.setPage(url);
	
	String text = je.getText();

	String line = null;

	StringTokenizer to = new StringTokenizer(text, "\n");
	
		while (to.hasMoreTokens()) {
			line = to.nextToken();
			
			if(line!=null){
				Matcher p = Pattern.compile("\\S+").matcher(line);
				String index = null;
				String name = null;
				double price = -1;
				String group = " ";
				if (p.find()) 
					index = p.group();
				if(p.find())									
					name = p.group();
				while(price==-1&&p.find()){
					try {
						group = p.group();
						price = Double.parseDouble(group);
					} catch (NumberFormatException e) {
						name += " "+group;						
					}
				}
				if (index != null && name != null && price != -1) {
					Card card = new Card(index, name, price);
					CardList.addCard(card);
					System.out.println("添加"+index);
				}else{
					System.out.println("没有添加");
				}
			}
		}
	}
}

⌨️ 快捷键说明

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