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
Related Q & A:
- how to display .pgm image on a JFrame?Best solution by Stack Overflow
- How to use Facebook login in a modal view in iOS SDK?Best solution by Stack Overflow
- How to use these skills in a job?Best solution by Yahoo! Answers
- How to use my laptop as a monitor for my Xbox 360?Best solution by Yahoo! Answers
- How to use my Monitor as a TV screen?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.