다음으로 전송:http://blog.csdn.net/youyigong/article/details/7081926
package com.waysame;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.jdic.browser.WebBrowserEvent;
import org.jdesktop.jdic.browser.WebBrowserListener;
public class WaySameBrowser extends JPanel{
public static ImageIcon browseIcon = new ImageIcon(
WaySameBrowser.class.getResource("images/Right.gif"));
BorderLayout borderLayout1 = new BorderLayout();
JToolBar jBrowserToolBar = new JToolBar();
JButton jStopButton = new JButton(" ",
new ImageIcon(getClass().getResource("images/Stop.png")));
JButton jRefreshButton = new JButton(" ",
new ImageIcon(getClass().getResource("images/Reload.png")));
JButton jForwardButton = new JButton(" ",
new ImageIcon(getClass().getResource("images/Forward.gif")));
JButton jBackButton = new JButton(" ",
new ImageIcon(getClass().getResource("images/Back.gif")));
JPanel jAddressPanel = new JPanel();
JLabel jAddressLabel = new JLabel();
JTextField jAddressTextField = new JTextField();
JButton jGoButton = new JButton();
JPanel jAddrToolBarPanel = new JPanel();
MyStatusBar statusBar = new MyStatusBar();
JPanel jBrowserPanel = new JPanel();
WebBrowser webBrowser;
public WaySameBrowser() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() {
this.setLayout(borderLayout1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setPreferredSize(new Dimension(screenSize.width * 9 / 10,
screenSize.height * 8 / 10));
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
jAddressLabel.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
jAddressLabel.setToolTipText("");
jAddressLabel.setText(" : ");
jGoButton.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(0,
2, 0, 2), new EtchedBorder()));
jGoButton.setMaximumSize(new Dimension(60, 25));
jGoButton.setMinimumSize(new Dimension(60, 25));
jGoButton.setPreferredSize(new Dimension(60, 25));
jGoButton.setToolTipText("Load the given URL");
jGoButton.setIcon(browseIcon);
jGoButton.setText("GO");
jGoButton.addActionListener(new Browser_jGoButton_actionAdapter(this));
jAddressPanel.setLayout(new BorderLayout());
jAddressTextField.addActionListener(new Browser_jAddressTextField_actionAdapter(this));
jBackButton.setToolTipText("Go back one page");
jBackButton.setHorizontalTextPosition(SwingConstants.TRAILING);
jBackButton.setEnabled(false);
jBackButton.setMaximumSize(new Dimension(75, 27));
jBackButton.setPreferredSize(new Dimension(75, 27));
jBackButton.addActionListener(new Browser_jBackButton_actionAdapter(this));
jForwardButton.setToolTipText("Go forward one page");
jForwardButton.setHorizontalTextPosition(SwingConstants.TRAILING);
jForwardButton.setEnabled(false);
jForwardButton.addActionListener(new Browser_jForwardButton_actionAdapter(this));
jRefreshButton.setToolTipText("Reload current page");
jRefreshButton.setEnabled(true);
jRefreshButton.setMaximumSize(new Dimension(75, 27));
jRefreshButton.setMinimumSize(new Dimension(75, 27));
jRefreshButton.setPreferredSize(new Dimension(75, 27));
jRefreshButton.addActionListener(new Browser_jRefreshButton_actionAdapter(this));
jStopButton.setToolTipText("Stop loading this page");
jStopButton.setVerifyInputWhenFocusTarget(true);
jStopButton.setText(" ");
jStopButton.setEnabled(true);
jStopButton.setMaximumSize(new Dimension(75, 27));
jStopButton.setMinimumSize(new Dimension(75, 27));
jStopButton.setPreferredSize(new Dimension(75, 27));
jStopButton.addActionListener(new Browser_jStopButton_actionAdapter(this));
jAddressPanel.add(jAddressLabel, BorderLayout.WEST);
jAddressPanel.add(jAddressTextField, BorderLayout.CENTER);
jAddressPanel.add(jGoButton, BorderLayout.EAST);
jAddressPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEtchedBorder(),
BorderFactory.createEmptyBorder(2, 0, 2, 0)));
jBrowserToolBar.setFloatable(false);
jBrowserToolBar.add(jBackButton, null);
jBrowserToolBar.add(jForwardButton, null);
jBrowserToolBar.addSeparator();
jBrowserToolBar.add(jRefreshButton, null);
jBrowserToolBar.add(jStopButton, null);
jBrowserToolBar.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEtchedBorder(),
BorderFactory.createEmptyBorder(2, 2, 2, 0)));
jAddrToolBarPanel.setLayout(new BorderLayout());
jAddrToolBarPanel.add(jAddressPanel, BorderLayout.CENTER);
jAddrToolBarPanel.add(jBrowserToolBar, BorderLayout.WEST);
jAddrToolBarPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
statusBar.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
statusBar.lblDesc.setText(" - ");
try {
webBrowser = new WebBrowser(new URL("http://www.baidu.com"));
// Print out debug messages in the command line.
//webBrowser.setDebug(true);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
return;
}
webBrowser.addWebBrowserListener(new WebBrowserListener(){
//
public void documentCompleted(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
public void downloadCompleted(WebBrowserEvent arg0) {
jBackButton.setEnabled(webBrowser.isBackEnabled());
jForwardButton.setEnabled(webBrowser.isForwardEnabled());
updateStatusInfo(" .");
URL currentURL = webBrowser.getURL();
if(currentURL != null){
jAddressTextField.setText(currentURL.toString());
}
}
public void downloadError(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
public void downloadProgress(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
//
public void downloadStarted(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
public void initializationCompleted(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
public void statusTextChange(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
public void titleChange(WebBrowserEvent arg0) {
updateStatusInfo(" .");
}
});
jBrowserPanel.setLayout(new BorderLayout());
jBrowserPanel.add(webBrowser, BorderLayout.CENTER);
this.add(jAddrToolBarPanel, BorderLayout.NORTH);
this.add(statusBar, BorderLayout.SOUTH);
this.add(jBrowserPanel, BorderLayout.CENTER);
}
void loadURL() {
String inputValue = jAddressTextField.getText();
if (inputValue == null) {
JOptionPane.showMessageDialog(this, "The given URL is NULL:",
"Warning", JOptionPane.WARNING_MESSAGE);
} else {
// Check if the text value is a URL string.
URL curUrl = null;
try {
// Check if the input string is a local path by checking if it starts
// with a driver name(on Windows) or root path(on Unix).
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
if (inputValue.toLowerCase().startsWith(roots[i].toString().toLowerCase())) {
File curLocalFile = new File(inputValue);
curUrl = curLocalFile.toURL();
break;
}
}
if (curUrl == null) {
// Check if the text value is a valid URL.
try {
curUrl = new URL(inputValue);
} catch (MalformedURLException e) {
if (inputValue.toLowerCase().startsWith("ftp.")) {
curUrl = new URL("ftp://" + inputValue);
} else if (inputValue.toLowerCase().startsWith("gopher.")) {
curUrl = new URL("gopher://" + inputValue);
} else {
curUrl = new URL("http://" + inputValue);
}
}
}
webBrowser.setURL(curUrl);
// Update the address text field, statusbar, and toolbar info.
updateStatusInfo("Loading " + curUrl.toString() + " ......");
} catch (MalformedURLException mue) {
JOptionPane.showMessageDialog(this,
"The given URL is not valid:" + inputValue, "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
}
void jGoButton_actionPerformed(ActionEvent e) {
loadURL();
}
void jAddressTextField_actionPerformed(ActionEvent e) {
loadURL();
}
void jBackButton_actionPerformed(ActionEvent e) {
webBrowser.back();
}
void jForwardButton_actionPerformed(ActionEvent e) {
webBrowser.forward();
}
void jRefreshButton_actionPerformed(ActionEvent e) {
webBrowser.refresh();
}
void jStopButton_actionPerformed(ActionEvent e) {
webBrowser.stop();
}
public static void main(String[] args) {
JFrame frame = new JFrame(" - ");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
contentPane.add(new WaySameBrowser());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
void updateStatusInfo(String statusMessage) {
statusBar.lblStatus.setText(statusMessage);
}
}
class Browser_jAddressTextField_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jAddressTextField_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jAddressTextField_actionPerformed(e);
}
}
class Browser_jBackButton_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jBackButton_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jBackButton_actionPerformed(e);
}
}
class Browser_jForwardButton_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jForwardButton_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jForwardButton_actionPerformed(e);
}
}
class Browser_jRefreshButton_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jRefreshButton_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRefreshButton_actionPerformed(e);
}
}
class Browser_jStopButton_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jStopButton_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jStopButton_actionPerformed(e);
}
}
class Browser_jGoButton_actionAdapter implements java.awt.event.ActionListener {
WaySameBrowser adaptee;
Browser_jGoButton_actionAdapter(WaySameBrowser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jGoButton_actionPerformed(e);
}
}
package com.waysame;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class MyStatusBar extends Box{
public JLabel lblStatus;
public JLabel lblDesc;
public MyStatusBar(){
super(BoxLayout.X_AXIS);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
lblStatus = new JLabel(" :", SwingConstants.LEADING);
lblStatus.setPreferredSize(new Dimension((int) (0.7 * screenSize.width),22));
lblStatus.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(lblStatus, null);
lblDesc = new JLabel(" :", SwingConstants.LEADING);
lblDesc.setPreferredSize(new Dimension((int) (0.3 * screenSize.width),22));
lblDesc.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(lblDesc, null);
}
}