单击按钮时在JPanel上绘制图像

时间:2013-06-07 16:51:56

标签: java swing jpanel mouselistener

当我点击按钮时,我正试图在JPanel上绘制图像,但我对paintComponentmouselistener感到困惑。

这是我到目前为止所做的代码。

public class Efi {

    public JFrame frame;
    public JButton button;
    private JPanel panel;
    private JPanel panel1 = new JPanel(new FlowLayout());
    private JPanel panel2 = new JPanel(new FlowLayout());
    private Image sex = new ImageIcon("img.jpg").getImage();

    public Efi() {
        frame = new JFrame("EFI'S APPLICATION");
        frame.setLayout(null);
        button = new JButton("IMAGE");
        button.setLocation(0, 0);
        button.setSize(500, 50);
        frame.add(button);
        button.addMouseListener(ML);

        panel = new JPanel(null);
        panel.setSize(400, 400);
        panel.setLocation(50, 50);
        panel.setVisible(true);
        panel.setLayout(null);
        panel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        frame.add(panel);
   }
   // super.paintComponent(g);
   MouseListener ML = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    };
  }

3 个答案:

答案 0 :(得分:4)

阅读Swing tutorialHow to Write a Mouse ListenerCustom Painting部分应该有所帮助。

答案 1 :(得分:2)

承诺阅读提到的Swing tutorial @camickr?

好的,这是一个让你前进的例子:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TrivialPaintComponent implements Runnable {
    private final JFrame frame;
    private final JPanel myCanvas;

    private boolean displayImage = false;

    public void toggleDisplayImage() {
        displayImage = !displayImage;
        myCanvas.repaint();
    }

    public class MyCanvasPanel extends JPanel {

        public MyCanvasPanel() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(final MouseEvent e) {
                    toggleDisplayImage();
                }
            });
        }

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

            if (!displayImage) {
                return;
            }

            g.setColor(Color.RED);
            g.fillArc(0, 0, this.getWidth(), this.getHeight(), 0, 360);
        }

    }

    private final class TogglePaintAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        private TogglePaintAction(final String name) {
            super(name);
        }

        @Override
        public void actionPerformed(final ActionEvent e) {
            toggleDisplayImage();
        }

    }

    public TrivialPaintComponent() {
        frame = new JFrame();
        frame.setTitle("Trivial paintComponent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myCanvas = new MyCanvasPanel();

        frame.add(myCanvas, BorderLayout.CENTER);
        frame.add(new JButton(new TogglePaintAction("Toggle Image")), BorderLayout.SOUTH);
    }

    @Override
    public void run() {
        frame.setBounds(16, 16, 640, 480);
        frame.setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
        final TrivialPaintComponent application = new TrivialPaintComponent();
        application.run();
    }
}

答案 2 :(得分:1)

您不需要MouseListener,因为您唯一想要检测的是点击JButton,因此您可以使用ActionListenerMouseListener的目的是处理JComponent发生的不同事件,例如我们已发布等。您可以找到更多{{} 3}}。您还可以找到有关paintComponent机制here的更多信息。

我花时间实施它。

public class Efi extends JFrame {

    public JButton button;

    public Efi() {
        super("EFI'S APPLICATION"); //or you can omit this and use setTitle();
        setLayout(new BorderLayout());
        setSize(250,300);


        button = new JButton("IMAGE");
        add(button, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ImagePanel imagePanel = new ImagePanel();

                //JFrames methods
                add(imagePanel, BorderLayout.CENTER);
                revalidate();
                repaint();
            }
        });

        //Added those
        setLocationRelativeTo(null); //This is for centering the frame to your screen.
        setDefaultCloseOperation(EXIT_ON_CLOSE); //This for closing your application after you closing the window.

    }

    public class ImagePanel extends JPanel {

        private BufferedImage image;

        public ImagePanel() {
            try {
                image = ImageIO.read(new File("img.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setBorder(BorderFactory.createLineBorder(Color.black, 2));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
    }

    public static void main(String args[]) {
        new Efi().setVisible(true);
    }
}

enter image description here enter image description here