I am trying to creating a customized JButton. I noticed that when I declare the class class MyButton extends JButton
, it works ok. But Eclipse shows "illegal modifier" error when I changed the class name to private class MyButton
or protect class MyButton
.
I understand that only one class can be declared as public in one file, but what's wrong with declaring it as a private class? Can someone help to explain how the different modifiers work for the classes in this case? Thanks!
class MyButton extends JButton {
@Override
protected void paintComponent(Graphics g){
Graphics2D g2d =(Graphics2D)g;
g2d.setColor(new Color(200,50,50,255));
g2d.fill(new RoundRectangle2D.Float(0,0,getWidth(),getHeight(),100,100));
}
}
public class CustomButton extends JFrame{
public CustomButton (){
add(new MyButton());
}
public static void main(String[] args) {
CustomButton b = new CustomButton();
b.setVisible(true);
}
}