我的代码仅显示一种随机颜色,并具有一个获取新颜色的按钮,但是我该如何使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);
    }
}

最佳答案

该按钮应该已经对Enter键做出了反应,但是需要将其设置为框架的默认按钮,并且需要具有焦点,因此将以下两行添加到代码中即可正常使用至:

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)

09-11 05:55
查看更多