带有JFrame和JButtons的Java ActionListener

时间:2018-05-12 14:48:38

标签: java swing jframe jbutton actionlistener

可以告诉我为什么我的ActionListener无法正常工作吗? 我用JFrame创建了一个widnow,用JButton创建了一些Buttons。

是否需要JPanel?

我想更改窗口的背景颜色。这不起作用。如果我尝试buttonz[0].setBackground(Color.red);,只有按钮的颜色会发生变化。但我想改变窗口的背景。

public class ColorButtons implements Runnable {

    public static void main(String args[]) {
        new ColorButtons().init();
    }


    public void init()  { 
 //Creating window

        JFrame frame = new JFrame();
        frame.setVisible (true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000,400);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER,100,100));

//creating buttons

        JButton[] buttonz = {
            new JButton("Red"),
            new JButton("Yellow"),
            new JButton("Blue"),
            new JButton("Green")};

        //adding my buttons

        for (int i = 0; i < buttonz.length; i++) {
            frame.add(buttonz[i]);
            final int j = i;

            buttonz[i].addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    switch (j) {
                        case 0:
                            frame.setBackground(Color.red);
                            break;
                        case 1:
                            frame.setBackground(Color.yellow);
                            break;
                        case 2:
                            frame.setBackground(Color.blue);
                            break;
                        default:
                            frame.setBackground(Color.green);
                            break;
                    }
                }
            });
        }
    }

    @Override
    public void run() {

    }

}

1 个答案:

答案 0 :(得分:1)

您需要设置JFrame的contentPane的颜色,而不是JFrame本身,因为它实际上是持有您的JButton的contentPane。所以:

@Override
public void actionPerformed(ActionEvent e) {
    switch (j) {
    case 0:
        frame.getContentPane().setBackground(Color.red);
        break;
    case 1:
        frame.getContentPane().setBackground(Color.yellow);
        break;
    case 2:
        frame.getContentPane().setBackground(Color.blue);
        break;
    default:
        frame.getContentPane().setBackground(Color.green);
        break;
    }
}

考虑

  • 将所有内容放入自己的JPanel并着色JPanel
  • 让按钮的听众更聪明&#34;通过让他们保持您想要更改背景的颜色,例如:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColorButtons2 extends JPanel {

    public ColorButtons2() {
        int ebGap = 80;
        setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        setLayout(new GridLayout(1, 0, ebGap/2, 0));
        add(new JButton(new ColorAction("Red", Color.RED)));
        add(new JButton(new ColorAction("Yellow", Color.YELLOW)));
        add(new JButton(new ColorAction("Blue", Color.BLUE)));
        add(new JButton(new ColorAction("Green", Color.GREEN)));
    }

    private class ColorAction extends AbstractAction {
        private Color color;

        public ColorAction(String name, Color color) {
            super(name);
            this.color = color;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            setBackground(color);
        }
    }

    private static void createAndShowGui() {
        ColorButtons2 mainPanel = new ColorButtons2();

        JFrame frame = new JFrame("ColorButtons2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}