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);
}
}
So what you’re doing, is implementing a
paintmethod inside your ownMainAcclass, not theJFrame.Your
MainAcclass itself, should derive from theJFrame.The code below should work. If you don’t understand inheritance, you should look over your class notes, it’ll be in there.