简体   繁体   English

当鼠标悬停在JButton图像上时,我想放大它

[英]I want to Enlarge my JButton Image when my mouse hovers over it

I can't figure this out for the life of me. 我无法一生解决这个问题。 What I want to happen is when my mouse hovers over the "Start" JButton in the menu to enlarge it by like 50-100px. 我想发生的是,当我的鼠标悬停在菜单中的“开始” JButton以将其放大到50-100px时。 What is a way to do that, that would work with my code here? 有什么方法可以在这里使用我的代码? thanks in advance! 提前致谢! "I'm using java eclipse btw" don't know if that helps or not “我正在使用java eclipse btw”不知道是否有帮助

//START_Button
    JButton button = new JButton("Start");
    frame.pack();
    JTextPane TEXT = new JTextPane();
    BufferedImage buttonIcon;
    try {
        buttonIcon = ImageIO.read(new File("C:\\Users\\GOULDEN\\Desktop\\MENU_START.png"));
        button = new JButton(new ImageIcon(buttonIcon));
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setContentAreaFilled(false);
        Container contentPane = frame.getContentPane();
        contentPane.add(button);
        button.setBounds(500,250,300,75);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}

Implement MouseListener and use the mouseEntered() and mouseExited() to make your button bigger. 实现MouseListener并使用mouseEntered()mouseExited()使按钮变大。

Declare the variables that will be accessed in other methods as instance variables to be able to access them. 将将在其他方法中访问的变量声明为实例变量 ,以便能够访问它们。

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Demo extends JFrame implements MouseListener {

    private static final long serialVersionUID = 1L;    
    private JButton startButton;


    public Demo() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 500, 500);
        this.setLayout(null);        

        startButton = new JButton("Start");                                   
        startButton= new JButton(new ImageIcon("path/to/image.jpg"));
        startButton.setBorder(BorderFactory.createEmptyBorder());
        startButton.setContentAreaFilled(false);
        startButton.setBounds(1, 2, 100, 25);
        startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        startButton.addMouseListener(this);

        this.add(startButton);
        this.setVisible(true);
    }

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

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub


    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        startButton.setSize(400, 125);
        this.repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        startButton.setSize(100, 25);
        this.repaint();
    }              
    }

btw, there is really no need to use the bufferedImage if you aren't going to use your image properties, so just insert the image location directly into ImageIcon so you can remove the try-catch block 顺便说一句,如果您不打算使用图像属性,则实际上不需要使用bufferedImage ,因此只需将图像位置直接插入ImageIcon即可删除try-catch

new JButton(new ImageIcon("path/to/image.jpg"));

Check out these links to read more about the topics used in your example. 查看这些链接以阅读有关示例中使用的主题的更多信息。

  1. Variable Scopes 可变范围
  2. MouseListener implementation MouseListener的实现
  3. Working With Images 处理图像

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

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