我正在调用一个返回字符串(来自网页的编辑文本)的方法,并且我想将该字符串打印到我的 JTextArea 上。我知道我发送到 JTextArea 的字符串是正确的,因为它将正确打印到命令行,但不会打印到 JTextArea。一定是我在将它添加到 TextArea 时做错了什么。任何帮助,将不胜感激。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BrowserPanel extends JPanel {
private JTextField textField;
private String urlText;
private JTextArea textArea;
private BrowserPageReader myModel;
private String pageContent;
private BrowserFrame myFrame;
private String pageTitle;
private String pageBody;
public BrowserPanel(JTextField myTextField, BrowserPageReader model,
BrowserFrame frame)
{
myFrame = frame;
myModel = model;
textField = myTextField;
textField.addActionListener(new InputHandler());
/*JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250,250));*/
textArea = new JTextArea(20,40);
textArea.setEditable(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textArea);
add(scroll);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
private class InputHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
urlText = textField.getText();
//textArea.append(urlText);
myModel.setURL(urlText);
pageTitle = myModel.getTitle();
myFrame.setTitle(pageTitle);
pageBody = myModel.getBody();
textArea.setText(pageBody);
System.out.println(pageBody); //This prints out exactly what Im wanting
// Its just a test
textArea.repaint();
}
}
}
我猜我可能需要向我的paintComponent 添加一些东西,因为我的TextArea 位于附加到我的Panel 的scrollPane 中。我真的不知道出了什么问题。如果我把textArea.setText("blah");
它做它应该做的。我发送的变量是一个非常大的字符串,它是整个网页。这可能是问题吗?使用代码,textArea 保持空白,我希望它显示的内容正确打印到命令行。帮助!
编辑这里是我的代码的其余部分
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class BrowserFrame extends JFrame{
public BrowserFrame()
{
BrowserPageReader myModel = new BrowserPageReader();
setTitle("My Browser");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize =kit.getScreenSize();
setSize(screenSize.width/2,screenSize.height-500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JTextField textField = new JTextField(20);
BrowserPanel myPanel = new BrowserPanel(textField,myModel,this);
contentPane.add(myPanel);
contentPane.add(textField,BorderLayout.PAGE_START);
setVisible(true);
}
}
import javax.swing.*;
import java.io.*;
import java.net.*;
public class BrowserPageReader {
private URL myURL;
//private String webURL;
private String totalWebContent;
private String htmlString;
private String contentToPrint = " ";
private String urlPath;
private String urlHost;
private String pageTitle;
private String pageBody;
private String formattedBody;
public void setURL (String webURL)
{
try{
myURL = new URL(webURL);
urlPath = myURL.getPath();
urlHost = myURL.getHost();
}
catch(MalformedURLException e)
{
JOptionPane.showMessageDialog(null,"URL is incorrectly formatted");
}
}
public void retrieveContent()
{
try{
Socket socket = new Socket(urlHost,80);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new
BufferedReader(new InputStreamReader(socket.getInputStream()));
out.print("GET " + urlPath + " HTTP/1.1\n");
out.print("Host: "+ urlHost + "\n");
out.print("\n");
out.flush();
while((totalWebContent = in.readLine()) != null)
{
//System.out.println(totalWebContent);
htmlString = htmlString + totalWebContent;
//System.out.println(contentToPrint);
}
//System.out.println("htmlString\n" + htmlString);
}
catch(Exception e){
e.printStackTrace();
}
}
public String getTitle()
{
retrieveContent();
//System.out.println(htmlString);
pageTitle = htmlString.substring(htmlString.indexOf("<title>")+ 7,
htmlString.indexOf("</title>"));
return pageTitle;
}
public String getBody()
{
String toDelete;
String edited;
retrieveContent();
pageBody = htmlString.substring(htmlString.indexOf("<body")+5,
htmlString.indexOf("</body>"));
toDelete = pageBody.substring(0,pageBody.indexOf('<'));
edited = pageBody.replace(toDelete,"");
pageBody = edited
formattedBody = pageBody.replaceAll("<[^>]*>", "");
//System.out.println(formattedBody);
return formattedBody;
}