简体   繁体   English

按下按钮时图像更改Java

[英]Image change when button is pressed Java

Okay I want my background image to change when helpButton is pressed. 好的,我希望在按下helpButton时更改背景图像。 I can make the button image change when the mouse hovers over it with a Mouse Listener. 当鼠标悬停在鼠标监听器上时,我可以更改按钮图像。 I did the same steps except with the Action Listener but with no success. 除了Action Listener之外,我做了同样的步骤,但没有成功。 Any help would be great! 任何帮助都会很棒!

public class test extends JFrame{

    private JLabel label;
    private JButton button;

    private ImageIcon bgi;
    private JLabel bgl;

    public static Rectangle gameSquare;


    private JButton startButton;
    private JButton helpButton;
    private final Action action = new SwingAction();


    public static void main(String[] args) throws MalformedURLException, IOException {
        test gui = new test ();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
        gui.setSize(902, 305);
        gui.setVisible(true);
        gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
    }

    public test() throws MalformedURLException, IOException{

        bgi = new ImageIcon(getClass().getResource("tu.png"));
        getContentPane().setLayout(null);

        BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
        //ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
        startButton = new JButton("");
        startButton.setIcon(new ImageIcon(img));
        startButton.setBounds(22, 186, 114, 50);


        getContentPane().add(startButton);

        BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
        final JButton helpButton = new JButton("");
        helpButton.setIcon(new ImageIcon(img2));
        helpButton.setBounds(192, 186, 114, 50);

        getContentPane().add(helpButton);

        bgl = new JLabel (bgi);
        bgl.setBounds(0, 0, 886, 272);
        getContentPane().add(bgl);

        Events e = new Events();
        startButton.addActionListener(e);
        helpButton.addActionListener(e);
    }

    public class Events implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == startButton) {
               label.setText("Searching");

               try {
                Unfollow();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }
            else if (e.getSource() == helpButton){
                System.out.println("gottem");
                bgi = new ImageIcon(getClass().getResource("tu2.png"));
            bgl = new JLabel (bgi);
            }
    }

    }
bgl = new JLabel (bgi);

Here you're creating a new JLabel, and putting it into the bgl variable but are doing anything with it and make no change to the JLabel object that continues to be shown in the GUI. 在这里,您将创建一个新的JLabel,并将其放入bgl变量中,但正在对其执行任何操作,并且不会对继续在GUI中显示的JLabel对象进行任何更改。 It's a common newbie trap to think that by changing the reference of a variable you change the state of the original object that the variable previously referred to. 这是一个常见的新手陷阱,认为通过更改变量的引用,您可以更改变量先前引用的原始对象的状态。 That's not how it works. 这不是它的工作原理。 In other words, the original JLabel that was held by the bgl variable still exists and still displays its original content in the GUI despite this code above. 换句话说,由bgl变量保存的原始JLabel仍然存在,并且仍然在GUI中显示其原始内容,尽管上面的代码。 What you should do instead is to change the icon shown by the original JLabel, or in other words, change the state of the current JLabel object, not change references held by the bgl variable. 你应该做的是改变原始JLabel显示的图标,换句话说,改变当前JLabel对象的状态 ,而不是改变bgl变量持有的引用。 ie,

bgl.setIcon(bgi);

Also, you'll want to get rid of any and all use of null layout and calls to setBounds(...) as this will lead to buggy hard to maintain and upgrade code. 此外,您将希望摆脱任何和所有使用null布局和调用setBounds(...)因为这将导致难以维护和升级代码的错误。 Let the layout managers do the heavy lifting with regards to laying out the GUI. 让布局管理人员在布局GUI方面做了很多工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM