我如何让ActionListener听一键按?

时间:2016-11-28 20:39:15

标签: java swing actionlistener

我的代码只是显示一个随机颜色,并有一个按钮来获取一个新颜色,但我怎么能让ActionListener响应按 Enter

另外,如何使按钮不填充JFrame

的整个宽度

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomColour {

    public static JFrame frame;

    public static Color[] colours = {Color.RED, Color.YELLOW, Color.ORANGE, Color.BLUE, Color.GREEN, Color.BLACK, Color.MAGENTA, Color.CYAN, Color.PINK,};

    public static int random;

    public static void main(String[] args) {


        random = (int)(Math.random()*(9)+0);

        JButton button = new JButton ("New Random Colour");

        button.setSize(10, 10);

        frame = new JFrame ("Random Colour");
        JPanel panel = new JPanel(new GridLayout(6,6,6,6));

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().setBackground(colours[(int)(Math.random()*(9)+0)]);
            }
        });


        panel.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setContentPane(panel);
        frame.getContentPane().setBackground(colours[random]);
        frame.setLocationRelativeTo(null);
    }
}

2 个答案:

答案 0 :(得分:1)

该按钮已经应该对Enter按键作出反应,但我需要将 t设置为您的框架的默认按钮,并且需要具有焦点,因此请添加以下两行你的代码,它将按照你期望的那样工作:

frame.getRootPane().setDefaultButton(button);
button.requestFocus();

下面更详细的说明:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomColour {
    public static JFrame frame;

    public static Color[] colours = {Color.RED, Color.YELLOW, Color.ORANGE, Color.BLUE, Color.GREEN, Color.BLACK, Color.MAGENTA, Color.CYAN, Color.PINK,};

    public static int random;

    public static void main(String[] args) {


        random = (int)(Math.random()*(9)+0);

        JButton button = new JButton ("New Random Colour");

        button.setSize(10, 10);

        frame = new JFrame ("Random Colour");
        frame.setLayout(new GridLayout(3,2));

        JPanel panel = new JPanel(new GridLayout(3,2));

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().setBackground(colours[(int)(Math.random()*(9)+0)]);
            }
        });

        panel.add(button);

        // The changes are the next two lines
        frame.getRootPane().setDefaultButton(button);
        button.requestFocus();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setContentPane(panel);
        frame.getContentPane().setBackground(colours[random]);
        frame.setLocationRelativeTo(null);
    }
}

Swing使用布局管理器为您处理组件演示和排列,如果您想要更灵活,则应考虑使用GridBagLayout Example here或设计更复杂的布局合并现有的布局管理器(例如默认的BorderLayoutManager和GridLayout)

答案 1 :(得分:0)

所以你有两个选择,

您可以复制已编写的整个动作侦听器代码,并以相同的方式将其添加到回车按钮。

... OR

您可以将动作侦听器变为命名对象(示例)

gremlin> g.addV('name', 'Alice')
==>v[0]
gremlin> g.V().out().hasNext()
==>false
gremlin> g.V().hasNext()
==>true

然后

ActionListener thing = new ActionListener(...)

将两者都粘在一起。