使用Java圆角Swing JButton
好吧,我有一个图像,我想把它作为一个按钮的背景(或一些可引用的东西)。 问题是这个图像是圆的,所以我需要显示这个图像,没有任何边框等。
保存此按钮的JComponent具有自定义背景,因此按钮确实只需要显示图像。
搜索谷歌后,我无法做到这一点。 我尝试了以下所有,但没有运气:
button.setBorderPainted(false); button.setContentAreaFilled(false); button.setOpaque(true);
在我在背景上绘制图标后,按钮会绘制它,但是会保留带边框的丑陋灰色背景等。我还尝试使用JLabel和JButton。 并在其上绘制ImageIcon,但如果用户调整窗口大小或最小化窗口,图标就会消失!
我怎样才能解决这个问题?
我只需要将图像绘制并将其舍入到JComponent并听取它的点击次数……
创建一个新的Jbutton:
JButton addBtn = new JButton("+"); addBtn.setBounds(x_pos, y_pos, 30, 25); addBtn.setBorder(new RoundedBorder(10)); //10 is the radius addBtn.setForeground(Color.BLUE);
在设置JButton的边框时,调用重写的javax.swing.border.Border
类。
addBtn.setBorder(new RoundedBorder(10));
这是class级
private static class RoundedBorder implements Border { private int radius; RoundedBorder(int radius) { this.radius = radius; } public Insets getBorderInsets(Component c) { return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius); } public boolean isBorderOpaque() { return true; } public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.drawRoundRect(x, y, width-1, height-1, radius, radius); } }
你试过以下吗?
button.setOpaque(false); button.setFocusPainted(false); button.setBorderPainted(false); button.setContentAreaFilled(false); setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important
setBorder(null)
可能有效,但是在Sun中描述了一个错误,它解释说,除非客户端设置一个不实现UIResource
接口的非空边框,否则UI会在组件上设置边框。
当传入null时,JDK本身不将边框设置为EmptyBorder
客户端应该自己设置EmptyBorder
(一种非常简单的解决方法)。 这样就不会混淆谁在代码中做了什么。
我建议覆盖paint(Graphics g)方法,如下所示:
class JImageButton extends JComponent implements MouseListener { private BufferedImage img = null; public JImageButton(BufferedImage img) { this.img = img; setMinimumSize(new Dimension(img.getWidth(), img.getHeight())); setOpaque(false); addMouseListener(this); } public void paintComponent(Graphics g) { g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } }
-
将普通按钮拖到面板上
-
右键单击您的按钮,然后转到属性:
boarder = no barder boarder painted = false contentAreaFilled = false focusPainted = false opaque = false
-
通过导入到项目设置(图标)和(rolloverIcon)。
您可以尝试以下方法。 它对我来说很好,我也遇到了与按钮相同的问题。
// Jbutton JButton imageButton = new JButton(); // Buffered Icon BufferedImage buttonIcon = null; try { // Get the image and set it to the imageicon buttonIcon = ImageIO.read(getClass().getClassLoader().getResource("images/login.png")); } catch(Exception ex) { } // Set the image icon here imageButton = new JButton(new ImageIcon(buttonIcon)); imageButton.setBorderPainted(false); imageButton.setContentAreaFilled(false); imageButton.setFocusPainted(false); imageButton.setOpaque(false);
不透明度应该设置为false,所以
button.setOpaque(false);
可能已经是你想要的了。
您可以为按钮创建一个空边框,如下所示:
button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));