2014-02-19 19 views
-1

有一些错误,我找不到它。
当我使KeyPressed类的对象工作时,但它们中的每一个都将颜色设置为Color.Blue而不是黄色和绿色(取决于哪个快捷键被按下)。
有趣的是,短工作,但他们都设置颜色为蓝色。我不知道为什么。感谢帮助。Java,构造函数生成的一个变量就像常量,尽管其他2不是

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.InputEvent; 
import java.awt.event.KeyEvent; 

import javax.swing.AbstractAction; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 

public class Frame extends JFrame { 

private JPanel buttonPanel = new JPanel(); 
InputMap imap = buttonPanel 
     .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 
ActionMap amap = buttonPanel.getActionMap(); 

public Frame(int width, int height, String title) { 
    setSize(width, height); 
    setTitle(title); 

    MakeButtons yellow = new MakeButtons("Yellow", Color.YELLOW); 
    MakeButtons green = new MakeButtons("Green", Color.GREEN); 
    MakeButtons blue = new MakeButtons("Blue", Color.BLUE); 

    add(buttonPanel); 

    KeyPressed yellowAction = new KeyPressed(KeyEvent.VK_Y, 
      InputEvent.CTRL_DOWN_MASK, Color.YELLOW); 
    KeyPressed greenAction = new KeyPressed(KeyEvent.VK_G, 
      InputEvent.CTRL_DOWN_MASK, Color.GREEN); 
    KeyPressed blueAction = new KeyPressed(KeyEvent.VK_B, 
      InputEvent.CTRL_DOWN_MASK, Color.BLUE); 

    System.out.println(yellowAction.getClass().getDeclaredFields()); 


} 

class MakeButtons extends JButton { 

    public MakeButtons(String nameOfTheButton, final Color bcolor) { 
     JButton button = new JButton(nameOfTheButton); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       buttonPanel.setBackground(bcolor); 
      } 
     }); 
     button.setText(nameOfTheButton); 
     buttonPanel.add(button); 

    } 
} 

class KeyPressed extends AbstractAction { 

    public void actionPerformed(ActionEvent event) { 
    } 

    public KeyPressed(final int keyEvent, final int inputEvent, 
      final Color kcolor) { 

     KeyStroke keyAbbreviation = KeyStroke.getKeyStroke(keyEvent, 
       inputEvent); 
     imap.put(keyAbbreviation, "temp"); 

     amap.put("temp", new AbstractAction() { 
      public void actionPerformed(ActionEvent event) { 
       buttonPanel.setBackground(kcolor); 
      } 

     }); 

    } 


} 

}

+2

这里有很多事情没有意义。为什么你使用“temp”作为每个动作的关键?为什么MakeButton扩展JButton? –

回答

2

我认为这就是问题所在:

imap.put(keyAbbreviation, "temp"); 

amap.put("temp", new AbstractAction() { 
    public void actionPerformed(ActionEvent event) { 
     buttonPanel.setBackground(kcolor); 
    } 

}); 

您使用"temp"作为actionMapKey所有三个输入,但你覆盖了什么行动应该做的每一时间。你想要三个不同的动作。您可能使用:

imap.put(keyAbbreviation, kcolor); 

amap.put(kcolor, new AbstractAction() { 
    public void actionPerformed(ActionEvent event) { 
     buttonPanel.setBackground(kcolor); 
    } 
}); 

...但我强烈劝你重新考虑你的整个设计。这是一个可怕的建设者滥用副作用,首先...

相关问题