import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
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.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class google extends JFrame {
JButton button;
JTextField text;
JTextArea area;
JLabel label;
public static void main(String[] args) {
google f = new google();
f.setSize(500,600);
f.setTitle("# PIS 10 03 AAN");
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
}
google() {
final JTextArea area = new JTextArea(20,50);
final JScrollPane sr = new JScrollPane();
sr.getViewport().add(area);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("File ");
JMenu menu1 = new JMenu("Edit ");
JMenu menu2 = new JMenu("View ");
menu.setMnemonic(KeyEvent.VK_O);
menu.setVisible(true);
menu1.setVisible(true);
menu2.setVisible(true);
JLabel menuItem = new JLabel("Save ");
menu.add(menuItem);
menu.setMnemonic(KeyEvent.VK_O);
menuItem = new JLabel("Open");
menu.add(menuItem);
menu.setMnemonic(KeyEvent.VK_O);
menuItem = new JLabel("Exit");
menu.add(menuItem);
menu.setMnemonic(KeyEvent.VK_O);
menuItem = new JLabel("Edit ");
menu1.add(menuItem);
menu1.setMnemonic(KeyEvent.VK_O);
menuItem = new JLabel("View ");
menu2.add(menuItem);
menu2.setMnemonic(KeyEvent.VK_O);
label = new JLabel("GOOGLE ");
label.setFont(new Font("Georgia", Font.BOLD, 30));
text = new JTextField();
text.setFont(new Font("Tahoma", Font.PLAIN, 14));
text.setToolTipText("www.facebook.com");
button = new JButton("SEARCH");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
final JTextField field = new JTextField();
panggilKoneksi(text,area);
sr.setVisible(true);
}
private void panggilKoneksi(final JTextField text, final JTextArea jtx) {
try {
URL url = new URL(text.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);
}
jtx.setText(new String(os.toByteArray()));
os.close();
is.close();
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "Kesalahan Pada Link URL",
"Warning", 1);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Kesalahan Pada Link URL",
"Warning", 0);
}
}
});
text.setBounds(new Rectangle(30, 80, 300, 25));
button.setBounds(new Rectangle(350, 80, 90, 25));
menu.setBounds(new Rectangle(0, 0, 100, 50));
menu1.setBounds(new Rectangle(100, 0, 100, 50));
menu2.setBounds(new Rectangle(150, 0, 100, 50));
bar.setBounds(new Rectangle(0, 0, 500, 30));
sr.setBounds(new Rectangle(30, 130, 400, 400));
label.setBounds(new Rectangle(160, 30, 410, 50));
this.getContentPane().setLayout(null);
this.getContentPane().add(bar,null);
this.getContentPane().add(menu,null);
this.getContentPane().add(menu1,null);
this.getContentPane().add(menu2,null);
this.getContentPane().add(text,null);
this.getContentPane().add(button,null);
this.getContentPane().add(sr,null);
bar.add(menu);
bar.add(menu1);
bar.add(menu2);
sr.getViewport().add(area);
}
}
Hasilnya :
Tidak ada komentar:
Posting Komentar