Java - JFrame在我将一些字母输入JTextField后继续冻结

时间:2014-01-12 19:15:50

标签: java swing jframe paintcomponent repaint

我不知道为什么我的JFrame在JTextField“用户名”和“密码”中输入信件后仍然保持冻结:/任何人都可以查看我的代码并告诉我原因并解决它吗?

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          
    }
}

2 个答案:

答案 0 :(得分:1)

显而易见的是,您不应该使用AWT事件调度线程(EDT)中的Swing(或AWT)。修复了java.awt.EventQueue.invokeLaterjava.swing.Timer java.util!)。使用jstack(和jps)或终端窗口中的相关键序列进行诊断(嗯,Windows中的ctrl-break,Linux中的ctrl-3)。

答案 1 :(得分:1)

  1. 您不需要while循环Thread.sleeap()。它正在搞乱你的程序。
  2. 你在这里想要实现的目标,可以通过javax.swing.Timer
  3. 轻松完成连续重拍的调用
  4. 请勿明确呼叫paintComponent。对真实 repaint()方法的实际调用将为您完成此操作。无需创建模仿Repaint()
  5. 使用JPanel进行绘画,而不是尝试在JFrame
  6. 上绘画
  7. 我会初始化,然后设置可见
  8. 您的代码甚至无法编译,因此我可以尝试为您修复。
  9. 使用Java命名约定:方法和变量以小写字母开头。
  10. 这是example of a simple Login Window,使用模态JDialog
  11. 了解如何使用Layout Managers而不是依赖setBounds()

  12. 第2点。您的main应该如下所示

    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);
            }
        });
    }
    

    在您的构造函数中,使用javax.swing.Timer而不是while循环

    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();
    }
    

    第4点。有一个内部JPanel类,您可以在其中完成所有绘画,并将该组件添加到框架中。您需要覆盖paintComponent方法。

    private DrawPanel extends JPanel {
        @Override
        protected void paintComponent(Graophics g) {
            super.paintComponent(g);
            Draw(g);
        }
    }