繁体   English   中英

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

[英]Change the background color on a custom Button?

我希望我的按钮在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");
    }
}

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

你发生了很多奇怪的事情...

  • 您有一个无缘无故地覆盖所有四种主要绘画方法的组件。
  • 在此组件中,您的paint方法重写将调用super方法,并调用其他3个方法,从本质上讲,这3个方法将被调用两次。
  • 在myButton的paintComponent方法中已经有了程序逻辑( i前进),这是不应该做的。 您无法完全控制何时甚至调用此方法。
  • 您是从paintComponent内调用setBackground(...) ,这是不应该做的。
  • 您的类名不是以大写字母开头,这违反了编码约定,并可能使尝试阅读您的代码的任何人困惑。
  • 如果要在调整大小时更改组件的状态,请使用ComponentListener。

例如

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();
         }
      });
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM