import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PIS1003_Mella {
public static void main(String[] args) {
final JFrame frame = new JFrame("PIS 10 03 Mella");
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel("URL");
final JTextField textField = new JTextField(25);
JButton button = new JButton("Search");
final JTextArea textArea = new JTextArea(20, 30);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(textArea);
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("file");
JMenu edit = new JMenu("Edit");
JMenu format = new JMenu("Format");
JMenu view = new JMenu("View");
JMenu help = new JMenu("Help");
menu.add(file);
menu.add(edit);
menu.add(format);
menu.add(view);
menu.add(help);
menu.setVisible(true);
JMenuItem menusetting = new JMenu("setting");
JMenuItem menusave = new JMenuItem("save");
JMenuItem menuclose = new JMenuItem("close");
JMenuItem menusetting1 = new JMenuItem("setting1");
JMenuItem menusetting2 = new JMenuItem("setting2");
JMenuItem menuundo = new JMenuItem("Undo" + " " + "ctrl+z");
JMenuItem menucut = new JMenuItem("Cut" + " " + "ctrl+x");
JMenuItem menupaste = new JMenuItem("Paste" + " " + "ctrl+v");
JMenuItem menudelete = new JMenuItem("Delete" + " " + "del");
JMenuItem menuword = new JMenuItem("Word Warp");
JMenuItem menufont = new JMenuItem("Font");
JMenuItem menustatus = new JMenuItem("Status Bar");
JMenuItem menuview = new JMenuItem("View Help");
JMenuItem menuabout = new JMenuItem("About Notepad");
menusetting.add(menusetting1);
menusetting.add(menusetting2);
file.add(menusetting);
file.addSeparator();
file.add(menusave);
file.add(menuclose);
edit.add(menuundo);
edit.add(menucut);
edit.add(menupaste);
edit.add(menudelete);
format.add(menuword);
format.add(menufont);
view.add(menustatus);
help.add(menuview);
help.add(menuabout);
file.setVisible(true);
panel.add(label);
panel.add(textField);
panel.add(button);
panel.add(scrollPane);
frame.add(panel);
frame.add(menu, BorderLayout.NORTH);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
koneksi(textField, textArea);
frame.setVisible(true);
}
});
}
private static void koneksi(final JTextField tf, final JTextArea ta) {
try {
URL url = new URL(tf.getText().trim());
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[10];
int len;
while ((len = is.read(b, 0, b.length)) != -1) {
os.write(b, 0, len);
}
ta.setText(new String(os.toByteArray()));
os.close();
is.close();
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "coba lagi url", "Warning", 1);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "coba lagi url", "Warning", 0);
}
}
}
Minggu, 04 Desember 2011
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar