好的,所以我有我的主班,有一些按钮,一个用于三角形,另一个用于

椭圆形和盒子。

我有一个ColorChooser按钮,我想单击它,然后ColorChooser出现。我有

椭圆和三角形以及ColorChooser的类,我将它们各自设置为一种模式

在我的主程序中。

所以这是我的主程序,只有盒子和ColorChooser:

如您所见,即时消息对每个按钮使用模式,我为ColorChooser设置模式4

如果要我添加box类或ColorChooser类,如果没有,我会添加

感。我只是不希望它不再。

这是我的主要程序:

import java.awt.*;

import java.util.ArrayList;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.ChangeEvent;


public class Kaleidescope extends JFrame implements MouseListener, ActionListener,

MouseMotionListener

{

   Box b;

   ArrayList<Box> boxes; // list of boxes

   ColorChooser oo;

   ColorChooser[] colors;

   int colorCount;

   // Buttons

   JButton boxButton;

   JButton ColorButton;

   int x1, y1; // mousePressed

   int w1, z1; // mouseEntered

   int mode =1; // 1 = line, 2= boxes, 3 = oval, 4= text, 5 = SG, twoLines = 7.

   public static void main( String[] args )

   {
      System.out.println("hi there.");

      new Kaleidescope();
   }

   public Kaleidescope()

   {
      setDefaultCloseOperation( EXIT_ON_CLOSE );

      addMouseListener(this);

      addMouseMotionListener(this);

      boxes = new ArrayList<Box>();

      colors = new ColorChooser[20];

      colorCount = 0;

      setLayout( new FlowLayout() );

      boxButton = new JButton("Boxes");

      add(boxButton);

      boxButton.addActionListener( this );

      ColorButton = new JButton("Color Chooser");

      add(ColorButton);

      ColorButton.addActionListener( this);

      setSize( new Dimension(500,500) );

      setVisible(true);

   }

   // returns a random color

   public Color randomColor()

   {

      int red = (int)(Math.random()*255);

      int green = (int)(Math.random()*255);

      int blue = (int)(Math.random()*255);

      return new Color(red,green,blue);

   }

   public void mouseClicked( MouseEvent e )

   {

      // box

      if ( mode == 2)

      {

          boxes.add(new Box(e.getX(), e.getY(), randomColor()));
      }


      repaint();
   }

   //action performed

   public void actionPerformed( ActionEvent e )

   {

       if      ( e.getSource()==TriangleButton ) { mode  = 1;}

       else if ( e.getSource()==boxButton ) { mode = 2;}

       else if ( e.getSource()==ovalButton) { mode = 3;}

       else if ( e.getSource()==ColorButton) { mode = 4;}

       //clear all

        else if (e.getSource() == clearButton)

        {
            boxes.clear();
                triangles.clear();
                ovals.clear();

        }

      repaint();

   }
   public void mouseEntered( MouseEvent e ) { }

   public void mousePressed( MouseEvent e )  { }

   public void mouseExited( MouseEvent e ) { }

   public void mouseReleased( MouseEvent e ) {}

   public void mouseMoved( MouseEvent e ) {}

   public void mouseDragged( MouseEvent e ){ }
   }

   public void paint( Graphics g )

   {
      //draw/paint box triangle and oval

      super.paint(g);

      for (Box box : boxes)
      {
            box.drawMe(g);
      }
    }
}


这是我的colorChooser类:

    import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.colorchooser.*;

public class ColorChooser extends JPanel implements ChangeListener

{

    public static final long serialVersionUID = 1L;

    public JColorChooser tcc;

        public JLabel banner;

    public ColorChooser()

    {
        super(new BorderLayout());

        banner = new JLabel("",JLabel.CENTER);

        banner.setOpaque(true);

        banner.setPreferredSize(new Dimension(100, 65));

        JPanel bannerPanel = new JPanel(new BorderLayout());

        bannerPanel.add(banner, BorderLayout.CENTER);

        bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));

        //Set up color chooser for setting text color

        tcc = new JColorChooser(banner.getForeground());

        tcc.getSelectionModel().addChangeListener(this);

        tcc.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));

        add(bannerPanel, BorderLayout.CENTER);

        add(tcc, BorderLayout.PAGE_END);
    }

    public void stateChanged(ChangeEvent e)

    {
        Color newColor = tcc.getColor();

        banner.setForeground(newColor);
    }


    private static void createAndShowGUI()

    {
        //Create and set up the window.

        JFrame frame = new JFrame("ColorChooserDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.

        JComponent newContentPane = new ColorChooser();

        newContentPane.setOpaque(true); //content panes must be opaque

        frame.setContentPane(newContentPane);

        //Display the window.

        frame.pack();

        frame.setVisible(true);
    }

    public static void main(String[] args)

    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()

        {
            public void run()

            {
                createAndShowGUI();
            }
        });
    }


}

最佳答案

首先看一下How to Write an Action ListenerHow to Use Color Choosers

基本上,将ActionListener附加到要激活JButtonJColorChooser上,并在调用actionPerformed方法时,使用内置功能来显示默认选择器窗口,例如(来自链接的教程)。 。

Color newColor = JColorChooser.showDialog(
                    ColorChooserDemo2.this,
                    "Choose Background Color",
                    banner.getBackground());


更新

首先在Color中添加一个Kaleidescope实例变量,这将允许您维护对最后选择的颜色的引用

private Color currentPaintColor = Color.BLACK;


接下来,当按下ColorButton时,您将要创建某种对话框以显示选择器,这将使您可以等到用户选择一种颜色并获得最终的颜色...

} else if (e.getSource() == ColorButton) {
    ColorChooser chooser = new ColorChooser();
    int result = JOptionPane.showConfirmDialog(this, chooser, "Color Chooser", JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        currentPaintColor = chooser.getChoosenColor();
    }
} //clear all


您还需要更改stateChanged中的ColorChooser方法,使其更有意义...

public void stateChanged(ChangeEvent e) {
    Color newColor = tcc.getColor();

    banner.setBackground(newColor);
}


现在,问题是,您想用这种新颜色做什么?您想将其应用于您正在绘制的所有当前形状吗?如果是这样,则需要在绘制形状之前设置颜色...

public void paint(Graphics g) {
    //draw/paint box triangle and oval
    super.paint(g);
    g.setColor(currentPaintColor);
    for (Box box : boxes)
    {
        box.drawMe(g);
    }
}


还是只将颜色应用于更改后添加的新对象?

您应该避免覆盖顶级容器的paint,这是有很多原因的,它们不是双重缓冲的,这将在更新它们时引起闪烁,并且您将在框架上所有其他物体的顶部进行绘制,并且有可能绘制在框架边框下...

相反,您应该使用JPanel并覆盖它的paintComponent方法,有关更多详细信息,请参见Performing Custom Painting

10-01 17:58