java - 鼠标点击事件改变颜色

标签 java swing event-handling mouse mouseclick-event

好吧,这个程序的目标是创建一个靶心;当您单击面板时,它会将靶心的颜色从红色更改为蓝色。当你点击我的时,什么也没有发生。这是什么原因造成的?

import java.awt.*;

public class TargetLogoPanel extends javax.swing.JPanel {

public Color ringColor = Color.RED;
public int size = 400;
public TargetLogoPanel() {

    setBackground(Color.WHITE);
    setPreferredSize(new Dimension(700,400 ));
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth();
        int height = getHeight();

       int radius = getHeight();

     g.setColor(ringColor);
        g.fillOval(
                (int)((width/2 ) - (radius * 0.3)),
                (int)((height/2) - (radius * 0.3)), 
                (int)(radius * .6), 
                (int)(radius * .6));
        g.setColor(Color.WHITE);
        g.fillOval(
                (int)((width / 2) - (radius * 0.22)),
                (int)((height / 2) - (radius * 0.22)), 
                (int)(radius * 0.44), 
                (int)(radius * 0.44));
        g.setColor(ringColor);
        g.fillOval(
                (int)((width / 2) - (radius * 0.15)),
                (int)((height / 2) - (radius * 0.15)), 
                (int)(radius * 0.3), 
                (int)(radius * 0.3));
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            formMouseClicked(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>                        

private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
   if(ringColor == Color.RED){
       ringColor = Color.BLUE;
       repaint();
   } else{
       ringColor = Color.RED;
       repaint();
   }
}                                 


// Variables declaration - do not modify                     
// End of variables declaration                   
}

最佳答案

在面板的构造函数中调用 initComponents()

public TargetLogoPanel() {
    initComponents();
    setBackground(Color.WHITE);
    setPreferredSize(new Dimension(700, 400));
}

经测试:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;

/**
 * @see http://stackoverflow.com/a/22548923/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new TargetLogoPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    class TargetLogoPanel extends javax.swing.JPanel {

        public Color ringColor = Color.RED;
        public int size = 400;

        public TargetLogoPanel() {
            initComponents();
            setBackground(Color.WHITE);
            setPreferredSize(new Dimension(700, 400));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int width = getWidth();
            int height = getHeight();
            int radius = getHeight();
            g.setColor(ringColor);
            g.fillOval(
                (int) ((width / 2) - (radius * 0.3)),
                (int) ((height / 2) - (radius * 0.3)),
                (int) (radius * .6),
                (int) (radius * .6));
            g.setColor(Color.WHITE);
            g.fillOval(
                (int) ((width / 2) - (radius * 0.22)),
                (int) ((height / 2) - (radius * 0.22)),
                (int) (radius * 0.44),
                (int) (radius * 0.44));
            g.setColor(ringColor);
            g.fillOval(
                (int) ((width / 2) - (radius * 0.15)),
                (int) ((height / 2) - (radius * 0.15)),
                (int) (radius * 0.3),
                (int) (radius * 0.3));
        }

        /**
         * This method is called from within the constructor to initialize the
         * form. WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    formMouseClicked(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>                        

        private void formMouseClicked(java.awt.event.MouseEvent evt) {
            if (ringColor == Color.RED) {
                ringColor = Color.BLUE;
                repaint();
            } else {
                ringColor = Color.RED;
                repaint();
            }
        }

// Variables declaration - do not modify                     
// End of variables declaration                   
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

关于java - 鼠标点击事件改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22548839/

相关文章:

java - SwingWorker 得到一个结果

java - 创建一个带有今天日期标签的面板,根据今天的数据更改内容

jquery - Bootstrap 选项卡动态内容

C# 从列表框中拖放

c++ - Borland C++ Builder 5 - 通过 Escape 键取消不起作用

Java 8 Stream 修改可变变量

java - 它如何设置 "Fit all columns on one page"设置的文件

java - 管理动态添加到 JScrollPane 的 JCheckBox

java - 通过 JavaFX 过滤 JDK 错误系统

java - 如何设置JGoodies Look and Feel?