如何通过使用空格键来制作这种替代颜色?

时间:2013-06-03 13:16:14

标签: java swing jpanel paintcomponent keyevent

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class TrafficLight extends JFrame implements ActionListener {
    JButton b1, b2, b3;

      Signal green = new Signal(Color.green);
      Signal yellow = new Signal(Color.yellow);
      Signal red = new Signal(Color.red);

    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        b1 = new JButton("Red");
        b2 = new JButton("Yellow");
        b3 = new JButton("Green");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);       

        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(true);

        JPanel p1 = new JPanel(new GridLayout(3,1));
        p1.add(red);
        p1.add(yellow);
        p1.add(green);
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);

        getContentPane().add(p1);
        getContentPane().add(p2);
        pack();
        }


    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();       
        tl.setVisible(true);
    }   
    public void actionPerformed(ActionEvent e){       
        if (e.getSource() == b1){
            green.turnOn(false);           
            yellow.turnOn(false);
            red.turnOn(true);
        } else if (e.getSource() == b2){
            yellow.turnOn(true);           
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == b3){
            red.turnOn(false);           
            yellow.turnOn(false);
            green.turnOn(true);
        }
    }
}    
class Signal extends JPanel{

    Color on;
    int radius = 40;
    int border = 10;
    boolean change;

    Signal(Color color){
        on = color;
        change = true;
    }

    public void turnOn(boolean a){
        change = a;
        repaint();       
    }

    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }

    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());

        if (change){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}

这会创建一个交通灯,其中有3个按钮,这些按钮对应于颜色,并使它们一次打开一个。如何通过使用空格键使其成为替代颜色?

目前,代码有3个不同的按钮,用户可以与之交互以确定交通信号灯照射的颜色。 我不确定如何使用空格键作为改变颜色的命令。

3 个答案:

答案 0 :(得分:3)

查看使用Key BindingsKeyStroke可以用于SPACE键,可以映射到Action以循环显示包含JButtonred和{{yellow的{​​{1}}数组1}}按钮并按顺序调用doClick

green

您可以使用模运算符JPanel content = (JPanel) getContentPane(); // from JFrame InputMap inputMap = content.getInputMap(); inputMap.put(KeyStroke.getKeyStroke("SPACE"), "pressed"); content.getActionMap().put("pressed", new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { currentButton = ++currentButton % buttons.length; buttons[currentButton].doClick(); } }); 将按钮索引保持在范围内。

%不同,KeyListeners不需要使用组件焦点,因此在开发Swing应用程序时应始终首选。

答案 1 :(得分:1)

实现KeyListener接口,然后使用此代码

public void keyPressed( KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE){

   //Your Light Changing Code Here
...

}

答案 2 :(得分:0)

听起来你需要一个KeyListener。它的行为与ActionListener非常相似,只是它专门为您的键盘设计。查看API:

http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyListener.html

各种keyEvents:

http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html