JTextField问题
import org.jsoup.Jsoup;
@SuppressWarnings("unused")
public class SimpleWebCrawler extends JFrame {
JTextField yourInputField = new JTextField(20);
static JTextArea _resultArea = new JTextArea(200, 200);
JScrollPane scrollingArea = new JScrollPane(_resultArea);
private final static String newline = "\n";
public SimpleWebCrawler() throws MalformedURLException {
_resultArea.setEditable(false);
String word2 = yourInputField.getText();
try {
URL my_url = new URL("http://" + word2 + "/");
BufferedReader br = new BufferedReader(new InputStreamReader(
my_url.openStream()));
String strTemp = "";
while (null != (strTemp = br.readLine())) {
_resultArea.append(strTemp + newline);
}
} catch (Exception ex) {
ex.printStackTrace();
}
_resultArea.append("\n");
_resultArea.append("\n");
_resultArea.append("\n");
String url = "http://" + word2 + "/";
print("Fetching %s...", url);
try{
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
System.out.println("\n");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
_resultArea.append("\n");
for (Element link : links) {
print(" %s ", link.attr("abs:href"), trim(link.text(), 35));
bw.write(link.attr("abs:href"));
bw.write(System.getProperty("line.separator"));
}
bw.flush();
bw.close();
} catch (IOException e1) {
}
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
content.add(yourInputField);
this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
private static void print(String msg, Object... args) {
_resultArea.append(String.format(msg, args) +newline);
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width - 1) + ".";
else
return s;
}
//.. Get the content pane, set layout, add to center
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
}
我正在尝试创建一个 JTextField 来接收用户的输入。我创建了 JTextField 的实例并将其添加到 JFrame 中。但是,这段代码不起作用。介意指出我的错误吗?它应该可以工作,但我找不到问题所在。我还想念其他东西吗?
JTextField 的代码:
JTextField yourInputField = new JTextField(20);
String word2 = yourInputField.getText();
content.add(yourInputField);
此行显示 IllegalArgumentException 的错误。
my_url.openStream()
我期望看到弹出一个 JTextField 来接收随机 URL 的输入,并且代码将处理该 URL。抱歉我的问题表现不佳。我对编程问答论坛不是很熟悉。
import org.jsoup.Jsoup;
@SuppressWarnings("unused")
public class SimpleWebCrawler extends JFrame {
JTextField yourInputField = new JTextField(20);
static JTextArea _resultArea = new JTextArea(200, 200);
JScrollPane scrollingArea = new JScrollPane(_resultArea);
private final static String newline = "\n";
public SimpleWebCrawler() throws MalformedURLException {
_resultArea.setEditable(false);
String word2 = yourInputField.getText();
try {
URL my_url = new URL("http://" + word2 + "/");
BufferedReader br = new BufferedReader(new InputStreamReader(
my_url.openStream()));
String strTemp = "";
while (null != (strTemp = br.readLine())) {
_resultArea.append(strTemp + newline);
}
} catch (Exception ex) {
ex.printStackTrace();
}
_resultArea.append("\n");
_resultArea.append("\n");
_resultArea.append("\n");
String url = "http://" + word2 + "/";
print("Fetching %s...", url);
try{
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
System.out.println("\n");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
_resultArea.append("\n");
for (Element link : links) {
print(" %s ", link.attr("abs:href"), trim(link.text(), 35));
bw.write(link.attr("abs:href"));
bw.write(System.getProperty("line.separator"));
}
bw.flush();
bw.close();
} catch (IOException e1) {
}
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
content.add(yourInputField);
this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
private static void print(String msg, Object... args) {
_resultArea.append(String.format(msg, args) +newline);
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width - 1) + ".";
else
return s;
}
//.. Get the content pane, set layout, add to center
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
}
I am trying to create a JTextField to receive input from the user. I have created an instance of JTextField and added into the JFrame. However, this code is not working. Mind point out my mistakes ? It suppose to work, however I could not find out the problem is. Do I miss something else ?
The code for the JTextField :
JTextField yourInputField = new JTextField(20);
String word2 = yourInputField.getText();
content.add(yourInputField);
This line shows the error of the IllegalArgumentException.
my_url.openStream()
I expected to see a JTextField pop up to receive inputs which is a random URL and the code will process the URL. Sorry for my bad display of question. I am not very familiar with programming question answering forum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从哪里读取文本字段的输入?我看到它在构造函数中发生过一次,但由于您正在从新的空文本字段读取文本,因此您不会通过该调用从用户那里获得任何输入。
如果您希望用户能够将数据输入到文本字段中并在之后处理输入,您将需要使用带有动作侦听器等的事件驱动编程。这将允许当用户执行某些操作(例如文本输入)时运行部分程序。
Where do you read input from the text field? I see it happening once in the constructor, but since you're reading the text from a new, empty text field, you won't be getting any input from the user with that call.
If you want the user to be able to input data into the text field and have the input processed afterwards, you're going to need to use event-driven programming with action listeners and the like. This will allow parts of the program to run when the user performs certain actions, like text input.
这里的一个问题是您没有指定要正确添加InputField 的位置。使用示例
代替
如果您没有指定值,BorderLayout 默认情况下会添加到中心,因此您看不到滚动区域。
One problem here is that you are not specifying where you want to add yourInputField correctly. Use for example
instead
If you are not specifying value the BorderLayout adds by default to center thus you do not see the scrolling area.