My Java Learning notes (22) about explicit information in frames

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.