java - 如何仅使用空格键来制作替代颜色?

标签 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 个不同的按钮,用户可以与它们交互以确定交通信号灯的颜色。 我不确定如何使用空格键作为更改颜色的命令。

最佳答案

看看使用Key Bindings 。一个KeyStroke可用于可映射到 Action 的 SPACE 键循环遍历 JButton包含 red 的数组, yellowgreen按钮并调用 doClick按顺序。

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不同, Key Bindings不需要组件焦点即可工作,因此在开发 Swing 应用程序时应始终首选。

关于java - 如何仅使用空格键来制作替代颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16897652/

相关文章:

java - 如何在 JFrame 中分隔面板以分隔类

c# - 内部录音程序

java - 如何让线程按顺序工作并多次运行?

swing - clojure doto 中的普通实例方法调用

java - 如何在 java swing 中设置 Filepicker 以仅选择特定文件?例如。仅限以 .mo 为扩展名的文件

java - 当我单击文本字段并弹出时,是否有 Java Swing 的日期选择器?

java - 在 Java 中实现新按钮

java - 克隆 JFrame

java - servlet java 抛出的自定义 503 错误页面

java - java新手: getting unexpected output