Paint in JFrame not working (Java)
graphics, java, jframe, paint, swing
Solution
So what you're doing, is implementing a `paint` method inside your own `MainAc` class, not the `JFrame`.
Your `MainAc` class itself, should derive from the `JFrame`.
The code below should work. If you don't understand inheritance, you should look over your class notes, it'll be in there.
public class MainAc extends JFrame {
public static void main(String[] args) {
new MainAc();
}
public MainAc() {
super("Class Paint");
JButton button = new JButton("Click for more");
setSize(800, 600);
add(button);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setLayout(null);
button.setLocation(100,100);
button.setSize(200,100);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello", 200, 50);
}
}
Problem
So in class we are making a Java program. We are trying to use the paint(Graphics g) function in the JFrame. We have tried it in the past (weeks ago) and it used to work. But now it doesnt (or we made a mistake somewhere). We have also tried to use paintComponent(Graphics g) but nothing seems to work. Here is our code: ``` public class MainAc { public static void main(String[] args) { JFrame frame = new JFrame("Class Paint"); JButton button = new JButton("Click for more"); frame.setSize(800, 600); frame.add(button); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button.setLayout(null); button.setLocation(100,100); button.setSize(200,100); frame.setVisible(true); } public void paint(Graphics g){ g.drawString("Hello", 200, 50); } } ```