gpt4 book ai didi

单击绘图区域时 Java 背景发生变化

转载 作者:行者123 更新时间:2023-12-01 13:42:59 24 4
gpt4 key购买 nike

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

按钮面板

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

最佳答案

只需在 DrawingArea 中的 actionPerformed() 方法中调用 repaint() 方法即可:

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

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

但是当您设置背景颜色时,您还需要强制重新绘制。

关于单击绘图区域时 Java 背景发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com