2

我想向矩形添加鼠标侦听器,以便当用户单击一个矩形并将鼠标拖动到下一个矩形时,会绘制一条线来跟踪用户的移动,并且用户所在的当前矩形必须更改颜色。我是java新手,因此您的帮助将不胜感激。

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class SundayPanel extends JPanel implements MouseListener{

 private Rectangle rec;
 private JPanel p = new JPanel();
 private Graphics graphic;
 public SundayPanel()
 {
  setSize(1000, 150);
  setBackground(Color.white);
  setBorder(BorderFactory.createEtchedBorder(Color.orange, Color.red));

  rec = new Rectangle(0, 0, 130, 100);
 }

 public void paint(Graphics g)
 {
  Font font = new Font("Times New Roman", Font.PLAIN, 72);

     g.setFont(font);
  g.setColor(Color.black);
  //g.draw(rec);
  g.draw3DRect(0, 0, 130, 100, true);
  g.drawString("1", 50, 65);
  g.draw3DRect(130, 0, 130, 100, true);
  g.drawString("2", 180, 65);
  g.draw3DRect(260, 0, 130, 100, true);
  g.drawString("3", 300, 65);
  g.draw3DRect(390, 0, 130, 100, true);
  g.drawString("4", 430, 65);
  g.draw3DRect(520, 0, 130, 100, true);
  g.drawString("5", 550, 65);
  g.draw3DRect(650, 0, 130, 100, true);
  g.drawString("6", 700, 65);
  g.draw3DRect(780, 0, 130, 100, true);
  g.drawString("7", 820, 65);
  g.draw3DRect(910, 0, 130, 100, true);
  g.drawString("8", 950, 65);
  g.draw3DRect(1040, 0, 130, 100, true);
  g.drawString("9", 1080, 65);
  g.draw3DRect(1170, 0, 130, 100, true);
  g.drawString("10", 1200, 65);
 }
}

import javax.swing.JFrame;

public class SundayMain extends JFrame{

 /**
  * @param args
  */
 public static void main(String[] args) {
  JFrame sunFrame = new SundayFrame("Sunday Today");
  sunFrame.setVisible(true);
  sunFrame.setSize(1330, 150);
  sunFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

}


import java.awt.BorderLayout;

import javax.swing.JFrame;

public class SundayFrame extends JFrame{

 private SundayPanel sunPanel;

 public SundayFrame(String title)
 {
  super(title);
  sunPanel = new SundayPanel();
  add(sunPanel, BorderLayout.CENTER);

 }

}
4

1 回答 1

1

我修改了您的示例以绘制线条(按下按钮、移动鼠标​​、释放按钮)。代码是可编译的,可以在一个SundayPanel.java文件中执行。可能它会帮助您实现您的确切目标:

注意:小心 - 代码非常难看。

public class SundayPanel extends JPanel implements MouseMotionListener, MouseListener {

    public SundayPanel() {
        setSize(1000, 150);
        setBackground(Color.white);
        setBorder(BorderFactory.createEtchedBorder(Color.orange, Color.red));
    }

    String[] values = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

    public void paint(Graphics g) {
        Font font = new Font("Times New Roman", Font.PLAIN, 72);
        g.setFont(font);
        for (int i = 0; i < 10; i++) {
            int step = 130 * i;
            if ((start != null) && ((int) (end.getX() / 130) == i)) {
                g.setColor(Color.orange);
            } else {
                g.setColor(Color.gray);
            }
            g.fill3DRect(step, 0, 130, 100, true);
            g.setColor(Color.black);
            g.drawString(values[i], 50 + step, 65);
        }
        if ((start != null) && (end != null)) {
            g.drawLine((int) start.getX(), (int) start.getY(), (int) end.getX(), (int) end.getY());
        }
    }

    Point start;

    Point end;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        start = e.getPoint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        end = e.getPoint();
        e.getComponent().repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int from = (int) (start.getX() / 130);
        int to = (int) (end.getX() / 130);
        String tmp = values[from];
        values[from] = values[to];
        values[to] = tmp;
        start = end = null;
        e.getComponent().repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}

class SundayMain extends JFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame sunFrame = new SundayFrame("Sunday Today");
        sunFrame.setVisible(true);
        sunFrame.setSize(1330, 150);
        sunFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

class SundayFrame extends JFrame {

    private SundayPanel sunPanel;

    public SundayFrame(String title) {
        super(title);
        sunPanel = new SundayPanel();
        sunPanel.addMouseListener(sunPanel);
        sunPanel.addMouseMotionListener(sunPanel);
        add(sunPanel, BorderLayout.CENTER);

    }

}
于 2013-02-19T16:01:52.073 回答