java jtextarea 自动换行_求助贴吧大神,怎么实现在JTextArea文本域里输出的内容自动换行...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.*;

public class H1 extends JFrame implements ActionListener{

Panel panel=null;

JButton button=null;

static JTextField text_field=null;//这里的静态是为了在静态的search()函数中引用

JTextArea text_area=null;

JLabel label=null;

JScrollPane scroll_pane=null;

public static void main(String[] args){

H1 h=new H1();

}

public H1(){

panel=new Panel();

label=new JLabel("URL:");

text_field=new JTextField(15);//在这里输入网址,前面要写http://

text_field.getText();

button=new JButton("搜索");

button.addActionListener(this);//添加监听

button.setActionCommand("search");//设置监听命令

text_area=new JTextArea();

text_area.setText(ReadURLif.sl);

scroll_pane=new JScrollPane(text_area);

panel.add(label); panel.add(text_field); panel.add(button);

this.add(panel,BorderLayout.NORTH);

this.add(scroll_pane);

this.setTitle("浏览器");

this.setIconImage((new ImageIcon("image/ie.jpg")).getImage());

this.setSize(400,300);

this.setLocation(200,150);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){//监听

if(e.getActionCommand().equals("search")){

ReadURLif.search();

text_area.setText(ReadURLif.sl);

}

}

}

class ReadURLif{//获取HTML源代码

static URL url;

static URLConnection context=null;

static InputStream in=null;

static String s=null;

static String sl=null;

public static void search()

{//静态是为了能够在启动监听的时候用类名调用search()函数,用对象名调用会出错

try{

url=new URL(H1.text_field.getText());

context = url.openConnection();

in = context.getInputStream();

BufferedReader str = new BufferedReader(new InputStreamReader(in,"gbk"));

while (str.ready()) {

s = str.readLine();

sl+=s;

}

}

catch(IOException ioe){

System.out.println("IOException");

}

}

}

class Panel extends JPanel{ //这个类在这段程序中没有任何作用

public void paint(Graphics g){//但是可拓展性强,方便添加函数

super.paint(g);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值