java保存图片的方法,求大牛帮助
我刚学Java不久,老师让做一个网页浏览器,只可以浏览源码那种,实现三种功能,我先贴代码
import java.awt.EventQueue;,现在实现了其中的两个功能,但是第三个,搞不定,首先执行连接出现
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import javax.swing.JTextPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import java.io.*;
import javax.swing.JScrollBar;
import java.awt.ScrollPane;
import java.awt.Panel;
import javax.swing.JSlider;
import java.awt.Scrollbar;
public class Text {
private JFrame frame;
private JTextField txtHttp;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Text window = new Text();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Text() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("网页浏览器");
frame.setBounds(100, 100, 705, 423);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenuItem menuItem = new JMenuItem("\u5730\u5740\u680F");
menuBar.add(menuItem);
txtHttp = new JTextField();
txtHttp.setText("http://www.yahoo.com/");
menuBar.add(txtHttp);
txtHttp.setColumns(10);
JButton button = new JButton("\u8FDE\u63A5");
menuBar.add(button);
button.addActionListener(new MyMonitor());
JButton button_1 = new JButton("\u4FDD\u5B58");
menuBar.add(button_1);
button_1.addActionListener(new MyMonitor1());
JButton button_2 = new JButton("\u4FDD\u5B58\u56FE\u7247");
menuBar.add(button_2);
button_2.addActionListener(new MyMonitor2());
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
class MyMonitor implements ActionListener{
public void actionPerformed(ActionEvent e){
String s=txtHttp.getText();
txtHttp.setText("加载中请稍候....");
//System.out.println(s);
try {
URL url=new URL(s);
try {
URLConnection wwwConnection=url.openConnection();
String line=System.getProperty("line.separator");
DataInputStream dis=new DataInputStream(wwwConnection.getInputStream());
String inputLine;
while((inputLine=dis.readLine())!=null)
{
//System.out.println(inputLine);
textArea.append(inputLine);
textArea.append(line);
}
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
} catch (MalformedURLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
txtHttp.setText("加载成功");
}
}
class MyMonitor1 implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
int chooser1 = chooser.showSaveDialog(frame); //显示保存文件选择窗体
File curFile = chooser.getSelectedFile();
try {
FileWriter fw1 = new FileWriter(curFile);
BufferedWriter bw1 = new BufferedWriter(fw1);
String g = textArea.getText();
txtHttp.setText("保存成功");
bw1.write(g); //将界面文本框中内容写入所获取的文件中
bw1.flush(); //清空缓存区数据
bw1.close(); //关闭文件
fw1.close();
}
catch (IOException ex) {
}
/*try {
FileOutputStream fos=new FileOutputStream("1.html");
String file;
file=textArea.getText();
try {
fos.write(file.getBytes());
txtHttp.setText("保存成功");
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}*/
}
}
class MyMonitor2 implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
}