java编写设置按钮随机背景色_java用SWT实现按钮按一下窗体背景换一个随机颜色...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package hello;

import org.eclipse.swt.*;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.events.SelectionListener;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

import org.eclipse.swt.graphics.Color;

public class SWTtrainning{

public static void main (String[] args) {

Display display=new Display();

Shell shell=new Shell();

Button button=new Button(shell, SWT.PUSH);//在shell上添加一个按钮,并选中按钮类型为PUSH。

shell.setVisible(true);//窗口可见

shell.setBounds(250,0, 900,800);//设置窗口位置和长宽

shell.setText("实验窗口");

button.setBounds(20, 20,20, 20);//设置按钮大小

shell.setBackground(new Color(Display.getCurrent(),80,200,51));//设置窗口背景颜色。

Text text1 = new Text(shell, SWT.BORDER );//shell中添加文本框,并设置文本框格式。

text1.setBackground(new Color(Display.getCurrent(),255,255,0));//设置文本框背景颜色。

text1.setText("");

text1.setBounds(50,50,400,50);

text1.setVisible(true);

Label label1 = new Label(shell, SWT.SINGLE);

label1.setText("red");

label1.setBounds(20, 50, 20, 50);

SelectionListener listener = null ;

button.addSelectionListener(listener);;//按钮被选中时,改变窗口背景颜色。

while (button.getSelection()) {

listener=new SelectionAdapter(){

public void widgetSelected( SelectionEvent event ) {

int a =(int) Math.random()*224;

int b =(int) Math.random()*224;

int c =(int) Math.random()*224;

Color color =new Color(Display.getCurrent(),a,b,c) ;

shell.setBackground(color);;

text1.setText("hello"); }

};

};

while(!shell.isDisposed()) {

}

while (!shell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

display.dispose();

}}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值