更改自定义按钮的背景颜色?

时间:2022-11-20 22:56:32

I want my button to change color on the mod == 0 of i % 3. The paintComponent(...) is called when the form is re-sized and index is passed in so I would think that this should change the color of my button ever time I start moving the form around the screen.

我希望我的按钮在i = 3的mod == 0上更改颜色。当重新调整表单大小并传入索引时,将调用paintComponent(...),因此我认为这应该会改变颜色我的按钮有时我开始在屏幕上移动表格。

I have two components on the screen but both will not show up this might be a factor.

我在屏幕上有两个组件,但两个都不会显示,这可能是一个因素。

Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testform {
    public static void main (String[] p) {
         testBall3 j1  = new testBall3();
         myButton  b1  = new myButton("test");


         JPanel testPane = new JPanel();
         testPane.setBackground(Color.green);
         testPane.setLayout(new BorderLayout());
         j1.setPreferredSize(new Dimension(10,10));
         //testPane.add(b1);
         testPane.add(j1);

         JFrame frame = new JFrame("Testing");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setLayout(new BorderLayout());
         frame.add(testPane);
         frame.pack();
         frame.setSize(300, 200); 
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);

         //j1.setColorBall(Color.BLACK);
         //j1.repaint();
    }
}

    class myButton extends JButton {
    private static final long serialVersionUID = 1L;

    public myButton(String s) {
        super(s);   
    }

    public void setPrefferedSize(Dimension d) {
        //this.setBounds(x, y, width, height)
        setPreferredSize(d);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        index += i;
        System.out.println(i);
        if (index % 3 == 0) {
          setBackground(Color.RED);
        }
        else {
            setBackground(Color.BLACK);
        }
    }
}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

class testBall3 extends JComponent
{
    private static final long serialVersionUID = 1L;
    private Color colorBall = Color.red;
    private int x1, y1;
    int index = 0;

    public void setColorBall(Color c)
    {
        this.colorBall = c;
    }

    public testBall3() 
    { 
        super();
        System.out.println("MyBall (0)");
    }

    public testBall3(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        System.out.println("MyBall (1)");
        x1 = x;
        y1 = y;
    }

    public void paintBorder(Graphics g) 
    {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(colorBall);
        g.fillOval(x1, y1, 10, 10);
        System.out.println("paintComponent");
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        paintComponent(g);
        paintBorder(g);
        paintChildren(g);
        System.out.println("Paint");
    }
}

2 个解决方案

#1


2  

But paintComponent doesn't take a second parameter, how are you passing it? I would think that instead of trying to pass i, you would want make i an attribute of class myButton instead and initialize it to 0 upon instantiation. That is, if you want each button to have its own counter. That sounds like the better plan.

但是paintComponent没有采取第二个参数,你是如何传递它的?我认为不是试图传递i,而是希望make i是类myButton的属性,并在实例化时将其初始化为0。也就是说,如果您希望每个按钮都有自己的计数器。这听起来像是更好的计划。

#2


2  

You've got a lot of strange stuff going on...

你有很多奇怪的事情......

  • You've got a component where you override all four major painting methods for no good reason.
  • 你有一个组件,你无缘无故地覆盖所有四种主要的绘画方法。

  • In this component, your paint method override calls the super method, and calls the other 3 methods, which in essence will make those 3 methods be called twice.
  • 在这个组件中,你的paint方法覆盖调用super方法,并调用其他3个方法,这实际上将使这3个方法被调用两次。

  • You've got program logic (advancement of i) inside of your myButton's paintComponent method -- something that should never be done. You do not have full control over when or even if this method is called.
  • 你的myButton的paintComponent方法中有程序逻辑(i的推进) - 这是永远不应该做的事情。您无法完全控制何时或甚至是否调用此方法。

  • You are calling setBackground(...) from within paintComponent, something which shouldn't be done.
  • 您正在paintComponent中调用setBackground(...),这是不应该做的事情。

  • Your class names do not begin with an upper case letter, going against coding conventions, and potentially confusing anyone who tries to read your code.
  • 您的类名不是以大写字母开头,违反编码约定,并且可能会使试图阅读代码的任何人感到困惑。

  • If you want to change the state of a component on resize, use a ComponentListener.
  • 如果要在调整大小时更改组件的状态,请使用ComponentListener。


e.g.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class Foo2 extends JPanel {
   protected static final Color MAGIC_BACKGROUND = Color.red;
   protected static final int MAGIC_NUMBER = 3;
   private static final int TIMER_DELAY = 20;
   private int index = 0;
   private JButton myButton = new JButton("My Button");
   protected int DELTA_SIZE = 2;

   public Foo2() {
      add(myButton);
      addComponentListener(new ComponentAdapter() {

         @Override
         public void componentResized(ComponentEvent e) {
            index++;
            if (index % MAGIC_NUMBER == 0) {
               myButton.setBackground(MAGIC_BACKGROUND);
            } else {
               myButton.setBackground(null);
            }
         }

      });

      new Timer(TIMER_DELAY, new ActionListener() {
         private Toolkit toolkit = Toolkit.getDefaultToolkit();
         private int screenWidth = toolkit.getScreenSize().width;
         private int screenHeight = toolkit.getScreenSize().height;

         @Override
         public void actionPerformed(ActionEvent e) {
            if (getWidth() >= screenWidth || getHeight() >= screenHeight) {
               ((Timer)e.getSource()).stop();
            } else {
               int width = getWidth() + DELTA_SIZE;
               int height = getHeight() + DELTA_SIZE;
               setPreferredSize(new Dimension(width, height));
               Window win = SwingUtilities.getWindowAncestor(Foo2.this);
               win.pack();
               win.setLocationRelativeTo(null);
            }
         }
      }).start();
   }

   private static void createAndShowGui() {

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

#1


2  

But paintComponent doesn't take a second parameter, how are you passing it? I would think that instead of trying to pass i, you would want make i an attribute of class myButton instead and initialize it to 0 upon instantiation. That is, if you want each button to have its own counter. That sounds like the better plan.

但是paintComponent没有采取第二个参数,你是如何传递它的?我认为不是试图传递i,而是希望make i是类myButton的属性,并在实例化时将其初始化为0。也就是说,如果您希望每个按钮都有自己的计数器。这听起来像是更好的计划。

#2


2  

You've got a lot of strange stuff going on...

你有很多奇怪的事情......

  • You've got a component where you override all four major painting methods for no good reason.
  • 你有一个组件,你无缘无故地覆盖所有四种主要的绘画方法。

  • In this component, your paint method override calls the super method, and calls the other 3 methods, which in essence will make those 3 methods be called twice.
  • 在这个组件中,你的paint方法覆盖调用super方法,并调用其他3个方法,这实际上将使这3个方法被调用两次。

  • You've got program logic (advancement of i) inside of your myButton's paintComponent method -- something that should never be done. You do not have full control over when or even if this method is called.
  • 你的myButton的paintComponent方法中有程序逻辑(i的推进) - 这是永远不应该做的事情。您无法完全控制何时或甚至是否调用此方法。

  • You are calling setBackground(...) from within paintComponent, something which shouldn't be done.
  • 您正在paintComponent中调用setBackground(...),这是不应该做的事情。

  • Your class names do not begin with an upper case letter, going against coding conventions, and potentially confusing anyone who tries to read your code.
  • 您的类名不是以大写字母开头,违反编码约定,并且可能会使试图阅读代码的任何人感到困惑。

  • If you want to change the state of a component on resize, use a ComponentListener.
  • 如果要在调整大小时更改组件的状态,请使用ComponentListener。


e.g.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class Foo2 extends JPanel {
   protected static final Color MAGIC_BACKGROUND = Color.red;
   protected static final int MAGIC_NUMBER = 3;
   private static final int TIMER_DELAY = 20;
   private int index = 0;
   private JButton myButton = new JButton("My Button");
   protected int DELTA_SIZE = 2;

   public Foo2() {
      add(myButton);
      addComponentListener(new ComponentAdapter() {

         @Override
         public void componentResized(ComponentEvent e) {
            index++;
            if (index % MAGIC_NUMBER == 0) {
               myButton.setBackground(MAGIC_BACKGROUND);
            } else {
               myButton.setBackground(null);
            }
         }

      });

      new Timer(TIMER_DELAY, new ActionListener() {
         private Toolkit toolkit = Toolkit.getDefaultToolkit();
         private int screenWidth = toolkit.getScreenSize().width;
         private int screenHeight = toolkit.getScreenSize().height;

         @Override
         public void actionPerformed(ActionEvent e) {
            if (getWidth() >= screenWidth || getHeight() >= screenHeight) {
               ((Timer)e.getSource()).stop();
            } else {
               int width = getWidth() + DELTA_SIZE;
               int height = getHeight() + DELTA_SIZE;
               setPreferredSize(new Dimension(width, height));
               Window win = SwingUtilities.getWindowAncestor(Foo2.this);
               win.pack();
               win.setLocationRelativeTo(null);
            }
         }
      }).start();
   }

   private static void createAndShowGui() {

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}