| |

VerySource

 Forgot password?
 Register
Search
View: 821|Reply: 5

Please help me analyze it, why does the following program enter an infinite loop?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-13 18:00:02
| Show all posts |Read mode
Please help me analyze it, why does the following program enter an infinite loop?
mainPanel.addComponentListener (new ComponentAdapter () {
      public void componentResized (ComponentEvent e) {
        Dimension d = mainPanel.getPreferredSize ();
        if (d.width <200) {
          mainPanel.setSize (200, d.height);
        }
        if (d.height <400) {
          mainPanel.setSize (d.width, 400);
        }
      }
    });
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-13 14:45:01
| Show all posts
Each time you resize, componentResized is triggered
And componentResized will resize
That will definitely produce an endless loop
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-4-14 14:45:01
| Show all posts
Is mainPanel.setSize (200, d.height) triggering the componentResize event again,
Is the infinite loop going on? !
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-4-14 15:00:02
| Show all posts
Is mainPanel.setSize () triggering the componentResize event again,
Is the infinite loop going on? !
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-4-18 14:45:01
| Show all posts
... where is the loop?
 if (d.width <200) {
          mainPanel.setSize (200, d.height);
        }
        if (d.height <400) {
What if d.width> 200&&d.height> 400?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-29 15:15:02
| Show all posts
The problem has been found: mainly caused by these two sentences
Dimension d = mainPanel.getPreferredSize ();

 mainPanel.setSize (d.width, 400);
In many circumstances:
  preferredSize << size
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list