2013-03-12 66 views
1

我正在製作遊戲Master Mind,我用JButton填充了我的矩陣,以便人們可以點擊它們來更改顏色。更改JButton的形狀

現在我想將矩形按鈕的形狀更改爲圓形,是否有一種方法可以一次將它們全部更改,因爲我使用循環來創建所有這些按鈕。

回答

2

您可以在Google上搜索關於此的教程。

這是一個簡單的教程:How to change the shape of a JButton

+0

要注意的是[只有鏈路答案往往被刪除(http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-在其他地方,真的好,答案) – 2015-07-15 07:56:18

5

這裏有一些方法必須被重寫到編輯組件的形狀。 (含樣品代碼)

protected void paintComponent(Graphics g) 
    { 
    if (getModel().isArmed()) { 
     g.setColor(Color.lightGray); 
    } else { 
     g.setColor(getBackground()); 
    } 
    g.fillOval(0, 0, getSize().width-1,getSize().height-1); 

    super.paintComponent(g); 
    } 

    protected void paintBorder(Graphics g) { 
    g.setColor(getForeground()); 
    g.drawOval(0, 0, getSize().width-1,  getSize().height-1); 
    } 

    Shape shape; 
    public boolean contains(int x, int y) { 
    if (shape == null || 
     !shape.getBounds().equals(getBounds())) { 
     shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
    } 
    return shape.contains(x, y); 
    }