2012-02-21 87 views
1

我需要使用底部欄中的某些按鈕創建框架。我的所有按鈕都顯示一個帶有顏色的圖像,例如黑色,灰色,白色等。我有一個我可以用我選擇的顏色繪製的面板。問題是,當我按下按鈕時,我不知道如何製作捕捉該顏色的方法。從我的JButton中獲取顏色

private JToolBar barreOutils; 

// 
private JToggleButton[] btnTab = new JToggleButton[9]; 

// 
private String[] btnName = { "Couleur noire", "Couleur grise", 
     "Couleur blanche", "Couleur rouge", "Couleur orange", 
     "Couleur jaune", "Couleur verte", "Couleur cyan", "Couleur bleue" }; 

// 
private String[] btnColor = { "dark.gif", "gray.gif", "white.gif", 
     "rouge.gif", "orange.gif", "yellow.gif", "vert.gif", "cyan.gif", 
     "blue.gif" }; 

String[] colorTab = { "Color.DARK", "Color.GRAY", "Color.WHITE", 
     "Color.RED", "Color.ORANGE", "Color.YELLOW", "Color.GREEN", 
     "Color.CYAN", "Color.BLUE" }; 

// buttonGroup 
private ButtonGroup groupeCouleurs; 
// Notre panneau principal 
private JPanel panneau; 

public Fenetre() { 

    // Organization 
    setTitle("Application"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(600, 600); 
    setLocationRelativeTo(null); 

    // Organization 
    panneau = new JPanel(); 
    panneau.setBackground(Color.white); 
    panneau.addMouseListener(new Dessiner()); 
    panneau.addMouseMotionListener(new Dessiner()); 
    getContentPane().add(panneau); 

    // 
    barreOutils = createToolbar(); 
    getContentPane().add(barreOutils, BorderLayout.SOUTH); 

} 

private JToolBar createToolbar() { 

    JLabel couleur = new JLabel("Couleurs : "); 
    barreOutils = new JToolBar(); 


    groupeCouleurs = new ButtonGroup(); 
    barreOutils.add(couleur); 


    createButton(btnTab, btnName, btnColor); 

    return barreOutils; 
} 

private void createButton(JToggleButton[] btnTab, String[] btnName, 
     String[] btnColor) { 
    // TODO Auto-generated method stub 

    // add the buttons on the bar at the bottom 
    for (int indBtn = 0; indBtn < btnTab.length; indBtn++) { 
     btnTab[indBtn] = new JToggleButton(new ImageIcon(
       Fenetre.class.getResource(btnColor[indBtn]))); 
     btnTab[indBtn].setToolTipText(btnName[indBtn]); 
     groupeCouleurs.add(btnTab[indBtn]); 
     barreOutils.add(btnTab[indBtn]); 
    } 
} 

private class Dessiner extends MouseAdapter { 

    public void mouseDragged(MouseEvent e) { 
     // TODO Auto-generated method stub 
     Graphics g = ((JComponent) e.getSource()).getGraphics(); 
     g.setColor(**???????????????????????**); 
     g.drawOval(e.getX(), e.getY(), 1, 1); 

    } 

回答

2

您可以擴展JToggleButton並讓該類包含所需的任何信息。

class ColorButton extends JToggleButton { 

    private Color color; 

    public ColorButton(Color c) { 
    super(); 
    this.color = c; 
    } 

    public Color getColor() { 
    return color; 
    } 
} 
+0

這也可以工作 - 1+。或者另一種解決方案是使用一個將JToggleButton的actionCommand String與Color相匹配的HashMap 。 – 2012-02-21 22:34:56

4

建議:

  • 將按鈕的actionCommand通過btnTab[indBtn].setActionCommand(btnName[indBtn]);
  • 的的ButtonGroup可以告訴你選中了哪個按鈕通過讓這將是一個ButtonModel的對象,如果選擇作出或無效的選擇如果沒有選擇存在。
  • 通過調用getActionCommand()方法從上面的ButtonModel中獲取所選按鈕的actionCommand字符串。
  • 考慮使用HashMap<String, Color>將actionCommand字符串與其關聯的顏色關聯起來。

而且

  • 請勿通過的getGraphics得到一個組件的Grahpics。而是在BufferedImage中繪製圖形,然後在JComponent(或擴展JComponent,如JPanel)的paintComponent方法的類中繪製BufferedImage。
  • 您可以通過調用getGraphics()來從BufferedImage中獲取Graphics對象,但只要確保在完成Graphics對象的刪除操作時就可以刪除它。
  • 在您的MouseListener中,通過更改類字段來更改對象的狀態,然後調用repaint。
0

我注意到你在按鈕圖像名稱中有顏色名稱,爲什麼不從那裏抓取它。

+0

因爲我試圖做一個動作事件來捕捉我單擊按鈕的顏色,但失敗了。我不知道 :( – MTHeadss 2012-02-21 22:46:06