I am getting the error when compiling:
RoundButtonExample.java:38: error: non-static variable this cannot be referenced from a static context
button = new RoundButton("Click me!");
The code is:
import javax.swing.*;
import java.awt.*;
public class RoundButtonExample {
public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);
setOpaque(false);
setContentAreaFilled(false);
setFocusPainted(false);
setBorderPainted(false);
}
@Override
protected void paintComponent(Graphics g) {
if ( getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
super.paintComponent(g);
}
@Override
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Round Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RoundButton button;
button = new RoundButton("Click me!");
button.setPreferredSize(new Dimension(100, 100));
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Why?
0