java图形窗口仅在调整大小时更新

时间:2018-01-28 16:47:51

标签: java swing user-interface

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;



//import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

class Rectangle extends JPanel {
   private static int rect_x = 40;
   private static int rect_y = 40;
   private static final int rect_width = 100;
   private static final int rect_height = rect_width;




    KeyListener listener;

    public Rectangle() {

            this.listener = new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e)
            {
            }
            @Override
            public void keyPressed(KeyEvent e)
            {
                //System.out.println(e.getKeyCode());

                // w
                if(e.getKeyCode() == 87)
                {
                    rect_y -= 10;
                    //revalidate();
                    repaint();

                }
                // s
                else if (e.getKeyCode() == 83)
                {
                    rect_y += 10;
                    repaint();

                }
                // a
                else if (e.getKeyCode() == 65)
                {
                    rect_x -= 10;
                    repaint();

                }
                // d
                else if (e.getKeyCode() == 68)
                {
                    rect_x += 10;
                    repaint();


                }
            }
            @Override
            public void keyReleased(KeyEvent e)
            {
            }
        };  }





   @Override
   public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawRect(rect_x, rect_y, rect_width, rect_height);
    g.fillRect(rect_x, rect_y, rect_width, rect_height);
    g.setColor(Color.BLACK);
}

   @Override
   public Dimension getPreferredSize() {
      // so that our GUI is big enough
      return new Dimension(rect_width + 2 * rect_x, rect_height + 2 * rect_y);   
   }

   // create the GUI explicitly on the Swing event thread
   private void createAndShowGui() {
      Rectangle mainPanel = new Rectangle();

      JFrame frame = new JFrame("DrawRect");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
      frame.addKeyListener(listener);
   }




   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Rectangle Rct = new Rectangle();
            Rct.createAndShowGui();


        }

      });



   }
}

此代码用于在屏幕上移动矩形。 由于某种原因,除非窗口最小化或调整大小,否则它不会更新。

我意识到之前已经多次询问过,但我发现的答案非常具体,或者高于我的理解。 我是java的新手很抱歉,如果我看起来像个傻瓜。

1 个答案:

答案 0 :(得分:3)

您正在创建一个Extra Rectangle对象,并在其上调用repaint(),这不是显示的对象,因此您的侦听器不会重新绘制显示的JPanel。不要这样做。只创建一个 Rectangle对象,这样您的重绘就会转到正确的引用。

例如,改变这个:

  Rectangle mainPanel = new Rectangle();  // ***** no!!! ****

  JFrame frame = new JFrame("DrawRect");

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(mainPanel);  // **** no ****
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
  frame.addKeyListener(listener);

到此:

  // Rectangle mainPanel = new Rectangle();

  JFrame frame = new JFrame("DrawRect");

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(this);  // ****** note change
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
  frame.addKeyListener(listener);

其他方面问题:

  • 首选键绑定到KeyListener
  • 避免使用诸如密钥代码之类的幻数,这会使调试变得困难。而是使用KeyEvent.VK_?常量。
  • 遵循Java命名约定:常量应该全部为大写。
  • 您的rect_x和y字段不应该是静态的。