2009-01-08 153 views
13

那麼,我有一個圖像,我想把它作爲一個按鈕的背景(或某些東西clicable)。問題是這個圖像是圓的,所以我需要顯示這個圖像,沒有任何邊框等。使用Java的圓角搖擺JButton

持有此按鈕的JComponent具有自定義背景,所以按鈕確實只需要顯示圖像。

在搜索Google後,我無法做到這一點。我已經嘗試了所有的追隨者,但是沒有運氣:

button.setBorderPainted(false); 
button.setContentAreaFilled(false); 
button.setOpaque(true); 

後,我畫的圖標背景,按鈕油漆,但持有醜陋的灰色背景,邊框,等我也試圖使用JLabel和JButton。並在其上繪製ImageIcon,但如果用戶調整或最小化窗口,圖標消失!

我該如何解決這個問題?

我只是需要油漆和圓形圖像以一個JComponent,聽在它的點擊...

+0

您可以查看先前回答類似的問題的線索: http://stackoverflow.com/questions/5120116/how在java中創建四捨五入的jbutton/31699716#31699716 – GeekOnJava 2015-07-29 11:57:03

回答

0

透明度應設置爲false,那麼

button.setOpaque(false); 

可能已經是你想要的。

11

你試過以下嗎?

button.setOpaque(false); 
button.setFocusPainted(false); 
button.setBorderPainted(false); 
button.setContentAreaFilled(false); 
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important 

setBorder(null)可能會奏效,但a bug described at Sun解釋它是通過設計的UI設置一個組件上的邊界,除非客戶端將不執行UIResource接口一個非空邊界。

當傳入null時,並非JDK自己將邊框設置爲EmptyBorder,客戶端應該自己設置EmptyBorder(一種非常簡單的解決方法)。這樣就不會有誰在代碼中做什麼的困惑。

+0

您可以查看以前類似問題的答案: http://stackoverflow.com/questions/5120116/how-to-create-round-jbutton -in-java/31699716#31699716 – GeekOnJava 2015-07-29 11:56:49

+0

@GeekOnJava看起來不錯。 +1 – VonC 2015-07-29 15:28:19

1

我會建議壓倒一切的油漆(圖形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) { 
    } 
} 
13

創建一個新的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)); 

這裏是類

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); 
    } 
} 
+0

這幾乎是正確的答案 – UVM 2012-03-28 06:11:02

1
  1. 將一個普通按鈕到面板

  2. 右鍵單擊按鈕,進入屬性:

    boarder   = no barder 
    boarder painted = false 
    contentAreaFilled = false 
    focusPainted  = false 
    opaque   = false 
    
  3. 設置一個(圖標)和一個(rolloverIcon)通過導入到項目。

1

您可以嘗試以下操作。它對我來說工作得很好,而且我也遇到了與按鈕相同的問題。

// 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); 
0

您可以創建一個空的邊框按鈕這樣的:

button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));