日期:2014-05-20 浏览次数:21057 次
import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Insets; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.JTextPane; import javax.swing.border.AbstractBorder; public class JQQTextPane extends JTextPane { public JQQTextPane() { super(); initialize(); } private void initialize() { ImageButton button = new ImageButton(); button.setIcon(new ImageIcon("src/snow.png")); setText("12345"); insertComponent(button); } class ImageButton extends JButton { private JPopupMenu popupMenu; public ImageButton() { initialize(); } private void initialize() { setBorder(new ImageBorder()); setContentAreaFilled(false); popupMenu = new JPopupMenu(); popupMenu.add(new JMenuItem("另存为")); popupMenu.add(new JMenuItem("等等")); addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { popupMenu.show(ImageButton.this, e.getX(), e.getY()); } } }); } } class ImageBorder extends AbstractBorder { @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Color oldColor = g.getColor(); g.setColor(Color.black); if (c.hasFocus()) { g.drawRect(x, y, width - 1, height - 1); } g.setColor(oldColor); } @Override public Insets getBorderInsets(Component c) { return new Insets(1, 1, 1, 1); } @Override public Insets getBorderInsets(Component c, Insets insets) { insets.top = 1; insets.left = 1; insets.bottom = 1; insets.right = 1; return insets; } } public static void main(String... args) { JFrame frame = new JFrame(); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new JQQTextPane()); frame.setVisible(true); } }
------解决方案--------------------
帮顶。