IT_Programming/Java

웹 브라우저 실행하기

JJun ™ 2009. 2. 3. 10:54

package src;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;

 

public class WebBrowserPopUp extends Frame
{
         private Label l;
         private TextField tf;
         private Button b;
         private Panel p1, p2;
 
         public WebBrowserPopUp()
         {
                  super("Web Browser");
  
                  initLayout();
  
                  Toolkit tk = Toolkit.getDefaultToolkit();
                  Dimension screenSize = tk.getScreenSize();
  
                  this.setSize(250, 100);
                  this.setResizable(false);
                  this.setLocation(screenSize.width/2 - 125, screenSize.height/2 - 50);
                  this.setVisible(true);
  
                  this.addWindowListener(new WindowAdapter()
                   {
                            public void windowClosing(WindowEvent we)
                            {
                                     System.exit(0);
                            }
                   }  );
         }

 

         private void initLayout() 
         {
                  l = new Label("URL: ");
                  tf = new TextField(20);
                  p1 = new Panel();
                  p1.add(l);
                  p1.add(tf);
                  add(p1, BorderLayout.NORTH);
          
                  b = new Button("웹 브라우저 띄우기");
                  b.addActionListener(new PopUp());
  
                  p2 = new Panel();
                  p2.setBackground(Color.yellow);
                  p2.add(b);
                  add(p2, BorderLayout.CENTER);
         }
 
         public static void main(String[] args)
         {
                  new WebBrowserPopUp();
         }
 
         private class PopUp implements ActionListener
         {
                  @Override
                  public void actionPerformed(ActionEvent e) 
                  {
                           // 방법 1
                           if( Desktop.isDesktopSupported() ) 
                           { 
                                    try
                                    {
                                             String address = tf.getText();
                             
                                             if(address.length() > 0)
                                                      Desktop.getDesktop().browse(new URI(address));
                                    }
                                    catch(Exception ex)
                                    {
                                             ex.printStackTrace();
                                    }
                           } 

                        

                           /* 방법 2
                           try
                           {
                                    String address = tf.getText();
    
                                    if(address.length() > 0)
                                             Runtime.getRuntime().exec("C:/Program Files/Internet

                                                                                      Explorer/IEXPLORE.exe " + address);
                           }
                           catch(Exception ex)
                           {
                                    ex.printStackTrace();
                           }
                           */
                  }
         }
}

'IT_Programming > Java' 카테고리의 다른 글

[펌_ jdbc] ResultSet 크기 구하기   (0) 2009.04.08
[펌] Generics 튜토리얼  (0) 2009.03.24
[펌] JDBC 별 드라이버 사용법   (0) 2009.01.30
[펌] 제네릭스 해부, Part 2  (0) 2009.01.22
[펌] 제네릭스 해부, Part 1  (0) 2009.01.22