public class JavaWebBrowser{
protected static final String LS = System.getProperty("line.separator");
public static JComponent createContent() {
JPanel contentPane = new JPanel(new BorderLayout());
//初始化标签栏
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
//初始化浏览器
JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setMenuBarVisible(false);
webBrowser.navigate("www.baidu.com");
//为浏览器设置监听事件
addWebBrowserListener(tabbedPane, webBrowser);
tabbedPane.addTab("new", webBrowser);
//设置标题
setPanelTitle(tabbedPane, webBrowser, "百度一下,你就知道");
//把控件添加进JPanel
contentPane.add(tabbedPane, BorderLayout.CENTER);
return contentPane;
}
/**
* 设置标题
* @param tabbedPane
* @param webBrowser
* @param title
*/
private static void setPanelTitle(final JTabbedPane tabbedPane, final JWebBrowser webBrowser, String title) {
//新建一个面板
JPanel jpPanel = new JPanel();
jpPanel.setOpaque(false);
//添加一个文本
JLabel jLabel = new JLabel(title);
//添加一个按钮
TabButton jButton = new TabButton();
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int closeNumberTab = tabbedPane.indexOfComponent(webBrowser);
tabbedPane.removeTabAt(closeNumberTab);
}
});
jpPanel.add(jLabel, BorderLayout.WEST);
jpPanel.add(jButton, BorderLayout.EAST);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, jpPanel);
}
/**
* 设置新窗口
*
* @param tabbedPane
* @param webBrowser
*/
private static void addWebBrowserListener(final JTabbedPane tabbedPane, final JWebBrowser webBrowser) {
webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
@Override
public void titleChanged(WebBrowserEvent e) { //改变标题
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
if (tabbedPane.getComponentAt(i) == webBrowser) {
if (i == 0) {
return;
}
String title = webBrowser.getPageTitle();
if (title.length() > 15)
title = title.substring(0, 15);
setPanelTitle(tabbedPane, webBrowser, title);
break;
}
}
}
@Override
public void windowWillOpen(WebBrowserWindowWillOpenEvent e) { //将要打开新窗口
JWebBrowser newWebBrowser = new JWebBrowser();
addWebBrowserListener(tabbedPane, newWebBrowser);
tabbedPane.addTab("", newWebBrowser);
e.setNewWebBrowser(newWebBrowser);
}
@Override
public void windowOpening(WebBrowserWindowOpeningEvent e) { //新窗口打开中
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
e.getWebBrowser().setMenuBarVisible(false);
}
});
}
public static void main(String[] args) {
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.setIconImage(
Toolkit.getDefaultToolkit().getImage(JavaBrowser.class.getResource("/Img/ic_launcher.png")));
frame.setTitle("自定义浏览器");
// double width =
// Toolkit.getDefaultToolkit().getScreenSize().getWidth();
// double height =
// Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setSize(900, 800);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// frame.setAlwaysOnTop(true); //设置永远置于最上层
// frame.setLocationRelativeTo(null);// 居中显示
// frame.setLocation(0, 0);
// frame.setLocationByPlatform(true);
// frame.setResizable(false); //禁用最大化按钮
frame.getContentPane().add(createContent(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, "确定要退出吗?", "先森科技", JOptionPane.OK_CANCEL_OPTION) == 0) {
System.exit(0);
}
return;
}
});
}
});
NativeInterface.runEventPump();
}
}
以上是界面的开发效果,下面的是自定义的BUTTON作用是关闭当前的界面
class TabButton extends JButton {
private static final long serialVersionUID = 1L;
public TabButton() {
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("关闭");
setUI(new BasicButtonUI());
setContentAreaFilled(false);
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
//翻转效果
setRolloverEnabled(true);
//鼠标事件,进入时画边框,移出时取消边框
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
});
}
@Override
public void updateUI() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
//鼠标按下时偏移一个坐标点
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLACK);
//鼠标在按钮上时为红色
if (getModel().isRollover()) {
g2.setColor(Color.RED);
}
int delta = 5;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
g2.dispose();
}
}
以上就是所有代码,其中自定义Button使用的网络上面的代码。。。