检测鼠标点击的位置

时间:2014-07-09 16:44:19

标签: java calendar

好的,所以我终于找到了!现在我只需要帮助了一件事。我需要检测我点击的位置,查看它是否在日历中,然后检测单击了哪个框。 如果你可以帮助我那将是惊人的!!!

这是我的代码!:

package drawing;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Drawing_something extends JPanel{
    //private JLabel statusBar;
    private static final long serialVersionUID = 1L;

    int[] calender_squares = {1, 2, 3, 4, 5, 6, 7};

    String[] Month = {"January", "February", "March", "April","May","June","July",
            "August","September","October","November","December"};

    String[] Days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    int g = 0, i = 0, fillRectx = 250, fillRecty = 150, fillRectWidth = 99, fillRectHeight = 99;

    int x = 0, y = 0;
    static int year = 2014;

    boolean currentDate = true, anotherDay = false, itsJune = true;

    String date = "";

    private int calculateBlankDays(int month){
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1);
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }

    private static int getDaysInMonth(int month){
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1);
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

//  private int calculateCurrentDay(int month){
//      Calendar cal = Calendar.getInstance();
//      cal.set(year, month, 10);
//      return cal.get(Calendar.DATE);
//  }
//  
    private int currentMonth(int month){
        Calendar cal = Calendar.getInstance();
        Calendar.getInstance().get(Calendar.MONTH);
        DateFormat dateFormat = new SimpleDateFormat("MMM");
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    private String getDateTime(){
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dateformat.format(date);
    }

    public void paintComponent(Graphics c){

        super.paintComponent(c);
        this.setBackground(Color.WHITE);
        Graphics2D g2d = (Graphics2D) c;
        Calendar C = Calendar.getInstance();
        int currentDay = C.get(Calendar.DAY_OF_YEAR);
        int blankDays = calculateBlankDays(i);
        int WIDTH = 55 + (blankDays * 100); 
        int HEIGHT = 65;
        int currentDayOfTheYear = C.get(Calendar.DAY_OF_YEAR);


        if (currentDate){
            i = currentMonth(i);
            currentDate = false;
        }

        // Drawing the squares on the canvas
        for (@SuppressWarnings("unused") int in: calender_squares) {
            for (int counter = 0; counter < 7; counter++){
                c.drawRect(50, 50, 25, 25);
                c.drawRect(50, 50, 700, 600);
                c.copyArea(50, 50, 600, 500, 100, 0);
                c.copyArea(50, 50, 700, 500, 0, 100);
            }
        }
        // Setting the transparency of the rectangle highlighting the day
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
        g2d.setColor(Color.CYAN);
        g2d.fillRect(currentDay + 161, currentDay - 39, 98, 98);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        c.setColor(Color.BLACK);

        if (anotherDay){
            fillRectx += 100;
            anotherDay = false;
            if (fillRectx > 650){
                fillRectx = 50;
                fillRecty += 100;
                anotherDay = false;
            }
        }

        // setting the correct days in the month
        for (int date = 1; date <= getDaysInMonth(i); date++){
            String s = String.valueOf(date);
            c.drawString(s, WIDTH, HEIGHT);

            if (WIDTH > 555){
                WIDTH = 55;
                HEIGHT += 100;
            } else {
                WIDTH += 100;
            }
        }
        if (g == 1){
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
            g2d.setColor(Color.YELLOW);
            g2d.fillRect(x, y, 100, 100);
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            c.setColor(Color.BLACK);
        }
        // Drawing and Redrawing every month when buttons are pressed
        if (i >= 12){
            i = 0;
            year = year + 1;
        } else if (i < 0){
            i = 11;
            year = year - 1;
        }

        c.setFont(new Font("default", Font.BOLD, 18));
        c.drawString("Sunday", 65, 45);
        c.drawString("Monday", 165, 45);
        c.drawString("Tuesday", 265, 45);
        c.drawString("Wednesday", 350, 45);
        c.drawString("Thursday", 465, 45);
        c.drawString("Friday", 565, 45);
        c.drawString("Saturday", 665, 45);

        c.setFont(new Font("default", Font.BOLD, 40));
        c.drawString(Month[i], 320, 28);
        repaint();

        c.setFont(new Font("default", Font.BOLD, 40));
        c.drawString(String.valueOf(year), 800, 45);


    }

    //defining the buttons to change the months
    public Drawing_something(){
        setBackground(Color.WHITE);
        setLayout(new BorderLayout());

        JButton N = new JButton("NEXT");

        JButton B = new JButton("BACK");

        JButton Test = new JButton("Test Button");

        JPanel P = new JPanel();

        JPanel L = new JPanel();
        //mouse clicking
        JPanel mousepanel = new JPanel();
        mousepanel.setBackground(new Color(0, 0, 0, 0));
        //statusBar = new JLabel("default");

        P.add(B);
        P.add(N);
        P.add(Test);

        add(P, BorderLayout.SOUTH);
        add(L, BorderLayout.EAST);
        //add(statusBar, BorderLayout.NORTH);
        add(mousepanel);

        B.addActionListener(new HandlerClass());
        N.addActionListener(new NextClass());
        Test.addActionListener(new TestClass());
        mousepanel.addMouseListener(new MouseClick());
    }

    // BACK BUTTON
    public class HandlerClass implements ActionListener{
        public void actionPerformed(ActionEvent e){
            i -= 1;
            itsJune = false;
        }
    }

    // NEXT BUTTON
    public class NextClass implements ActionListener{
        public void actionPerformed(ActionEvent e){
            i += 1;
            itsJune = false;
        }
    }

    // TEST BUTTON
    public class TestClass implements ActionListener{
        public void actionPerformed(ActionEvent e){
            anotherDay = true;
        }
    }

    //Mouse Click Listener
    public class MouseClick implements MouseListener{
        // Changing the statusBar(JLabel) to the destination of the click.
        public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
            x = e.getX();
            y = e.getY();
            //System.out.println("Your click is now known");
            g = 1;
        }

        public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

        }

        public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

        }

        public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

        }

        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    }
        // Opening the Frame
        public static void main(String[] args){
            JFrame mainFrame = new JFrame("Calender");
            mainFrame.add(new Drawing_something());
            mainFrame.setSize(950, 780);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setVisible(true);
        }
}

3 个答案:

答案 0 :(得分:0)

您似乎在顶级MousListener上有JPanel,其中包含许多按钮。由于点击的焦点将随鼠标移动,您是否会验证您的点击是否落在JPanel上而不是其中一个内部子组件上?

如果是这样,可能有一个按钮正在处理鼠标点击。

答案 1 :(得分:0)

在我看来,您从未将鼠标面板添加到主JPanel或任何其他JPanel。也许这样做:

add(mousepanel); // With some kind of BorderLayout

答案 2 :(得分:0)

MouseClick应扩展MouseAdapter并覆盖mouseClicked(MouseEvent e),而不是实施MouseListener并拥有大量未使用的方法。

无论如何,只需在构造函数中调用addMouseListener(new MouseClick());即可。您不需要MousePanel