【问题标题】:JColorChoose, similar colors exceptionJColorChoose,相似颜色例外
【发布时间】:2015-08-20 12:40:46
【问题描述】:

我必须编写一个程序,其中 JColorChooser 被打开两次,这将允许用户选择背景和前景色。但是,如果用户选择的颜色过于相似,程序应该会抛出错误消息并再次打开 JColorChoosers...

我知道我必须使用 VetoableChangeListener 但我不知道如何在我的代码中实现它...

在我到目前为止的代码下方。

非常感谢您的帮助

   package test;

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

public class jcolortest{

   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public jcolortest(){
      prepareGUI();
   }

   public static void main(String[] args){
      jcolortest  swingControlDemo = new jcolortest();      
      swingControlDemo.showColorChooserDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("JColorChooser");
      mainFrame.setSize(400,200);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 



      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }




   private void showColorChooserDemo(){
      headerLabel.setText("Test foreground text");
      headerLabel.setForeground(Color.RED);

            Color backgroundColor = JColorChooser.showDialog(mainFrame,
               "Background Color", Color.white);
            if(backgroundColor != null){
               controlPanel.setBackground(backgroundColor);
               mainFrame.getContentPane().setBackground(backgroundColor);
            }

            Color foregroundColor = JColorChooser.showDialog(mainFrame,
               "Text Color", Color.white);
            if(foregroundColor != Color.RED){
               controlPanel.setForeground(foregroundColor);
               headerLabel.setForeground(foregroundColor);


         }

      mainFrame.setVisible(true);  
   }


}

【问题讨论】:

    标签: java swing jcolorchooser


    【解决方案1】:

    在这种情况下,我可能会使用类似的东西:

    Color foregroundColor;
    do{                           //loop as long as colors are similar
        foregroundColor = JColorChooser.showDialog(mainFrame,
        "Text Color", Color.white);
    }while (isSimilar(backgroundColor, foregroundColor));
    

    并添加数学:

    public boolean isSimilar(Color background, Color foreground){
        int r1 = background.getRed();
        int g1 = background.getGreen();
        int b1 = background.getBlue();
    
        int r2 = foreground.getRed();
        int g2 = foreground.getGreen();
        int b2 = foreground.getBlue();
    
        if((r2 >= r1+150 || r2 <= r1-150) || (g2 >= g1+150 || g2 <= g1-150) || (b2 >= b1+150 || b2 <= b1-150)){
            return false;             // 150 controls the "sensitivity"
    
        }
        JOptionPane.showMessageDialog(mainFrame,"Too similar color","Error",JOptionPane.INFORMATION_MESSAGE);
        return true;
    }
    

    没有什么花哨的,它相当原始,但在某种程度上它是有效的。问题是将“敏感度”设置在令人满意的水平。

    【讨论】:

    • 简单但非常有效,基本上解决了我的问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2011-12-12
    • 2010-11-13
    • 1970-01-01
    • 2021-05-04
    • 2014-11-14
    相关资源
    最近更新 更多