首页 > 编程 > Java > 正文

java组件更改颜色方法

2019-11-06 06:56:34
字体:
来源:转载
供稿:网友

关键代码:new JButton().setBackground(new Color(255,255,255));

                  new JButton().setBackground(Color.RED);
示例:import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.WindowConstants;public class MyChessCover2 extends JFrame {	public static int k=8;	public MyChessCover2(){		Container co=getContentPane();		JPanel p1=new JPanel(new GridLayout(k,k,1,1));				JButton jb[]=new JButton[k*k]; 		int r,g,b;		for(int i=0;i<(k*k);i++){			 r=(int)(Math.random()*100);			 g=(int)(Math.random()*100);			 b=(int)(Math.random()*100);			jb[i]=new JButton();			jb[i].setBackground(new Color(r,g,b));			p1.add(jb[i]);		}		co.add(p1);		setSize(300,300);		setResizable(false);		setVisible(true);		setTitle("颜色设置");		setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);	}	public static void main(String[] args) {		// TODO Auto-generated method stub		new MyChessCover2();	}}结果如图:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表