The 1.JFRAME structure is quite complex and has four layers of panels. Where swing programmers are most concerned is the content pane.
2. To draw a component, you need to define a class that extends JComponent and override the Paintcomponent method in it.
Class Exp extends Jcomponent{public void paintcomponent (Graphics g) {...}}
The 3.paintComponent method has a graghics type parameter that holds the settings for drawing images and text.
4. In Java, all drawings must use the Graphics object.
5. For whatever reason, the event handler advertises the component as long as the window needs to be redrawn, causing the Paintcpmponent method to execute all the components, so be sure not to call the method yourself.
6. Explicit text uses the DrawString method in the graphics class.
g.DrawString (Test,x,y);
7. When filling in a frame with one or more components, if you only want to use their preferred size, you can call the pack method instead of the SetSize method.
Instance Code
Test class
Import Java.awt.eventqueue;import Javax.swing.jframe;public class Test {public static void main (string[] args) { Eventqueue.invokelater (New Runnable () {public void run () {JFrame frame = new TextFrame (); Frame.settitle ("Test"); Frame.setdefaultcloseoperation (Jframe.exit_on_close); frame.setvisible (True);}});}}
Framework class
Import Javax.swing.jframe;public class TextFrame extends Jframe{public TextFrame () {Add (New Textstr ());p ack ();}}
Drawing classes
Import Java.awt.color;import java.awt.dimension;import Java.awt.graphics;import Javax.swing.jcomponent;public class TEXTSTR extends JComponent {public static final int message_x = 75;public static final int message_y = 50;private static F inal int default_width = 300;private static final int default_height = 200;public void Paintcomponent (Graphics g) {G.setcol or (color.red); g.DrawString ("Hello Ah ah Ah!! ", message_x, message_y);} Public Dimension getPreferredSize () {return new Dimension (default_width,default_height);}
Output results
My Java Learning notes (22) about explicit information in frames