【问题标题】:Java background changes when click on draw area单击绘制区域时Java背景发生变化
【发布时间】:2013-12-31 11:39:46
【问题描述】:

在此之前,我在使用颜色选择器时更改笔的颜色和背景颜色时遇到问题。现在可以更改颜色笔,但背景不能更改颜色。它可以改变背景颜色,但我需要点击绘图​​区域然后背景会改变..当我们选择颜色时它应该改变背景颜色对吗?但它没有..

按钮面板

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

【问题讨论】:

    标签: java swing colors background click


    【解决方案1】:

    只需在DrawingAreaactionPerformed() 方法中调用repaint() 方法即可:

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

    因为当您更改画笔颜色并单击鼠标时,repaint() 方法被触发,

    但是当你设置背景颜色时,你也需要强制重绘。

    【讨论】:

    • 哦,我的工作..你解决了我的问题..Thankx..只是有点错误,错过了那里。非常感谢您的帮助。非常感谢
    • 其实他连“设置背景色”都没有;选择颜色后,他所做的只是在DrawingArea 中设置了一个名为colorBg 的变量,由于没有调用paintComponent(),因此没有效果。
    • hmm 我以为通过drawingArea.setColorBg(color) 它会设置bg的颜色..我真的没有注意到repaint是需要执行这个改变的功能的一部分..
    • @ajb 提到你只改变颜色(inctance 变量),但是当你在paintComponent() 方法中用新颜色重新填充面板时,它会改变面板的颜色。
    • 是的,当我在绘图区域上单击鼠标时背景才发生变化时,我变得很奇怪.. 我真的很好奇我不会与 mousePressed 相关.. 哦,是的,实际上存在;另一个问题正在发生在那个代码中它的关于保存按钮..我已经删除了上面的一些编码以避免某些情况..应该重新发布新问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2022-11-16
    • 1970-01-01
    • 2017-02-04
    • 2021-03-06
    • 2011-04-11
    • 2017-08-17
    相关资源
    最近更新 更多