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

时间:2014-05-06 19:54:20

标签: java swing

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

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

代码:

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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

  • 你有一个组件可以无缘无故地覆盖所有四种主要的绘画方法。
  • 在这个组件中,你的paint方法覆盖调用super方法,并调用其他3个方法,这实际上将使这3个方法被调用两次。
  • 你的myButton的paintComponent方法中有程序逻辑(i的推进) - 这是永远不应该做的事情。您无法完全控制何时或甚至是否调用此方法。
  • 您正在从paintComponent中调用setBackground(...),这是不应该做的事情。
  • 您的类名不是以大写字母开头,违反编码惯例,并且可能会使试图阅读您的代码的任何人感到困惑。
  • 如果要在调整大小时更改组件的状态,请使用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();
         }
      });
   }
}