Java 是否更改自定义按钮上的背景色?

Java 是否更改自定义按钮上的背景色?,java,swing,Java,Swing,我希望我的按钮更改I%3的mod==0上的颜色。油漆组件。。。当窗体被重新调整大小并传入索引时调用,因此我认为每当我开始在屏幕上移动窗体时,这应该会更改按钮的颜色 我有两个组件在屏幕上,但都不会显示这可能是一个因素 代码: 但不需要第二个参数,如何传递它?我认为,与其尝试传递I,不如将I设置为类myButton的属性,并在实例化时将其初始化为0。也就是说,如果您希望每个按钮都有自己的计数器。这听起来是个更好的计划。你有很多奇怪的事情发生 您已经有了一个组件,在该组件中,您可以毫无理由地覆盖所有四

我希望我的按钮更改I%3的mod==0上的颜色。油漆组件。。。当窗体被重新调整大小并传入索引时调用,因此我认为每当我开始在屏幕上移动窗体时,这应该会更改按钮的颜色

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

代码:


但不需要第二个参数,如何传递它?我认为,与其尝试传递I,不如将I设置为类myButton的属性,并在实例化时将其初始化为0。也就是说,如果您希望每个按钮都有自己的计数器。这听起来是个更好的计划。

你有很多奇怪的事情发生

您已经有了一个组件,在该组件中,您可以毫无理由地覆盖所有四种主要的绘制方法。 在此组件中,paint方法重写调用super方法,并调用其他3个方法,这实际上会使这3个方法被调用两次。 在myButton的paintComponent方法中,您已经获得了程序逻辑i的进步——这是永远不应该做的事情。您无法完全控制何时或是否调用此方法。 你说的是挫折。。。从paintComponent内部,不应该做的事情。 您的类名不以大写字母开头,这违反了编码约定,可能会让试图阅读您的代码的人感到困惑。 如果要在调整大小时更改组件的状态,请使用ComponentListener。 e、 g


我在我的例子中改变了这一点。这样每个按钮都有自己的按钮。但是它仍然没有按照我认为应该的方式工作;。这是一个打字错误还是在你的真实代码中?这更多的是为了测试学习,而不是在现实世界中应该如何做。我想看看油漆的各个部分。我知道每个JComponent都有一个调用Update的画图。@DougHauf:但是为什么这些方法要调用两次呢?毫无意义。那么你到底想做什么呢?@DougHauf:请参阅“编辑以回答”部分,该部分展示了如何在GUI上更改JButton的背景。
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");
    }
}
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();
         }
      });
   }
}