更改图像背景颜色

时间:2014-03-19 22:45:33

标签: java swing colors background jcomponent

如何将黑色背景图像的颜色更改为选择的颜色?

JComponent colorBar = new ColorComponent(Color.BLACK);
colorBar.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            Color defaultColor = getBackground();
            Color selected = JColorChooser.showDialog(DessinFrame.this,
                    "Set background", defaultColor);
            if (selected != null) setBackground(selected);
        }
    });

1 个答案:

答案 0 :(得分:0)

关闭。看起来你的get / setBackground(选中)应该引用colorBar组件

JComponent colorBar = new ColorComponent(Color.BLACK);
colorBar.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            Color defaultColor = colorBar.getBackground();
            Color selected = JColorChooser.showDialog(DessinFrame.this, "Set background", defaultColor);
            if (selected != null) colorBar.setBackground(selected);
        }
    });