如何根据光标位置更改框架中的文本? [英] How do you make text in a frame change dependent on cursor position?

查看:57
本文介绍了如何根据光标位置更改框架中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序基本上应该用于在光标进入JPanel上显示的多边形时更改标签的文本.我已经尝试了几种不同的方法而无济于事.目前,我正在尝试使用if语句来使其选择要添加的按钮,但是如果将光标移到多边形中,它仍然不会改变.当光标在多边形之外时,标签应显示"point is not in多边形",而在光标内部时应显示"point is in多边形".任何帮助将不胜感激.

I have a program that is basically just supposed to change the text of a label when your cursor enters a polygon that is shown on the JPanel. I have tried a few different things with nothing working. Currently I am trying an if statement to make it choose which button to add but it still doesn't change if i move my cursor into the polygon. when the cursor is outside of the polygon the label should say "point is not in the polygon" and when inside it should say "point is in the polygon". Any help would be greatly appreciated.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Chapter3Lab1  extends JFrame{

private RegularPolygonPanel canvas = new RegularPolygonPanel();

public Chapter3Lab1()
{

    JPanel panel = new JPanel();


    add(canvas, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);


    canvas.setFocusable(true);
    canvas.requestFocusInWindow();



}


public static void main (String[] args)
{

     String isInside = "The point is in the polygon";
     String notInside = "The point is not in the polygon";


     //Create a Polygon object
     Polygon polygon = new Polygon();

    polygon.addPoint(40,20);
    polygon.addPoint(70,40);
    polygon.addPoint(60,80);
    polygon.addPoint(45,45);
    polygon.addPoint(20,60);


    JLabel label = new JLabel(isInside, JLabel.CENTER);
    JLabel notlabel = new JLabel(notInside, JLabel.CENTER);

    Chapter3Lab1 frame = new Chapter3Lab1();;
    frame.setTitle("Chapter 3 Lab 1");
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);// Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    while (true)
    {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        int x = (int) b.getX();
        int y = (int) b.getY();
        if(polygon.contains(x, y) == false)
        {
        frame.remove(label);
        frame.add(notlabel);
        }
        else 
        {
        frame.remove(notlabel);
        frame.add(label);
        }

        frame.setVisible(true);
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }



}

class RegularPolygonPanel extends JPanel
{




     protected void paintComponent(Graphics g)
     {
         super.paintComponent(g);

         //Create a Polygon object
         Polygon polygon = new Polygon();

        polygon.addPoint(40,20);
        polygon.addPoint(70,40);
        polygon.addPoint(60,80);
        polygon.addPoint(45,45);
        polygon.addPoint(20,60);

         //Draw the polygon
         g.drawPolygon(polygon);
     }

     public Dimension getPreferredSize()
     {
         return new Dimension(200,200);
     }
}
}

推荐答案

首先查看如何编写鼠标侦听器

您将需要维护对Polygon对象的引用,并利用其contains方法来确定鼠标是否在Polygon本身之内...

You will need to maintain a reference to the Polygon object and make use of it's contains method to determine if the mouse is within the Polygon itself...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private RegularPolygonPanel polyPanel;
        private JLabel label;

        public TestPane() {
            polyPanel = new RegularPolygonPanel();
            polyPanel.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    if (polyPanel.isWithinPolygon(e.getPoint())) {
                        label.setText("Is inside");
                    } else {
                        label.setText("Is outside");
                    }
                }
            });
            label = new JLabel("...");
            setLayout(new BorderLayout());
            add(polyPanel);
            add(label, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    class RegularPolygonPanel extends JPanel {

        private Polygon polygon;

        public RegularPolygonPanel() {
            //Create a Polygon object
            polygon = new Polygon();

            polygon.addPoint(40, 20);
            polygon.addPoint(70, 40);
            polygon.addPoint(60, 80);
            polygon.addPoint(45, 45);
            polygon.addPoint(20, 60);
        }

        public boolean isWithinPolygon(Point p) {
            return polygon.contains(p);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            //Draw the polygon
            g.drawPolygon(polygon);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}

这篇关于如何根据光标位置更改框架中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆