【问题标题】:Changing colour of first JButton until second one has been clicked更改第一个 JButton 的颜色,直到单击第二个 JButton
【发布时间】:2017-10-13 05:24:49
【问题描述】:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;

public class ButtonsActionListener implements ActionListener {

    private JButton firstButton;
    private JButton secondButton;


    @Override
    public void actionPerformed(ActionEvent e) {
        if (firstClick == null) {
            firstClick = (JButton) e.getSource();
        } else {
            secondClick = (JButton) e.getSource();
            // Do something 
            firstClick = null;
            secondClick = null;
    }
}

}

此类记录用户单击的前两个 JButton。 firstButton 代表用户点击的第一个按钮, secondButton 代表用户点击的第二个按钮。

我希望当用户单击第一个 JButton 时,其颜色应变为红色,直到单击第二个 JButton。单击第二个 JButton 后,我希望第一个 JButton 的颜色变回原来的颜色。

我目前的实现有什么办法吗?

【问题讨论】:

    标签: java user-interface jbutton actionlistener


    【解决方案1】:

    要保留您当前的实现,请尝试这样的操作

    class ButtonsActionListener implements ActionListener {
    
        private JButton firstButton;
        private JButton secondButton;
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
        if (firstButton == null) {
                firstButton = (JButton) e.getSource();
                firstButton.setBackground(Color.RED);
            } else {
                if (firstButton == (JButton) e.getSource()) {
                    firstButton.setBackground(Color.RED);
                } else {
                    secondButton = (JButton) e.getSource();
                    firstButton.setBackground(null);// reset to original color                    
                }
            }
    
    
        }
    
    }
    

    【讨论】:

    • 之后我应该将 firstButton 和 secondButton 设置为 null 吗?
    • @JackKong 这取决于你的要求,你需要在改变颜色后清除按钮引用吗?
    • 获得第一个和第二个按钮后,我需要调用一个移动函数,该函数根据单击的两个按钮执行某些操作。
    • 如果你看看我的原始代码,我写“做某事”的部分,在那里我称之为我的移动函数。
    • @JackKong 我想你想将背景颜色设置为null以达到默认颜色
    【解决方案2】:

    单击第二个按钮后,您可以将背景颜色设置为默认值。最初,当单击第一个按钮时,如果单击第二个按钮,颜色会变为红色,则第一个按钮颜色会变回默认颜色。

    public static void main(String[] args) {
            final JButton button = new JButton("Click me");
            final JButton button2 = new JButton("Add");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    button.setBackground(Color.RED);
    
                }
            });
            button2.addActionListener( new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    button.setBackground(null);
                }
            });
        }
    

    【讨论】:

      【解决方案3】:

      要确定点击了哪个按钮并做出相应的响应,您可以这样做:

      class ButtonsActionListener implements ActionListener {
      
          @Override
          public void actionPerformed(ActionEvent e) {
      
                  if (((JButton) e.getSource()) == firstButton) {
                      firstButtonClicked();
                  } else if (((JButton) e.getSource()) == secondButton) {
                      secondButtonClicked();
                 }
          }
      
          private void firstButtonClicked(){
              System.out.println("1st button clicked ");
              //handle second button color 
          }
          private void secondButtonClicked(){
              System.out.println("2nd button clicked ");
              //handle second button color 
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-07
        • 2020-12-06
        • 1970-01-01
        • 2014-05-11
        • 2013-11-30
        • 1970-01-01
        相关资源
        最近更新 更多