2013-12-12 44 views
0

在使用顏色選擇器時,我有問題改變筆和背景顏色的顏色..現在顏色筆可以更改,但背景無法更改顏色。它可以改變背景顏色,但我需要點擊繪製區域,然後背景會改變..當我們選擇我們的顏色時,它應該改變背景顏色嗎?但它沒有..當點擊繪製區域時Java背景發生變化

ButtonPanel

import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.JColorChooser; 
import java.awt.Color; 

public class ButtonPanel extends JPanel implements ItemListener, 
    ActionListener 
{ 
    private DrawingArea drawingArea; 

     private String tools[] = {"Pencil", "Line", "Circle", "Rectangle", "Filled Circle", "Filled Rectangle", "Round Rectangle", "Filled Round Rectangle"}; 

     private Color color = (Color.WHITE); 
     private JComboBox<String> jcbTool; 
    private JButton btnClear; 
     private JButton save; 
     private JButton infobutton; 
     private JButton colorBtn; 
     private JButton colorBg; 



    public void itemStateChanged(ItemEvent ie) 
    { 
       if (ie.getSource()==jcbTool) 

      {  

       String tool = (String)jcbTool.getSelectedItem(); 
       drawingArea.setTool(tool); 

     } 
      // else 
       // if (ie.getSource()==eraser) 
//{    String tool = (String)eraser.getSelectedItem(); 
//    drawingArea.setTool(tool) 
     } 

    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getSource()==btnClear) 
      drawingArea.clear();  
       else if (e.getSource()==infobutton) 
       { 
      //default title and icon 
       JOptionPane.showMessageDialog(this,"Paint java created by bla bla bla bla bla blaa"); 
       } 
       else if (e.getSource()==colorBtn) 
       { 
        color = JColorChooser.showDialog(null,"LOL",color); 
        drawingArea.setColorBtn(color); 
       } 
        else if (e.getSource()==colorBg) 
        { 
        color = JColorChooser.showDialog(null,"LOL",color); 
        drawingArea.setColorBg(color); 
        } 
     } 
} 

回答

1

只需撥打repaint()方法在actionPerformed()方法DrawingArea

if (e.getSource() == colorBg) { 
    color = JColorChooser.showDialog(null, "LOL", color); 
    drawingArea.setColorBg(color); 
    drawingArea.repaint(); 
} 

,因爲當你改變畫筆顏色,然後單擊鼠標repaint()法進行燒結,

但是當你設置背景色時,你也需要強制重畫。

+0

哦,我的工作.. U解決了我的問題.. Thankx ..只是有點失誤,錯過了那裏。非常感謝您的幫助。非常感謝 – parkway

+0

其實,他甚至沒有「設置背景顏色」;在選擇顏色後所做的所有事情是在'DrawingArea'中設置一個名爲'colorBg'的變量,由於沒有調用'paintComponent()',所以沒有效果。 – ajb

+0

嗯我認爲通過drawingArea.setColorBg(顏色),它將設置bg的顏色..我真的沒有注意到重繪是執行此更改所需的功能的一部分.. – parkway