当前位置: 代码迷 >> J2SE >> 小白求教swing标签、控件如何获取数据
  详细解决方案

小白求教swing标签、控件如何获取数据

热度:46   发布时间:2016-04-24 00:47:01.0
小白求教swing标签、控件怎么获取数据?
package com.UrlHtml.Test;

import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.ws.Response;

public class Task extends WindowAdapter implements ActionListener
{
 JFrame f;
 //TextField tf;
 JTextField tf;
 JPanel p1,p2,p3;
 JButton bc1;  
 JLabel lb;
 JTextArea textArea;
 JScrollPane jsp;
 int a=20;
 int b=30;
 static SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
static final int split = 60 * 60 * 1000;
 public void display()
 {
f=new JFrame();
f.setTitle("抓取网页信息");  
f.setBounds(0, 0, 400, 600);
p1=new JPanel();
p1.setPreferredSize(new Dimension(300,50));
p1.setBackground(new Color(225,225,225));
p3=new JPanel();
p3.setPreferredSize(new Dimension(300,300)); 
Container c=f.getContentPane();
lb=new JLabel("请输入网址:");
p1.add(lb);
tf=new JTextField(15);
tf.setHorizontalAlignment(JTextField.CENTER );
p1.add(tf); //添加文本行
tf.setEditable(true);
bc1=new JButton("抓取");
bc1.setEnabled(true);
p1.add(bc1);
textArea=new JTextArea(50,35);
  textArea.setBackground(new Color(200,100,60));
  textArea.setLineWrap(true);
  JScrollPane scrollPane = new JScrollPane(textArea);
  p3.setLayout(new BorderLayout());
  p3.add(scrollPane, BorderLayout.CENTER);
p3.add(textArea);
bc1.addActionListener(this);
//f.addWindowListener(this);
f.add(p1,BorderLayout.NORTH);
f.add(p3,BorderLayout.CENTER);
f.setVisible(true);
 }

 public void windowClosing(WindowEvent e)
 {
  System.exit(0);
 }
 
 public void actionPerformed(ActionEvent e)
 {

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@SuppressWarnings("deprecation")
@Override
public void run() {
try {
//URL url = new URL("tf");
//eut系统
URL url = new URL("http://nn.nnedu.net.cn/");
//网站
URLConnection connection=url.openConnection();
DataInputStream in = new DataInputStream(url.openStream());
String inputStream = null;
inputStream = in.readLine();
while (inputStream != null) {
//textArea=inputStream;
System.out.println(inputStream);
inputStream = in.readLine();
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
}, 0, split);

if(e.getSource()==tf){


}else{

}

}
  public static void main(String args[])
  {
  (new Task()).display();
  }
}

我想让url等于tf输入的网址,点击抓取,然后抓取到的信息在textArea显示,并且让整个程序每一个小时自动运行一次。当抓取不到就弹出提示框。该怎么实现呢?



------解决方案--------------------
URL url = new URL( tf.getText() );
就可得到 tf 中输入的地址并将其作为 url
------解决方案--------------------