簡體   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