package com.xunlei;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

/**
 * @author Hank
 * @date 2015-4-22 下午4:38:55
 */
@SuppressWarnings("serial")
public class XunLeiMain extends JFrame {

	public XunLeiMain(String title){
		super(title);
		setSize(600, 500);
		setLocationRelativeTo(null);
		setContentPane(createPanel());
	}
	
	private JPanel createPanel(){
		JPanel jpanel = new JPanel(new BorderLayout());
		jpanel.add(BorderLayout.NORTH, createNorthPanel());
		jpanel.add(createResultPane());
		return jpanel;
	}
	
	private static JTextField urlField;
	private static JButton load;
	private static JButton clear;
	private static JButton save;
	private static int count = 1;
	
	private JPanel createNorthPanel(){
		JPanel jpanel = new JPanel();
		//创建子组件
		urlField = new JTextField(20);
		urlField.setText("http://www.xunleihuiyuan.net/");
		load = new JButton("加载");
		clear = new JButton("清空");
		save = new JButton("保存");
		//给加载按钮绑定事件
		load.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(count > 1){
					int pushBut = JOptionPane.showConfirmDialog(null, "重新加载?", "提示",JOptionPane.YES_NO_OPTION);
					if(pushBut != JOptionPane.YES_OPTION){
						return;
					}
				}
				XunleihuiyuanNet xunleihuiyuanNet = new XunleihuiyuanNet();
//				String url = "http://www.xunleihuiyuan.net/";
//				urlField.setText("http://www.xunleihuiyuan.net/");
				String url = urlField.getText();
				xunleihuiyuanNet.save1(url);
				String href = xunleihuiyuanNet.jx1(url);
				xunleihuiyuanNet.save2(href);
				String result = xunleihuiyuanNet.jx2();
				jtextArea.setText(result);
				count ++;
				JOptionPane.showMessageDialog(null, "加载成功!");
			}
		});
		//给重载按钮绑定事件
		clear.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jtextArea.setText("");
			}
		});
		//给保存按钮绑定事件
		save.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				File file = new File(getTitle());
				if(!file.exists()){
					FileDialog fd = new FileDialog(new JFrame(), "保存文本", FileDialog.SAVE);
					fd.setFile(getTitle());
					fd.setVisible(true);
					if(fd.getDirectory() == null){
						return;
					}
					file = new File(fd.getDirectory() + fd.getFile() + ".txt");
					setTitle(getTitle());
				}
				BufferedWriter bw = null;
				try {
					bw = new BufferedWriter(new FileWriter(file, false));
					bw.append(jtextArea.getText());
					bw.flush();
				} catch (IOException ioe) {
					JOptionPane.showMessageDialog(null, "提示消息", "无法保存文件", JOptionPane.ERROR_MESSAGE);
				} finally {
					try {
						if(bw != null){
							bw.close();
							JOptionPane.showMessageDialog(null, "保存成功!");
						}
					} catch (IOException ioe) {
						JOptionPane.showMessageDialog(null, "保存失败!", "保存失败", JOptionPane.ERROR_MESSAGE);
						ioe.printStackTrace();
					}
				}
			}
		});
		//把子组件添加进面板
		jpanel.add(urlField);
		jpanel.add(load);
		jpanel.add(clear);
		jpanel.add(save);
		
		return jpanel;
	}
	
	JTextArea jtextArea;
	private JScrollPane createResultPane(){
		JScrollPane pane = new JScrollPane();
		pane.setBorder(new TitledBorder("编辑区域"));
		jtextArea = new JTextArea();
		jtextArea.setFont(new Font("宋体", Font.PLAIN, 13));
		jtextArea.setLineWrap(true);
		pane.getViewport().add(jtextArea);
		return pane;
	}
	
	public static void main(String[] args) {
		new XunLeiMain("新建文本文件").setVisible(true);
	}
}
最近下载更多
gan857569302  LV9 2020年7月15日
xsj123456  LV1 2019年10月25日
17600446733  LV21 2019年6月11日
lris_luanling  LV11 2018年11月27日
1324488732  LV27 2018年11月22日
温涛涛  LV8 2018年11月5日
040413  LV3 2018年10月22日
dffdfdaf  LV3 2018年10月17日
chenzhen123456  LV9 2018年9月25日
murphyLu  LV12 2018年8月28日
最近浏览更多
zxf2342  LV2 2023年4月27日
猫和老鼠sa 2022年12月2日
暂无贡献等级
山鬼  LV2 2021年12月8日
a320575517 2021年11月24日
暂无贡献等级
1798672867  LV21 2021年7月4日
David 2021年7月2日
暂无贡献等级
1964341683 2021年6月21日
暂无贡献等级
Sean_admin  LV7 2021年4月28日
1530688385  LV5 2020年10月22日
397312607  LV2 2020年9月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友