Why cannot we use static keyword inside a method in java?

How to use paint method in a JFrame in java?

  • I'm trying to do a java program in which a paint method is used in JFrame. But the message contained in msg is not shown. Please, give me the answer. Here is my program import java.awt.Graphics; import java.awt.Paint; import javax.swing.JFrame; import javax.swing.RepaintManager; public class Image { String msg="This is for test"; public static void main(String[] args) { Image img= new Image(); } public Image(){ JFrame frame= new JFrame(); frame.setSize(400,400); frame.setVisible(true); frame.setDefaultCloseOperation(JFram… } public void paint(Graphics g) { g.drawString(msg, 10, 10); } }

  • Answer:

    import java.awt.BorderLayout; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Image { String msg = "This is for test"; public static void main(String[] args) { Image img = new Image(); } public Image() { JFrame frame = new JFrame(); frame.add(panel, BorderLayout.CENTER); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFram… } JPanel panel = new JPanel() { public void paint(Graphics g) { g.drawString(msg, 10, 10); } }; }

Rejaul at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.