设置JButton在不同状态的图片
1.源代码:
//
创建并设置一个登录按钮,并将其添加到用来添加按钮的面板中 final JButton landButton = new JButton();// 创建登录按钮组件对象 landButton.setMargin(new Insets(0, 0, 0, 0));// 设置按钮边框和标签之间的间隔 landButton.setContentAreaFilled(false);// 设置不绘制按钮的内容区域 landButton.setBorderPainted(false);// 设置不绘制按钮的边框 URL landUrl = this.getClass().getResource("/land_submit.png");// 获得默认情况下登录按钮显示图片的URL landButton.setIcon(new ImageIcon(landUrl));// 设置默认情况下登录按钮显示的图片 URL landOverUrl = this.getClass().getResource( "/land_submit_over.png");// 获得当鼠标经过登录按钮时显示图片的URL landButton.setRolloverIcon(new ImageIcon(landOverUrl));// 设置当鼠标经过登录按钮时显示的图片 URL landPressedUrl = this.getClass().getResource( "/land_submit_pressed.png");// 获得当登录按钮被按下时显示图片的URL landButton.setPressedIcon(new ImageIcon(landPressedUrl));// 设置当登录按钮被按下时显示的图片
landButton.addActionListener(new LandButtonActionListener ());// 为登录按钮添加事件监听器 buttonPanel.add(landButton);// 将登录按钮添加到用来添加按钮的面板中 2.相关代码:
class LandButtonActionListener
implements ActionListener {
public void actionPerformed(ActionEvent e){ }
}
3.又一个例子
final MButton passwordButton = new MButton(); URL passwordUrl = this.getClass().getResource("/img/password.png"); passwordButton.setIcon(new ImageIcon(passwordUrl)); URL passwordOverUrl = this.getClass().getResource( "/img/password_over.png"); passwordButton.setRolloverIcon(new ImageIcon(passwordOverUrl)); toolBar.add(passwordButton);
设置JButton在不同状态的图片
最新推荐文章于 2021-05-16 23:20:58 发布