import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainPanel extends JButton {
public Image borderImage=new ImageIcon(this.getClass().getResource("001.png")).getImage();
JFrame f= new JFrame();
JPanel p = new JPanel();
JButton b1 = new JButton("asdf");
JTextField tf = new JTextField(15);
public mainPanel(){
JButton button = new JButton(new CloseIcon());
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
button.setOpaque(false);//enable this to create a button border
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
f.setSize(500,500);
p.add(button);
p.add(tf);
f.add(p);
f.setVisible(true);
}
}
class CloseIcon implements Icon,MouseListener {
public int width;
public int height;
boolean hovered;
public Image img1=new ImageIcon(this.getClass().getResource("button1.png")).getImage();
public Image img2=new ImageIcon(this.getClass().getResource("button2.png")).getImage();
public CloseIcon() {
width = img1.getWidth(null);
height = img1.getHeight(null);
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
if(hovered=true){
g.drawImage(img1, 0, 0, null);
}else if(hovered=false){
g.drawImage(img2, 0, 0, null);
}
}
@Override public int getIconWidth() {
return width;
}
@Override public int getIconHeight() {
return height;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
hovered=true;
}
@Override
public void mouseExited(MouseEvent e) {
hovered=false;
}
}
custom java button with hovering effect
Page 1 of 14 Replies - 457 Views - Last Post: 28 June 2012 - 08:14 PM
#1
custom java button with hovering effect
Posted 27 June 2012 - 04:20 PM
Hello. I am having a problem finding my problem i am trying to create a hovering effect on my button which i have created from a image.could any one tell me what i have to do to fix the error.it does not change when it is hovered over
Replies To: custom java button with hovering effect
#2
Re: custom java button with hovering effect
Posted 27 June 2012 - 04:36 PM
#3
Re: custom java button with hovering effect
Posted 27 June 2012 - 06:05 PM
If your class mainPanel extends JButton (honestly you could have found a better name than mainPanel for a class that extends JButton like MyButton as example) why do you create a JButton inside it ?
It is already a JButton so should be:
setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
p.add(this);
...
It is already a JButton so should be:
setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
p.add(this);
...
#4
Re: custom java button with hovering effect
Posted 28 June 2012 - 04:14 AM
pbl, on 27 June 2012 - 06:05 PM, said:
If your class mainPanel extends JButton (honestly you could have found a better name than mainPanel for a class that extends JButton like MyButton as example) why do you create a JButton inside it ?
It is already a JButton so should be:
setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
p.add(this);
...
It is already a JButton so should be:
setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
p.add(this);
...
i am doing the extend of the button because i want to use an image as a button.and i want to create a hovering effectwith the mouse
#5
Re: custom java button with hovering effect
Posted 28 June 2012 - 08:14 PM
Does not explain: why you create a JButton inside your class that is already a JButton
Also, JButton are put inside JPanel or JFrame. Why a class that extends JButton would create a JFrame ?
I would understand that a JFrame would contain different different JPanel that would contain one or multiple mainPanel (aka JButton).
There is one JFrame per application, if all the JPanel contained in the master JFrame contain a total 20 mainPanel(JButton) you will end up with 20 JFrame and 20 JPanel in the 20 mainPanel... just does not make sense
Something like:
would make sense
Also, JButton are put inside JPanel or JFrame. Why a class that extends JButton would create a JFrame ?
I would understand that a JFrame would contain different different JPanel that would contain one or multiple mainPanel (aka JButton).
There is one JFrame per application, if all the JPanel contained in the master JFrame contain a total 20 mainPanel(JButton) you will end up with 20 JFrame and 20 JPanel in the 20 mainPanel... just does not make sense
Something like:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainPanel extends JButton {
public Image borderImage=new ImageIcon(this.getClass().getResource("001.png")).getImage();
public mainPanel(){
super(new CloseIcon());
setContentAreaFilled(false);
setFocusPainted(false);
setBorder(BorderFactory.createEmptyBorder());
setOpaque(false);//enable this to create a button border
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
}
would make sense
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|