1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// expected results in the JTextField
// AF.PA;36,39;4/5/2007;17h35;+1,25;35,53;36,62;35,52;2547827
// LVMH.PA;84,28;4/3/2007;17h38;+1,16;83,80;84,58;83,32;1963844
import java.applet.AppletContext;
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
public class ShowDocument extends JApplet
implements ActionListener {
the_URLWindow the_URLWindow;
public void init() {
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
private void createGUI() {
JButton button = new JButton("Bring up the_URL window");
button.addActionListener(this);
add(button);
JFrame.setDefaultLookAndFeelDecorated(true);
the_URLWindow = new the_URLWindow(getAppletContext());
the_URLWindow.pack();
}
public void destroy() {
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
destroyGUI();
}
});
} catch (Exception e) { }
}
private void destroyGUI() {
the_URLWindow.setVisible(false);
the_URLWindow = null;
}
public void actionPerformed(ActionEvent event) {
the_URLWindow.setVisible(true);
}
}
class the_URLWindow extends JFrame
implements ActionListener {
JTextField the_URL_Field;
JComboBox choice;
AppletContext appletContext;
public the_URLWindow(AppletContext appletContext) {
super("Show a Document!");
this.appletContext = appletContext;
JPanel contentPane = new JPanel(new GridBagLayout());
setContentPane(contentPane);
contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label1 = new JLabel("the_URL of document to show: ",
JLabel.TRAILING);
add(label1, c);
the_URL_Field = new JTextField("http://fr.finance.yahoo.com/d/quotes.txt?m=PA&f=sl1d1t1c1ohgv&e=.csv&s=AF.PA&s=LVMH.PA", 20);
label1.setLabelFor(the_URL_Field);
the_URL_Field.addActionListener(this);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
add(the_URL_Field, c);
JLabel label2 = new JLabel("Window/frame to show it in: ",
JLabel.TRAILING);
c.gridwidth = 1;
c.weightx = 0.0;
add(label2, c);
String[] strings = {
"(browser's choice)", //don't specify
"My Personal Window", //a window named "My Personal Window"
"_blank", //a new, unnamed window
"_self",
"_parent",
"_top" //the Frame that contained this applet
};
choice = new JComboBox(strings);
label2.setLabelFor(choice);
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(5,0,0,0);
c.anchor = GridBagConstraints.LINE_START;
add(choice, c);
JTextField box = new JTextField("Results");
//box.setLines(10);
box.setColumns(100);
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(5,100,100,0);
c.anchor = GridBagConstraints.LINE_START;
add(box, c);
JButton button = new JButton("Show document");
button.addActionListener(this);
c.weighty = 1.0;
c.ipadx = 10;
c.ipady = 10;
c.insets = new Insets(10,0,0,0);
c.anchor = GridBagConstraints.PAGE_END;
add(button, c);
}
public void actionPerformed(ActionEvent event) {
String the_URL_String = the_URL_Field.getText();
URL the_URL = null;
try {
the_URL = new URL(the_URL_String);
} catch (MalformedURLException e) {
System.err.println("Malformed URL: " + the_URL_String);
}
if (the_URL != null) {
box.setText(showDocument(the_URL));// *********** compilation bloque ici *************
/*
URLConnection conn = the_URL.openConnection();
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
*/
/*
if (choice.getSelectedIndex() == 0) {
appletContext.showDocument(the_URL);
} else {
appletContext.showDocument(the_URL,
(String)choice.getSelectedItem());
}
*/
}
}
} |
Partager