求java背景变色代码?
答案:2 悬赏:0 手机版
解决时间 2021-12-30 10:21
- 提问者网友:战皆罪
- 2021-12-29 22:56
求java背景变色代码?
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-12-29 23:29
说的不够细致
全部回答
- 1楼网友:北方的南先生
- 2021-12-30 00:27
setbackground设置背景色!可使用jpanel
布局随便选,下面程序使用了边框(borderlayout)和流水(flowlayout)2种布局方式!
顺便帮你加了一个按钮事件!有问题再追问吧!~
import java.awt.borderlayout;
import java.awt.color;
import java.awt.container;
import java.awt.flowlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
public class test extends jframe implements actionlistener {
private jpanel panel0 = null, panel2 = null;
private jbutton b1 = null, b2 = null, b3 = null, b4 = null;
public test() {
container c = this.getcontentpane();
//边框布局
c.setlayout(new borderlayout());
//创建panel
panel0 = new jpanel();
panel2 = new jpanel();
//为2个panel设置底色
panel0.setbackground(color.red);
panel2.setbackground(color.blue);
//2个panel都是用流水布局
panel0.setlayout(new flowlayout());
panel2.setlayout(new flowlayout());
//创建按钮
b1 = new jbutton("panel2黄色");
b2 = new jbutton("panel2绿色");
b3 = new jbutton("panel0橙色");
b4 = new jbutton("panel0灰色");
b1.addactionlistener(this);
b2.addactionlistener(this);
b3.addactionlistener(this);
b4.addactionlistener(this);
panel0.add(b1);
panel0.add(new jlabel());
panel0.add(b2);
panel2.add(b3);
panel2.add(b4);
c.add(panel0, borderlayout.center);
c.add(panel2, borderlayout.east);
this.setsize(500, 500);
this.setdefaultcloseoperation(jframe.exit_on_close);
this.setvisible(true);
}
public static void main(string[] args) {
new test();
}
@override
public void actionperformed(actionevent e) {
// todo auto-generated method stub
if (e.getsource() == b1) {
panel2.setbackground(color.yellow);
} else if (e.getsource() == b2) {
panel2.setbackground(color.green);
} else if (e.getsource() == b3) {
panel0.setbackground(color.orange);
} else if (e.getsource() == b4) {
panel0.setbackground(color.gray);
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯