【问题标题】:Image change when button is pressed Java按下按钮时图像更改Java
【发布时间】:2013-06-06 17:50:06
【问题描述】:

好的,我希望在按下 helpButton 时更改背景图像。当鼠标悬停在鼠标侦听器上时,我可以更改按钮图像。除了使用动作侦听器外,我执行了相同的步骤,但没有成功。任何帮助都会很棒!

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);
            }
    }

    }

【问题讨论】:

  • 你似乎忽略了我对昨天问题的回答——为什么?我们是志愿者,您的感谢通常是我们帮助您的动力。

标签: java image swing jbutton jlabel


【解决方案1】:
bgl = new JLabel (bgi);

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

bgl.setIcon(bgi);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多