Home > Back-end >  Although will use, but for the swing of the running rules or mechanisms are not well understood.
Although will use, but for the swing of the running rules or mechanisms are not well understood.

Time:10-30

This is a very simple example to swing, I also can see the more complex function, but the mechanism is not understood,
I draw a rectangle in the paint method, operation can be seen as a result,
But, you see not a word of it was call paint method in the code, it is automatically called how? Paint method parameters to preach a Graphics object, but I did not create Graphics object, how it is automatically?

 import Java awt. *; 

Public class extends A Frame {

The public (A) {
Enclosing setVisible (true);
}

@ Override
Public void paint (Graphics g) {
G.f illRect (50, 50, 180, 120);
}

Public static void main (String [] args) {
New A ();
}
}

CodePudding user response:

The feeling that is created without Graphics object, privately run paint method,

CodePudding user response:

refer to the original poster qq_16774199 response:
this is a very simple example to swing, I also can see the more complex function, but the mechanism is not understood,
I draw a rectangle in the paint method, operation can be seen as a result,
But, you see not a word of it was call paint method in the code, it is automatically called how? Paint method parameters to preach a Graphics object, but I did not create Graphics object, how it is automatically?

 import Java awt. *; 

Public class extends A Frame {

The public (A) {
Enclosing setVisible (true);
}

@ Override
Public void paint (Graphics g) {
G.f illRect (50, 50, 180, 120);
}

Public static void main (String [] args) {
New A ();
}
}



These questions you need to analyze the source code, are generally or static block of code to achieve the construction method,

CodePudding user response:

Use new (A) create inherited the object of the Frame is called after Frame constructor, can open source in the Frame class constructor,

CodePudding user response:

reference 3 floor?? PURPLE CLOUD? Response:
use new () to create A inherited the object of the Frame is called after the Frame of the constructor, can open source in the Frame class constructor,


Frame I look at the source of the constructor, cruise around, don't clearly see the paint method calls,
Given the current reading source capacity is insufficient, can't understand how they are automatically calls, only know anyway is invoked,

CodePudding user response:

You, of course, I can't find it, because it doesn't call paint, but the execution of the

CodePudding user response:

Rest assured, you won't write white writing method, the bottom will automatically call,
You can refer to the source code paintComponent method

CodePudding user response:

reference 4 floor qq_16774199 response:
Frame I look at the source of the constructor, cruise around, don't clearly see the paint method calls,
Given the current reading source capacity is insufficient, can't understand how they are automatically calls, only know anyway is invoked,



Not only to look at the source of the frame and the frame of the source of the parent, may also see the frame of the parent class parent, Java source code is a lot of dolls, to uncover many of you can see after
  • Related