JButton Release Action

筅森魡賤 提交于 2019-12-24 01:56:21

问题


I am trying to create an action on a JButton release and I am not sure how to accomplish this. I can make an action just fine when the button is pressed. When the button is pressed it will change the image to a red dot and when released it should change back to a default green dot.

My button press code is below if someone can point me in the direction on how to create an action when the button is released that would be most helpful. Thanks!

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == change1) {

        p1a.setIcon(CLR); // THIS IS THE IMAGE WHEN BUTTON PRESSED
        // WOULD LIKE TO CHANGE BACK TO DEFAULT IMAGE HERE WHEN BUTTON IS RELEASED
    }
}

回答1:


When the button is pressed it will change the image to a red dot and when released it should change back to a default green dot.

This can be entirely achieved within the JButton. See things like setPressedIcon(Icon) ..

E.G.

import javax.swing.*;
import java.net.*;

public class ButtonIcons {

    ButtonIcons() throws MalformedURLException {
        ImageIcon redIcon = new ImageIcon(new URL(
                "https://i.stack.imgur.com/wCF8S.png"));
        ImageIcon grnIcon = new ImageIcon(new URL(
                "https://i.stack.imgur.com/T5uTa.png"));
        JButton button = new JButton("Click me!", grnIcon);
        button.setPressedIcon(redIcon);
        JOptionPane.showMessageDialog(null, button);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                ButtonIcons o = new ButtonIcons();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}



回答2:


I'm not looking for an Icon on the button itself to change I am looking for the image to change in a JPanel....same concept though

Nice to have that information available ahead of time

One approach might be to attach a listener to the ButtonModel and monitor for it's state changes...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Icon normalIcon;
        private Icon pressedIcon;

        public TestPane() {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            normalIcon = makeIcon(Color.GREEN);
            pressedIcon = makeIcon(Color.RED);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JLabel label = new JLabel(normalIcon);
            JButton btn = new JButton("Pressy");

            add(btn, gbc);
            add(label, gbc);

            btn.getModel().addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    ButtonModel model = btn.getModel();
                    if (model.isArmed()) {
                        label.setIcon(pressedIcon);
                    } else {
                        label.setIcon(normalIcon);
                    }
                }
            });
        }

        protected Icon makeIcon(Color color) {
            BufferedImage img = new BufferedImage(25, 25, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(color);
            g2d.fillOval(0, 0, 25, 25);
            g2d.dispose();

            return new ImageIcon(img);
        }

    }

}


来源:https://stackoverflow.com/questions/52395907/jbutton-release-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!