Java JPanel某些区域上的气球尖

Java JPanel某些区域上的气球尖,java,swing,jpanel,Java,Swing,Jpanel,我想在JPanel上的特定区域上显示。我知道如何在JComponent上执行此操作 我在JPanel上有一行,只有当鼠标在行上移动时才能显示BallonTip 谢谢记住,JPanel是从JComponent扩展而来的,所以基本上是相同的过程 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graph

我想在
JPanel
上的特定区域上显示。我知道如何在
JComponent
上执行此操作

我在
JPanel
上有一行,只有当鼠标在行上移动时才能显示
BallonTip


谢谢

记住,
JPanel
是从
JComponent
扩展而来的,所以基本上是相同的过程

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
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;
import net.java.balloontip.CustomBalloonTip;
import net.java.balloontip.examples.complete.Utils;

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) {
                }

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

    public class TestPane extends JPanel {

        private CustomBalloonTip customBalloonTip;

        private Rectangle square = new Rectangle(50, 50, 200, 200);

        public TestPane() {
            customBalloonTip = new CustomBalloonTip(this,
                    new JLabel("<html>I'm a " + Utils.monospace("CustomBalloonTip") + ".</html>"),
                    square,
                    Utils.createBalloonTipStyle(),
                    Utils.createBalloonTipPositioner(),
                    null);
            customBalloonTip.setVisible(false);

            addMouseMotionListener(new MouseAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {
                    if (square.contains(e.getPoint())) {
                        customBalloonTip.setVisible(true);
                    } else {
                        customBalloonTip.setVisible(false);
                    }
                }

            });

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(square);
            g2d.dispose();
        }
    }

}

导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入net.java.balloottip.CustomBalloottip;
导入net.java.balloottip.examples.complete.Utils;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人定制气球提示定制气球提示;
私有矩形正方形=新矩形(50,50,200,200);
公共测试窗格(){
CustomBalloadTip=新的CustomBalloadTip(此,
新的JLabel(“我是一个”+Utils.monospace(“CustomBalloadTip”)+”,
广场
Utils.CreateBloadTipstyle(),
Utils.createBallootTippositioner(),
无效);
CustomBalloadTip.setVisible(false);
addMouseMotionListener(新的MouseAdapter(){
@凌驾
public void mouseMoved(MouseEvent e){
if(square.contains(如getPoint())){
CustomBalloadTip.setVisible(true);
}否则{
CustomBalloadTip.setVisible(false);
}
}
});
}
@凌驾
公共维度getPreferredSize(){
返回新维度(300300);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
g2d.setColor(Color.RED);
g2d.填充(方形);
g2d.dispose();
}
}
}
现在具有点/线交点

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.java.balloontip.CustomBalloonTip;
import net.java.balloontip.examples.complete.Utils;

public class TestBalloon {

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

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

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

    public class TestPane extends JPanel {

        private CustomBalloonTip customBalloonTip;

        private List<Line2D> lines = new ArrayList<>(25);
        private Point2D hitPoint = null;

        public TestPane() {

            customBalloonTip = new CustomBalloonTip(this,
                    new JLabel("<html>I'm a " + Utils.monospace("CustomBalloonTip") + ".</html>"),
                    new Rectangle(0, 0, 1, 1),
                    Utils.createBalloonTipStyle(),
                    Utils.createBalloonTipPositioner(),
                    null);
            customBalloonTip.setVisible(false);

            lines.add(new Line2D.Float(0, 0, 300, 300));
            lines.add(new Line2D.Float(300, 0, 0, 300));

            hitPoint = null;

            addMouseMotionListener(new MouseAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {

                    Point mp = e.getPoint();
                    Rectangle offset = null;

                    for (Line2D line : lines) {
                        double distance = line.ptSegDist(mp);
                        if (distance <= 4) {
                            offset = new Rectangle(mp, new Dimension(1, 1));
                        }
                    }

                    if (offset != null) {
                        System.out.println(offset);
                        customBalloonTip.setOffset(offset);
                        customBalloonTip.setVisible(true);
                    } else {
                        customBalloonTip.setVisible(false);
                    }

                    repaint();
                }

            });

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            for (Line2D line : lines) {
                g2d.draw(line);
            }

            if (hitPoint != null) {
                Ellipse2D ellipse = new Ellipse2D.Double(hitPoint.getX() - 5, hitPoint.getY() + 5, 10, 10);
                g2d.fill(ellipse);
            }
            g2d.dispose();
        }
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Point;
导入java.awt.Rectangle;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Line2D;
导入java.awt.geom.Point2D;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入net.java.balloottip.CustomBalloottip;
导入net.java.balloottip.examples.complete.Utils;
公共类测试气球{
公共静态void main(字符串[]args){
新的TestBalloon();
}
公共测试气球(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人定制气球提示定制气球提示;
私有列表行=新的ArrayList(25);
private Point2D hitPoint=null;
公共测试窗格(){
CustomBalloadTip=新的CustomBalloadTip(此,
新的JLabel(“我是一个”+Utils.monospace(“CustomBalloadTip”)+”,
新矩形(0,0,1,1),
Utils.CreateBloadTipstyle(),
Utils.createBallootTippositioner(),
无效);
CustomBalloadTip.setVisible(false);
添加(新的Line2D.Float(0,0,300,300));
添加(新的Line2D.Float(300,0,0,300));
hitPoint=null;
addMouseMotionListener(新的MouseAdapter(){
@凌驾
public void mouseMoved(MouseEvent e){
点mp=e.getPoint();
矩形偏移=空;
用于(线条2D线条:线条){
双倍距离=行。ptSegDist(mp);

如果(距离记住,
JPanel
JComponent
扩展而来,那么基本上,这是相同的过程

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
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;
import net.java.balloontip.CustomBalloonTip;
import net.java.balloontip.examples.complete.Utils;

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) {
                }

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

    public class TestPane extends JPanel {

        private CustomBalloonTip customBalloonTip;

        private Rectangle square = new Rectangle(50, 50, 200, 200);

        public TestPane() {
            customBalloonTip = new CustomBalloonTip(this,
                    new JLabel("<html>I'm a " + Utils.monospace("CustomBalloonTip") + ".</html>"),
                    square,
                    Utils.createBalloonTipStyle(),
                    Utils.createBalloonTipPositioner(),
                    null);
            customBalloonTip.setVisible(false);

            addMouseMotionListener(new MouseAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {
                    if (square.contains(e.getPoint())) {
                        customBalloonTip.setVisible(true);
                    } else {
                        customBalloonTip.setVisible(false);
                    }
                }

            });

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(square);
            g2d.dispose();
        }
    }

}