- You don't need the
while
loop with theThread.sleeap()
. It's screwing with your program. - What you're trying to achieve here, a call to repaint continuously, can easily be accomplished with a
javax.swing.Timer
- Don't explicitly call
paintComponent
. An actual call to the realrepaint()
method will do that for you. No need to create an imitationRepaint()
- Use a
JPanel
for painting instead of trying to paint on aJFrame
- I would initialize, then set visible
- Your code doesn't even compilable, so I can try and make fixes for you.
- Use Java naming convention: methods and variable start with lower case letters.
- Here's an example of a simple Login Window, using a modal
JDialog
- Learn how to use Layout Managers instead of relying on
setBounds()
Point 2. Your main
should look like this
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
LoginWindow loginframe = new LoginWindow();
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginframe.initialize();
loginframe.setVisible(true);
}
});
}
And in your constructor, have a javax.swing.Timer
instead of your while
loop
private Timer timer = null;
private DrawPanel drawPanel = new DrawPanel(); // see below for DrawPanel
public LoginWindow() {
// add drawPanel somewhere
...
timer = new Timer (50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
});
timer.start();
}
Point 4. Have an inner JPanel
class that you do all your painting in, and add that component to the frame. You'll need to override the paintComponent
method.
private DrawPanel extends JPanel {
@Override
protected void paintComponent(Graophics g) {
super.paintComponent(g);
Draw(g);
}
}