java - 为什么我的paintComponent 不工作?

标签 java swing paintcomponent mouselistener

我想要对程序执行的操作是,当我单击图像时,矩形将与 JOptionPane 一起出现。然而,JOptionPane 是唯一弹出的东西。

我尝试更改方法并添加更多类,但没有任何效果>.<任何人都可以阐明我的问题吗?这是我的代码片段。

下面是我调用文件选择器的地方,它允许我选择我的照片。此外,这里还有许多其他内容,例如标签。

public Help(){

        fc.setDialogTitle("Choose an image file to begin:");
        int returnval = fc.showOpenDialog(null);
        if (returnval == JFileChooser.APPROVE_OPTION){ //when user selects a file, value returned will be JFileChooser.APPROVE_OPTION
            File file = fc.getSelectedFile(); //the File value of the selection is returned from a call on getSelectedFile
            try{
                image = ImageIO.read(file); //reads and loads File as image
            }
            catch (IOException e){}
                System.out.println("You chose to open this file: " + file.getName());
        }else
            System.out.println("No file selected.");

        icon = new ImageIcon(image);
        label = new JLabel(icon);
        tagName = new JLabel(input);

        label.addMouseListener(new ImagePanel());
        label.addMouseMotionListener(new ImagePanel());
        panel.add(tagName);
    }

最后是我的 ImagePanel 类,其中包含麻烦的 PaintComponent。另外,还有几个 mouseListener。

class ImagePanel extends JPanel implements MouseListener, MouseMotionListener{

        @Override
        public void mouseClicked(MouseEvent event) {
            // TODO Auto-generated method stub

                x = event.getX();
                y = event.getY();

                input = JOptionPane.showInputDialog("Enter tag name");
                tagName.setText("You have tagged: " + input);
                System.out.println(input);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

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

                if(image != null && isRectPresent){
                    g.setColor(Color.DARK_GRAY);
                    g.drawRect(x-50, y-50, 100, 100);
                }
        }   

        @Override
        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub
        }
    }

您可以编译代码并亲自查看。如果您知道该怎么做,请告诉我:)非常感谢!

最佳答案

各种奇怪的东西:

label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel()); 

您不应该只是为了向组件添加监听器而创建新的 JPanel。您已经拥有该面板的一个实例。

addMouseMotionListener(this);  

切勿在绘画方法中向组件添加监听器。您永远无法控制何时调用绘画方法,并且最终会多次添加相同的监听器。

关于java - 为什么我的paintComponent 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8242510/

相关文章:

java - 即使在轴上自动量程,我能否保留完整的刻度线?

java - 有没有一种有效的方法通过java程序裁剪PDF并另存为图像(.JPG)?

java - 使用集合 .txt 文件中的 Map TreeSet 对单选按钮进行分组

Java Jframe 正在显示,但内容(面板)未正确显示

java.lang.NoClassDefFoundError : Could not initialize class play. 数据.format.Formatters

java - 无法弄清楚如何按这些条件对列表进行排序

java - 在 JToggle 按钮的 Jpanel onclick 中添加或删除 JTextPane

java - Java中全透明窗口的形状刷新

java - 由paintComponent()绘制的东西在调用repaint()后消失

Java 叠加两个 JPanel