Add ActionEvent in Swing [closed]

2019-09-20 05:17发布

class ImageComponent extends JComponent 
        implements MouseListener, MouseMotionListener {
    private final BufferedImage img;
    private Point p1, p2;

    public ImageComponent(File file) throws IOException {
        img = ImageIO.read(file);
        setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
        if (p1 != null && p2 != null)
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
    }
    @Override public void mousePressed(MouseEvent e) {
        p1 = e.getPoint();
    }
    @Override public void mouseDragged(MouseEvent e) {
        mouseReleased(e);
    }
    @Override public void mouseReleased(MouseEvent e) {
        p2 = e.getPoint();
        repaint();
    }
    @Override public void mouseMoved(MouseEvent e) {}
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}

    public static void main(String[] args) throws Exception {

        JFileChooser chooser=new JFileChooser();
        chooser.setCurrentDirectory(new File(" "));
        chooser.showOpenDialog(new JFrame());
        File file=chooser.getSelectedFile();
        final ImageComponent image = new ImageComponent(file);

        JFrame frame = new JFrame("Test");
        frame.add(new JScrollPane(image));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
}
}

Here in this code, it takes a file using JFileChooser directly. Now i want to add a button on which when i click, it should generates an ActionEvent and should open a JFileChooser from which i can choose a file and then it should show me that image on frame. How can i do that? I Have created something like that

JButton open_button=new JButton("Add File");
open_button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                try {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setCurrentDirectory(new File("."));
                    chooser.showOpenDialog(new JFrame());
                    File file = chooser.getSelectedFile();
                    ImageComponent image=new ImageComponent(file);


                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "IOException Occured");
                }
            }

But it isn't working..

2条回答
何必那么认真
2楼-- · 2019-09-20 05:26

This answer is just for reference (see the original question/answer):

public static void main(String[] args) {
    final JFrame frame = new JFrame("Test");
    final ImageComponent imageComponent = new ImageComponent();

    frame.add(new JButton(new AbstractAction("Load") {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser c = new JFileChooser(new File("."));
            if (c.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                try {
                    imageComponent.set(ImageIO.read(c.getSelectedFile()));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }), BorderLayout.NORTH);
    frame.add(new JScrollPane(imageComponent), BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

And the ImageComponent:

static class ImageComponent extends JComponent 
        implements MouseListener, MouseMotionListener {
    BufferedImage img; 
    Point p1, p2;
    public ImageComponent() {
        addMouseListener(this); addMouseMotionListener(this);
    }
    public void set(BufferedImage img) {
        this.img = img;
        setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
        repaint();
    }
    @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null)
            g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
        if (p1 != null && p2 != null)
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
    }
    @Override public void mousePressed(MouseEvent e) {
        p1 = e.getPoint();
    }
    @Override public void mouseDragged(MouseEvent e) {
        mouseReleased(e);
    }
    @Override public void mouseReleased(MouseEvent e) {
        p2 = e.getPoint();
        repaint();
    }
    @Override public void mouseMoved(MouseEvent e) {}
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
}
查看更多
SAY GOODBYE
3楼-- · 2019-09-20 05:40

Add Button In frame(In JFrame) object And also addActionListener For Button Click in Java

http://leepoint.net/notes-java/GUI/components/20buttons/10jbutton.html

查看更多
登录 后发表回答