日期:2014-05-20 浏览次数:20983 次
package test; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class ToolBarDemo extends JToolBar { private static final long serialVersionUID = 1L; public ToolBarDemo() { super(); this.setLayout(new FlowLayout(FlowLayout.LEFT)); loadButton(); } public void loadButton() { addButton("/images/open.gif", ""); } private void addButton(String url, String tip) { MyButton button = new MyButton(); button.setPreferredSize(new Dimension(30, 30)); button.setIcon(new ImageIcon(this.getClass().getResource(url))); button.setToolTipText(tip); this.add(button); } class MyButton extends JButton { private static final long serialVersionUID = 39082560987930759L; @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); int h = getHeight(); int w = getWidth(); if (getModel().isPressed()) { g2d.drawRect(6, 6, w - 12, h - 12); g2d.dispose(); } g2d.drawRect(0, 0, w - 1, h - 1); g2d.dispose(); super.paintComponent(g); } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.add(new ToolBarDemo(), BorderLayout.NORTH); setSkin(frame); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private static void setSkin(JFrame frame) { try { UIManager .setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel"); SwingUtilities.updateComponentTreeUI(frame); } catch (Exception ex) { System.err.println("皮肤设置有错误!"); return; } } }