效果
类似eclipse工具栏的效果,无悬浮,按钮无边框,悬浮时候呈现边框
实现
this.setOpaque(false); this.setBorder(emptyBorder); this.setContentAreaFilled(false); this.setFocusPainted(false); this.setRolloverEnabled(true); this.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { if (isRolloverEnabled()) { setBorder(roverBorder); } } @Override public void mouseExited(MouseEvent e) { if (isRolloverEnabled()) { setBorder(emptyBorder); } } });
private Border emptyBorder = BorderFactory.createEmptyBorder(0,0, 0, 0);
private Color roverBorderColor = Color.gray; private Border roverBorder = new Border() { public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.setColor(roverBorderColor); g.drawRect(x, y, width - 1, height - 1); } public Insets getBorderInsets(Component c) { return new Insets(1, 1, 1, 1); } public boolean isBorderOpaque() { return true; } };
最终效果:
keyword: Swing,JButton,Border