3

Okay I want my background image to change when helpButton is pressed. 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. 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);
            }
    }

    }
4

1 回答 1

4
bgl = new JLabel (bgi);

在这里,您正在创建一个新的 JLabel,并将其放入 bgl 变量中,但正在对其进行任何操作,并且不更改继续显示在 GUI 中的 JLabel 对象。这是一个常见的新手陷阱,认为通过更改变量的引用可以更改变量先前引用的原始对象的状态。这不是它的工作原理。换句话说,由 bgl 变量保存的原始 JLabel 仍然存在,并且仍然在 GUI 中显示其原始内容,尽管上面有这段代码。您应该做的是更改原始 JLabel 显示的图标,或者换句话说,更改当前 JLabel 对象的状态,而不是更改 bgl 变量持有的引用。IE,

bgl.setIcon(bgi);

此外,您将希望摆脱对 null 布局和调用的任何和所有使用,setBounds(...)因为这将导致难以维护和升级代码的错误。让布局管理器完成有关布局 GUI 的繁重工作。

于 2013-06-10T23:59:10.520 回答