标签:
package test; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class EmailSplider extends JFrame { private static final long serialVersionUID = -2498717483036732605L; private JTextField txtHttpvtiebabaiducomp; private JTextArea txtrn; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { EmailSplider frame = new EmailSplider(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } private String ReadHtml(URL txtUrl){ StringBuffer sb = new StringBuffer(); try { URLConnection conn =txtUrl.openConnection(); BufferedReader bf = new BufferedReader( new InputStreamReader( conn.getInputStream(),"utf-8")); String str = null; while((str = bf.readLine()) != null){ sb.append(str); } bf.close(); } catch (IOException e1) { e1.printStackTrace(); } return sb.toString(); } /** * Create the frame. */ public EmailSplider() { setResizable(false); setTitle("\u90AE\u7BB1\u63D0\u53D6\u5668"); setSize(663, 507); this.setLocationRelativeTo(null); //居中窗体 getContentPane().setLayout(null); JLabel label = new JLabel("\u8BF7\u8F93\u5165\u7F51\u5740"); label.setBounds(10, 10, 71, 15); getContentPane().add(label); txtHttpvtiebabaiducomp = new JTextField(); txtHttpvtiebabaiducomp.setText("http://v.tieba.baidu.com/p/3349997454"); txtHttpvtiebabaiducomp.setBounds(82, 7, 493, 21); getContentPane().add(txtHttpvtiebabaiducomp); txtHttpvtiebabaiducomp.setColumns(10); JButton button = new JButton("\u63D0\u53D6"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread( () -> { try { URL txtUrl = new URL(txtHttpvtiebabaiducomp.getText()); String html = ReadHtml(txtUrl); Pattern p =Pattern.compile("[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?"); Matcher m = p.matcher(html); while(m.find()){ //System.out.println(m.group()); try { SwingUtilities.invokeAndWait(()->{ txtrn.append(System.getProperty("line.separator")+m.group()); txtrn.setCaretPosition(txtrn.getText().length()); }); } catch (Exception e1) { e1.printStackTrace(); } } } catch (MalformedURLException e1) { JOptionPane.showMessageDialog(null, "请输入合法的网址!\n 必须以http:// 开头", "操作提示", JOptionPane.ERROR_MESSAGE); return; } }).start(); } }); button.setBounds(585, 6, 62, 23); getContentPane().add(button); txtrn = new JTextArea(); txtrn.setText("\u8FD9\u91CC\u663E\u793A\u63D0\u53D6\u7684\u90AE\u7BB1"); txtrn.setLineWrap(true); JScrollPane jScrollPane = new JScrollPane(txtrn); jScrollPane.setBounds(10, 35, 637, 432); getContentPane().add(jScrollPane ); jScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); } }
5、jar包附件
SwingUtilities.invokeLater( new Runnable() { @Override public void run() { txtrn.append(m.group()+System.getProperty("line.separator")); txtrn.setCaretPosition(txtrn.getText().length()); } } });
SwingUtilities.invokeAndWait(()->{ txtrn.append(System.getProperty("line.separator")+m.group()); txtrn.setCaretPosition(txtrn.getText().length()); });
Q3:怎么给JTextArea添加垂直滚动条?
txtrn = new JTextArea(); txtrn.setText("这里是内容"); txtrn.setLineWrap(true); //设置自动换行,让横向滚动条不显示 //用txtrn对象作为JScrollPane的构造方法的参数 JScrollPane jScrollPane = new JScrollPane(txtrn); //下面setBounds很重要 jScrollPane.setBounds(10, 35, 637, 432); //只需要把jScrollPane 添加到窗体,如果再添加txtrn对象就会乱。 getContentPane().add(jScrollPane );
Q4:提取邮箱的正则表达式怎么写?
标签:
原文地址:http://www.cnblogs.com/lovelp/p/EmailSplider.html