Sunday, 4 December 2011

Membuat Web Browser Sederhana

Berikut ini contoh program JAVA untuk membuka source code sebuah website, yang membuat program ini sebenarnya bukan saya tapi dosen saya hehe

mohon izin tampilkan pak :D

cek dimari gan :

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
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.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class MyGoogle {

    public static void main(String[] args) {

        final JFrame frame = new JFrame();
        frame.setSize(450, 200);
        frame.setTitle("My Google: " + frame.getWidth());
//        frame.setSize(450, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        // frame.setUndecorated(true);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new FlowLayout());

        final JLabel label = new JLabel("ENTER  THE  URL");
        label.setFont(new Font("", 10, 40));// 10->12, 40->80
        label.setForeground(Color.MAGENTA);
        frame.add(label);

        final JTextField field = new JTextField(25);
        field.setToolTipText("zwww.facebook.com");
        frame.add(field);

        JButton button = new JButton("Search !");
        button.setForeground(Color.BLACK);
        button.setBackground(Color.ORANGE);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (field.getText().equals("")) {
                    System.out.println("tidak ada teks yang ditulis!");
                    // JOptionPane.showConfirmDialog(frame, label, "nothing",
                    // 1);
                    JOptionPane.showMessageDialog(frame, label,
                            "Judul Pesannya", 1);// ---> maksudnya 0=x, 1=i,
                                                    // 2=!, 3=?
                } else {
                    // frame.setVisible(false);
                    JFrame frame2 = new JFrame("Frame Hasil");
                    // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame2.setSize(600, 400);
                    frame2.setLocationRelativeTo(frame);
                    final JTextPane textPane = new JTextPane();
                    final JScrollPane scrollPane = new JScrollPane(textPane);

                    panggilKoneksi(field, textPane);

                    frame2.add(scrollPane);
                    frame2.setVisible(true);
                }
            }

            private void panggilKoneksi(final JTextField field,    final JTextPane textPane) {
                try {
                    URL url = new URL(field.getText());

                    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);
                    }

                    textPane.setText(new String(os.toByteArray()));

                    os.close();
                    is.close();

                } catch (MalformedURLException e) {
                    // e.printStackTrace();
                    // System.out.println("terjadi kesalahan penulisan url!");
                    JLabel label2 = new JLabel("Salah Tulis di URL.");
                    JOptionPane.showMessageDialog(frame, label2,
                            "Kesalahan Tulis", 1);
                } catch (IOException e) {
                    // e.printStackTrace();
                    // System.out.println("terjadi kesalahan penulisan url!");
                    JLabel label3 = new JLabel("Salah Tulis di URL ini.");
                    JOptionPane.showMessageDialog(frame, label3,
                            "Kesalahan Tulis", 0);
                }
            }
        });
        frame.add(button);

         JRadioButton radioButton = new
         JRadioButton(" penelusuran dengan web ");
         frame.add(radioButton);
       
         JRadioButton radioButton2 = new
         JRadioButton(" penelusuran dengan gambar ");
         frame.add(radioButton2);

        JRadioButtonMenuItem item = new JRadioButtonMenuItem("choose 1");
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("helooo...");
            }
        });
        frame.add(item);

        JRadioButtonMenuItem item2 = new JRadioButtonMenuItem("choose 2");
        item2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("test....");
            }
        });
        frame.add(item2);

        JRadioButtonMenuItem item3 = new JRadioButtonMenuItem("choose 3");
        item3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("test heloo..");
            }
        });
        frame.add(item3);

        JRadioButtonMenuItem item4 = new JRadioButtonMenuItem("choose 4");
        item4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("terima kasih..");
                System.exit(0);
            }
        });
        frame.add(item4);

        frame.setVisible(true);

    }

}


penampakan SS nya gan :




No comments:

Post a Comment