2013-03-11 49 views
1

编辑:现在它工作正常,但是当我切换到“游戏”的JPanel,该KeyListener的不工作:(任何方式来解决这个中的ActionListener不能重绘

我只是编程? java游戏和repaint()方法有问题: 我想从菜单到游戏,所以我删除菜单面板并添加游戏面板(“Zeichnen”扩展JPanel)但每次我尝试重新绘制,日食给我下面的错误:“该方法重绘()是未定义的类型新的ActionListener(){}”谁能帮我:(

代码:

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

} 
} 
+0

见回答:你应该使用的,而不是一个关键监听键绑定。 – 2013-03-11 22:01:46

回答

2

Suggestsions:

  • 您应该使用CardLayout有一个容器交换组件,你正在做的,因为这是改变观点的最简单,最可靠的方式。在这个网站的先前的问题中有很多这样的例子。
  • 不要使用KeyListener,而要使用Key Bindings,因为它们在组件焦点方面更加宽容。
  • 将您的“Frame”类重命名为与核心Java类不匹配的类。这是一个非常令人困惑的名字。

例如:

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

和密钥绑定:

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

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

+0

感谢它的工作,但我不得不使用重新验证:) – 2013-03-11 21:29:52

1

这是因为有一个在任何类(没有方法在结构即Frame →匿名ActionListener),其延伸Component并具有repaint方法。

您可以让您的主Frame类延伸JFrame,或致电game.repaint()。你可能想要做前者。

+0

感谢上帝你救了我的一天,我差点扔我的笔记本在我的墙上,因为我无法找出问题... – 2014-05-29 18:07:52