简体   繁体   English

无法在ActionListener中重绘

[英]Can't repaint in ActionListener

EDIT: Now it works fine, but when i switch to the "game" JPanel, the KeyListener is not working :( Any way to fix this? 编辑:现在它工作正常,但当我切换到“游戏”JPanel时,KeyListener无法工作:(任何方法来解决这个问题?

i'm just programming a java game and there is a problem with the repaint() method: I wanna get from the menu to the game so i remove the menu panel and add the game panel ("Zeichnen" extends JPanel) But every time i try to repaint, eclipse gives me the following error: "The method repaint() is undefined for the type new ActionListener(){}" Can anyone please help me? 我只是编写一个java游戏,并且repaint()方法存在问题:我想从菜单到游戏,所以我删除菜单面板并添加游戏面板(“Zeichnen”扩展JPanel)但每次我尝试重新绘制,eclipse给了我以下错误:“方法repaint()未定义为类型new ActionListener(){}”任何人都可以帮助我吗? :( :(

Code: 码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Frame {

JFrame frame;
JPanel menu;
JButton start;
Zeichnen game;

public Frame() {

    start = new JButton("Start Game");
    menu = new JPanel();
    game = new Zeichnen();
    frame = new JFrame("Epic Game");

    start.addActionListener(new ActionListener () {
        public void actionPerformed(ActionEvent e) {

            frame.remove(menu);
            frame.add(game);
            repaint();

    }});

    frame.setVisible(true);
    frame.setSize(640,480);
    game.addKeyListener(new Listener(game));
    menu.add(start);
    frame.add(menu);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

}
}

Suggestsions: Suggestsions:

  • You should use a CardLayout to have a container swap components as you are doing since this is the easiest and most reliable way to change views. 您应该使用CardLayout来实现容器交换组件,因为这是更简单,最可靠的更改视图的方法。 There are many examples of using this in prior questions on this site. 在本网站的先前问题中有许多使用它的例子。
  • Don't use a KeyListener but rather use Key Bindings since they are much more forgiving with regard to component focus. 不要使用KeyListener,而是使用Key Bindings,因为它们在组件焦点方面更宽容。
  • Rename your "Frame" class to something that doesn't match a core Java class. 将“Frame”类重命名为与核心Java类不匹配的类。 It is a very confusing name. 这是一个非常令人困惑的名字。

For example: 例如:

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

public class ContentPane extends JPanel {

   public static final String MENU = "menu";
   public static final String GAME = "game";
   private static final int PREF_W = 640;
   private static final int PREF_H = 480;

   private CardLayout cardlayout = new CardLayout();
   private JPanel menu = new JPanel();
   private JButton start;
   private Zeichnen game;

   public ContentPane() {
      setLayout(cardlayout);
      start = new JButton("Start Game");
      game = new Zeichnen();

      add(menu, MENU);
      add(game, GAME);

      start.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardlayout.next(ContentPane.this);
         }
      });

      menu.add(start);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Epic Game");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ContentPane());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

And the Key Bindings: 和关键绑定:

@SuppressWarnings("serial")
class Zeichnen extends JPanel {
   private static final String DOWN = "down";

   public Zeichnen() {
      int condition = WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = getInputMap(condition);
      ActionMap actionMap = getActionMap();

      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
      actionMap.put(DOWN, new AbstractAction(DOWN) {
         {
            putValue(ACTION_COMMAND_KEY, DOWN);
         }

         @Override
         public void actionPerformed(ActionEvent evt) {
            System.out.println(evt.getActionCommand());
         }
      });
   }
}

你需要调用game.repaint(),因为你创建的匿名类没有名为repaint()的方法

That's because there's no method in any class in that structure (ie, the Frame → anonymous ActionListener ) that extends Component and has a repaint method. 那是因为在该结构中的任何类中都没有方法(即Frame →匿名ActionListener ),它扩展了Component并具有repaint方法。

You could either make your main Frame class extend JFrame , or call game.repaint() . 你可以让你的主Frame类扩展JFrame ,或者调用game.repaint() You probably want to do the former. 你可能想做前者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM