首页 > 编程知识 正文

java按钮颜色设置问题,java设置按钮背景颜色

时间:2023-12-28 11:57:09 阅读:328348 作者:NRAJ

本文目录一览:

java中如何设置按钮文字的大小、颜色和字体?

submit=newJButton("登陆");x0dx0ax0dx0asubmit.setFont(newFont("宋体",Font.PLAIN,16));x0dx0a三个参数分别表示:字体,样式(粗体,斜体等),字号x0dx0ax0dx0asubmit.setForeground(Color.RED);x0dx0a这个表示给组件上的文字设置颜色Color.RED表示红色x0dx0a当然你也可以自己给RGB的值比如submit.setForeground(newColor(215,215,200));x0dx0ax0dx0aJLabel组件支持HTML标记代码x0dx0ainfoLab=newJLabel("用户登陆系统",JLabel.CENTER);x0dx0ax0dx0a*注意:地址要单引号引起来。这个表示给用户登录系统几个字增加超链接x0dx0ainfoLab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));x0dx0ax0dx0a这个表示给这个文字添加鼠标样式,当鼠标移动到文字上,鼠标变成手型

如何改变java按钮中的颜色?

setForeground() 设置前景/字体颜色

setBackground() 设置背景颜色

具体实现:(假设按钮名称为:button)

设置红字:

button.setForeground(Color.red);

设置黑色背影:

button.setBackground(Color.black);

JAVA中的JButton的背景颜色设置问题。用的是MacBook pro,用eclipse写的。

mac os 默认的border是aqua border(look and feel设置),这个border是一个灰白色填充外加灰色边界的矩形,在初始化button的时候默认是显示border的,所以你会发现这个aqua border layer把你的button覆盖了。此时button上的图层从底向上分别是backgroundforegroundborder layer.所以你看到的灰白色其实是你的默认border。

设置button颜色的解决有两种,你可以设置background颜色,然后设置button foreground透明,border层不显示。

button.setBackground(Color.RED);//设置background颜色

button.setOpaque(true); //foreground设置透明

button.setBorderPainted(false); //最后显示红色

或者设置foreground颜色,这样会把background盖住,然后设置border层不显示。

button.setBackGround(Color.RED); // 随便设,反正会被盖

button.setForeGround(Color.BLUE); //盖住背景色

button.setBorderPainted(false); //最后显示蓝色

另外,你也可以自己设置一个lineBorder代替mac os默认的aqua border。

originBorder=BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1);//这个是windows的默认border

button.setForeGround(Color.BLUE);

button.setBorder(originBorder);//最后会显示一个蓝色,带浅灰边框的button

这个问题在window是不存在的,因为window的默认border是swing border,是无填充透明边界,不会盖住foreground layer和background layer。所以你在windows情况下只要设置好前景色背景色的覆盖关系就行了(比如设置button透明并设置背景色,或者直接设置前景色)。

覆盖关系,覆盖关系,覆盖关系。重说三。

另外mac第三种解决方式:自己网上关键字查找mac java swing look and feel,下载对应的包加入自己的库,然后在swing的window或者panel主函数第一句先:

try{

UIManager.setLookAndFeel(#这里是对应的look and feel包名字#);

}catch(Exception e){e.printStackTrace()}

不过我不推荐这个做法,因为换地方的时候没有库会异常,然后使用默认的lookandfeel。

java改变按钮颜色

为yellow、blue、red3个按钮添加actionlistener,当按钮点击时执行setBackground(backgroundColor),同时执行 按钮.setBackground(backgroundColor)即可,比如:

JButton btnYellow = null;

JButton btnBlue = null;

JButton btnRed = null;

btnYellow.setActionCommand("yellow");

btnBlue.setActionCommand("blue");

btnRed.setActionCommand("red");

public void actionPerformed(ActionEvent e) {

String command = event.getActionCommand();

if( "yellow".equals(command) ){

setBackground(Color.yellow);

btnYellow.setBackground(Color.yellow);

}else if( "blue".equals(command) ){

setBackground(Color.blue);

btnBlue.setBackground(Color.blue);

}else if( "red".equals(command) ){

setBackground(Color.red);

btnRed.setBackground(Color.red);

}

}

写出了部分代码

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。