Question

I have no Idea why my JFrame keeps freezeing after I type Letters into the JTextField "Username" and "Password" :/ Could anyone look thru my code and tell my why and fix it please ?

public class Main 
{       
    public static void main(String [ ] args)
    {    
        LoginWindow loginframe = new LoginWindow();
        loginframe.setVisible(true);
        loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginframe.initialize();    
        while(true)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            loginframe.Repaint();
        }
    }
}

FrameClass:

public class LoginWindow extends JFrame implements MouseListener, KeyListener, MouseMotionListener{

    public LoginWindow(){
        setSize(806, 629);
        setResizable(false);
        setLayout(new BorderLayout());
        background = new JLabel(ResourceLoader.Iconload("/main_01.jpg"));
        background.setBounds(0, 0, 800, 600);
        add(background);            
        background.setLayout(null);
        Username = new JTextField("", 20);
        Username.setForeground(Color.WHITE);
        Username.setBounds(312, 433, 170, 40);
        Username.setFont(new Font("Impact", Font.BOLD, 25));
        Username.setBackground(Color.BLACK);
        Username.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.BLACK));
        background.add(Username);           
        Password = new JPasswordField("", 20);
        Password.setForeground(Color.WHITE);
        Password.setBounds(312, 489, 170, 40);
        Password.setBackground(Color.BLACK);
        Password.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.BLACK));
        Password.setFont(new Font("Serif", Font.BOLD, 25));
        background.add(Password);
    }

    public void initialize()
    {
        makestrat();    
        addKeyListener(this);
        requestFocus();
        addMouseListener(this);
        addMouseMotionListener(this);    
    }

    public void makestrat()
    {
        createBufferStrategy(2);
        strat = getBufferStrategy(); 
    }

    public void Repaint()
    {           
        //System.out.println(mouseX + " " + mouseY);
        Graphics g = strat.getDrawGraphics();
        paintComponents(g);
        Draw(g);
        g.dispose();            
        strat.show();
    }

    public void Update()
    {           
    }

    public void Draw(Graphics g)
    {
        if(((mouseX >= 499) && (mouseX <=  669)) && ((mouseY >= 433)&&( mouseY <= 530))){
            g.drawImage(ResourceLoader.ImageLoad("/login_02.jpg"), 502, 459, null);
        }else{
            g.drawImage(ResourceLoader.ImageLoad("/login_01.jpg"), 502, 459, null);
        }
    }

    private class thehandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {   

        }           
    }

    public void mouseMoved(MouseEvent event)
    {
        mouseY = event.getY()-26;
        mouseX = event.getX()-3;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point point = new Point(a.getLocation());
        SwingUtilities.convertPointFromScreen(point, e.getComponent());
        double mouseX = (int) point.getX();
        double mouseY = (int) point.getY(); 
        System.out.println("(ContainerPos) Mouse clicked! X: " + mouseX + " Y: " + mouseY);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mousePressed(MouseEvent e) {            
    }

    @Override
    public void mouseReleased(MouseEvent e) {           
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub          
    }
}
Was it helpful?

Solution

  1. You don't need the while loop with the Thread.sleeap(). It's screwing with your program.
  2. What you're trying to achieve here, a call to repaint continuously, can easily be accomplished with a javax.swing.Timer
  3. Don't explicitly call paintComponent. An actual call to the real repaint() method will do that for you. No need to create an imitation Repaint()
  4. Use a JPanel for painting instead of trying to paint on a JFrame
  5. I would initialize, then set visible
  6. Your code doesn't even compilable, so I can try and make fixes for you.
  7. Use Java naming convention: methods and variable start with lower case letters.
  8. Here's an example of a simple Login Window, using a modal JDialog
  9. 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);
    }
}

OTHER TIPS

The obvious thing is that you shouldn't use Swing (or AWT really) off of the AWT Event Dispatch Thread (EDT). Fix with java.awt.EventQueue.invokeLater and java.swing.Timer (not java.util!). Diagnose with jstack(and jps) or the relevant key sequence in the terminal window (um, ctrl-break in Windows, ctrl-3 in Linux).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top