Warum ist das Verhalten einer Java -Swing -Anwendung zwischen den Läufen inkonsistent?Java

Java-Forum
Anonymous
 Warum ist das Verhalten einer Java -Swing -Anwendung zwischen den Läufen inkonsistent?

Post by Anonymous »

(Ich habe einen Blick auf das inkonsistente Display geworfen, wenn ein Java -Swing -Programm ausgeführt wird, aber es hat nicht geholfen.) < /P>
Hallo allerseits! Ich habe zwei Java -Dateien Window.java und Panel.java, hier angezeigt: < /p>
import javax.swing.*;

public class Window extends JFrame {
static Panel myPanel;

public Window() {
setSize(900, 900);
myPanel = new Panel();
add(myPanel);
setVisible(true);
}

public static void main(String[] args) {
Window w = new Window();
}
}
< /code>
import java.awt.*;
import javax.swing.*;

public class Panel extends JPanel {
public void paintComponent(Graphics g) {
System.out.println("Painting");
add(new JLabel("hi"));
}
}
< /code>
I am simply trying to get the JLabel "hi" to show up. The problem is, running Window.java produces very inconsistent results. Most of the time it produces a blank page (and an infinite loop for some reason, that I have to kill), but every once in a while it produces the desired output. Can anyone help me figure out what is going wrong in this code? Thank you!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post