Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-自定义形状面板?_Java_Swing_Jpanel - Fatal编程技术网

Java-自定义形状面板?

Java-自定义形状面板?,java,swing,jpanel,Java,Swing,Jpanel,我正在开发一个应用程序,用户需要将鼠标悬停在屏幕上的几个移动点上,才能启动特定的弹出窗口。目前,我正在收听JPanel上的mouseMoved事件,在该事件上渲染点,然后每当光标位于点的特定距离内时,启动所需的弹出窗口 当我有成百上千个圆点时,这可能会变得相当昂贵 理想的解决方案不是将我的“点”表示为小组件,并为每个点注册一个鼠标侦听器吗 有人知道我如何用JComponent表示一个小椭圆吗 非常感谢你写下“这个很可能很贵” 无论您是自己编写“isMouseCloseToDot”方法,还是使用S

我正在开发一个应用程序,用户需要将鼠标悬停在屏幕上的几个移动点上,才能启动特定的弹出窗口。目前,我正在收听
JPanel
上的
mouseMoved
事件,在该事件上渲染点,然后每当光标位于点的特定距离内时,启动所需的弹出窗口

当我有成百上千个圆点时,这可能会变得相当昂贵

理想的解决方案不是将我的“点”表示为小组件,并为每个点注册一个鼠标侦听器吗

有人知道我如何用
JComponent
表示一个小椭圆吗

非常感谢你写下“这个很可能很贵”

无论您是自己编写“isMouseCloseToDot”方法,还是使用Swing中内置的东西,在这两种情况下,工作仍然需要由计算机执行,以确定点是否被激活


我建议您坚持使用当前的方法,除非您确定这种方法确实太昂贵。用几百个点做一个小测试。响应时间可以接受吗?

不确定哪种方法更昂贵,但基于组件的方法肯定更容易实现

您所要做的就是创建自己的组件,覆盖
paint
contains
方法。
将其添加到容器中的特定位置(根据需要使用绝对布局或任何其他布局)。侦听器将成为提供组件行为的新组件的一部分


从设计和项目组织的角度来看,这是一种更优越的方法

下面是一些老代码,展示了如何创建“圆形”按钮。我将改为扩展JComponent,覆盖的重要方法是paintComponent()和contains():


这很容易简化命中检测,因为没有自定义代码。它还允许您轻松控制组件的重叠,因为您可以控制每个组件的Z顺序。

这是可以接受的。我有很多很多的听力活动要对面板上的各种对象执行。因此,尽管我假设swing例程会更有效,但我还面临一个侦听器类,该类到处都有几十个搜索循环,并且引用了面板上呈现的对象列表。从代码简洁的角度来看,这种方法也可以带来好处。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class RoundButton extends JButton {
    public RoundButton(String label) {
        super(label);

        // These statements enlarge the button so that it
        // becomes a circle rather than an oval.
        Dimension size = getPreferredSize();
        size.width = size.height = Math.max(size.width, size.height);
        setPreferredSize(size);

        // This call causes the JButton not to paint the background.
        // This allows us to paint a round background.
        setContentAreaFilled(false);
    }

    // Paint the round background and label.
    protected void paintComponent(Graphics g) {
    if (getModel().isArmed()) {
            // You might want to make the highlight color
            // a property of the RoundButton class.
            g.setColor(Color.lightGray);
        } else {
            g.setColor(getBackground());
        }
    g.fillOval(0, 0, getSize().width-1, getSize().height-1);

        // This call will paint the label and the focus rectangle.
    super.paintComponent(g);
    }

    // Paint the border of the button using a simple stroke.
    protected void paintBorder(Graphics g) {
        g.setColor(getForeground());
        g.drawOval(0, 0, getSize().width-1, getSize().height-1);
    }

    // Hit detection.
    Shape shape;
    public boolean contains(int x, int y) {
        // If the button has changed size, make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    }

    // Test routine.
    public static void main(String[] args) {
        // Create a button with the label "Jackpot".
        JButton button = new RoundButton("Jackpot");
        button.setBackground(Color.green);
        button.setBounds(0, 0, 100, 100);

        JButton button2 = new RoundButton("Jackpot2");
        button2.setBackground(Color.red);
        button2.setBounds(50, 50, 100, 100);

        // Create a frame in which to show the button.
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.yellow);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(button);
        frame.getContentPane().add(button2);
//        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(200, 200);
        frame.setVisible(true);

        MouseListener mouseListener = new MouseAdapter() {
            public void mouseEntered( MouseEvent e )
            {}

            public void mouseExited( MouseEvent e )
            {}

            public void mouseClicked( MouseEvent e )
            {
                System.out.println( "clicked " );
            }

            public void mousePressed( MouseEvent e )
            {
                System.out.println( "pressed " );
            }

            public void mouseReleased( MouseEvent e )
            {
                System.out.println( "released " );
            }
        };
        button.addMouseListener( mouseListener );

    }
}