繁体   English   中英

ActionListener和JButton

[英]ActionListeners and JButtons

我正在尝试编写一个有3个不同按钮的程序,当您单击一个按钮时,它将更改框架中面板的背景。 我已经设置好面板,一切都在正确的位置,但是我需要将所有用于按钮的动作监听器都放在一个类中。 我尝试这样做,但是所有按钮均更改颜色,而不更改背景。 这是我到目前为止的代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Kelle Schmitt
 */
public class BackgroundColorChooserFrame extends JFrame
{
    private static final int WIDTH = 300;
    private static final int HEIGHT = 300;

    private JLabel titleLbl;
    private JButton redBtn;
    private JButton greenBtn;
    private JButton blueBtn;
    private JButton quitBtn;

    public BackgroundColorChooserFrame()
    {
        createButton();
        createLabel();
        createPanel();

        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class ColorListener implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
            redBtn.setBackground(Color.red);
            greenBtn.setBackground(Color.green);
            blueBtn.setBackground(Color.blue);
        }
    }

    public void createButton()
    {
        redBtn = new JButton("Red");
        greenBtn = new JButton("Green");
        blueBtn = new JButton("Blue");

        ActionListener colorlistener = new ColorListener();
        redBtn.addActionListener(colorlistener);
        greenBtn.addActionListener(colorlistener);
        blueBtn.addActionListener(colorlistener);
    }

    public void createLabel()
    {
        titleLbl = new JLabel("Background Color Chooser");
    }

    //create and add panels
    public void createPanel()
    {
        JPanel mainPnl, titlePnl, colorPnl, controlPnl;

        mainPnl = new JPanel();
        mainPnl.setLayout(new BorderLayout());
        titlePnl = new JPanel();
        colorPnl = new JPanel();
        controlPnl = new JPanel();

        mainPnl.add(titlePnl, BorderLayout.NORTH);
        titlePnl.add(titleLbl);
        mainPnl.add(colorPnl, BorderLayout.CENTER);
        mainPnl.add(controlPnl, BorderLayout.SOUTH);
        controlPnl.add(redBtn);
        controlPnl.add(greenBtn);
        controlPnl.add(blueBtn);

        //add the mainPnl to the parent frame
        add(mainPnl);

    }


}

请帮忙! 谢谢!

也许像这样:

public void actionPerformed(ActionEvent evt)
{
    JButton button = (JButton)evt.getSource();
    Component parent = button.getParent();

    if (button == redBtn)
        parent.setBackground( Color.RED );
    else if (...)
}

尽管更好的解决方案是为每个按钮创建一个单独的ActionListener,以便您不使用嵌套的if / else逻辑:

public class ColorListener implements ActionListener
{
    private Color background;

    public ButtonListener(Color background)
    {
        this.background = background;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)evt.getSource();
        Component parent = button.getParent();
        parent.setBackground( background );   
    }
}

然后,您可以创建无限数量的按钮和颜色:

redButton.addActionListener( new ColorListener(Color.RED) );

关键在于使用getSource()方法,因此您可以编写通用代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM