creating a JTextField to receive input from user
System.out.println("Please enter the website :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan2.nextLine();
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();
}
System.out.println("\n");
System.out.println("\n");
System.out.println("\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) {
Hi, this is my code to extract links from a web address. The user will key in the desired URL and this code will extract links from the URL.
This code prompt the user to key in for URL in the ECLIPSE IDE console. After keyed in the input, the code will extract links from the URL and transfer the output to a JTextArea.
What i wanted to do now is, i would like to create a Jtextfield to receive the user input rather than the user key in the input inside the console.
The line of code that is responsible for handling the string input is :
URL my_url = new URL("http://" + word2 + "/");
and
String url = "http://" + word2 + "/";
I have not much experie开发者_StackOverflow社区nce in development of GUI, i hope someone can guide me. Thanks.
You could use a JButton
with an ActionListener
to grab the input when the user clicks on the Button. Use textField.getText()
to get a String with the textfield input.
Here's a short example:
// Create a Textfield
JTextField textField = new JTextField();
// Create a Button
JButton button = new JButton("Let's go");
// Add an Actionlistener to the button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// Set word2 with the input string from the textfield
word2 = textField.getText();
}
});
// Create a window
JFrame window = new JFrame();
// Exit on close
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the LayoutManager
window.setLayout(new BorderLayout());
// Add the textfield and button to the JFrames contentpane.
window.add(textField);
window.add(button, BorderLayout.SOUTH);
window.pack();
window.setVisible(true);
There are some great examples from Oracle that explain the usage of JTextFields: http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html
I didn't test the code below, and you should listen to MByD and follow a Swing tutorial. But just to give you an example, here is how it might work.
public class MyFrame extends JFrame{
private JTextField textField;
public MyFrame(){
this.textField = new JTextField();
add(this.textField);
}
public JTextField getTextField(){
return this.textField;
}
}
And somewhere in your code:
...
MyFrame myFrame = new MyFrame();
myFrame.pack();
myFrame.setVisible(true);
...
After this point, you can get the value in the textbox using the reference to your frame, such as:
myFrame.getTextField.getText();
精彩评论