本篇文章给大家谈谈java修改button颜色,以及java设置按钮颜色对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、用java代码改变xml文件中的button按钮的背景颜色2、java中如何设置按钮文字的大小、颜色和字体?3、java改变按钮颜色4、JAVA中的JButton的背景颜色设置问题。用的是MacBook pro,用eclipse写的。
用java代码改变xml文件中的button按钮的背景颜色
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
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;
class Frame1 extends JFrame // 设计一个窗体类
{
JButton jb1, jb2, jb3; // 按钮
JLabel jl; // 标签
Frame1() // 构造方法
{
jb1 = new JButton(“红色”); // 创建按钮对象
jb2 = new JButton(“绿色”);
jb3 = new JButton(“蓝色”);
// 给jb1添加监听事件
jb1.addActionListener(new ActionListener() {
// 覆写actionPerformed方法
@Override
public void actionPerformed(ActionEvent e) {
jl.setForeground(Color.RED);
}
});
// 给jb2添加监听事件
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jl.setForeground(Color.GREEN);
}
});
// 给jb3添加监听事件
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jl.setForeground(Color.BLUE);
}
});
jl = new JLabel(“设定标签颜色”); // 创建标签对象
jl.setFont(new Font(“隶书”, Font.PLAIN, 32)); // 标签字体
JPanel jp1 = new JPanel(); // 面板1
JPanel jp2 = new JPanel(); // 面板2
jp1.add(jl); // 标签放入面板1
jp2.add(jb1); // 三个按键放入面板2
jp2.add(jb2);
jp2.add(jb3);
setLayout(new BorderLayout()); // 窗体设定为边界布局
this.add(jp1, BorderLayout.NORTH); // 标签面板放在窗体上端
this.add(jp2, BorderLayout.CENTER); // 按钮面板放在窗体中间
setBounds(400, 300, 300, 140); // 设定窗体大小和位置
setTitle(“【实验10-1】座号25,王涛\n”); // 设定窗体标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // 设定窗体可见
}
}
public class Experiment10_1 {
public static void main(String[] args) {
new Frame1();
}
}
java中如何设置按钮文字的大小、颜色和字体?
submit=newJButton(“登陆”);\x0d\x0a\x0d\x0asubmit.setFont(newFont(“宋体”,Font.PLAIN,16));\x0d\x0a三个参数分别表示:字体,样式(粗体,斜体等),字号\x0d\x0a\x0d\x0asubmit.setForeground(Color.RED);\x0d\x0a这个表示给组件上的文字设置颜色Color.RED表示红色\x0d\x0a当然你也可以自己给RGB的值比如submit.setForeground(newColor(215,215,200));\x0d\x0a\x0d\x0aJLabel组件支持HTML标记代码\x0d\x0ainfoLab=newJLabel(“用户登陆系统”,JLabel.CENTER);\x0d\x0a\x0d\x0a*注意:地址要单引号引起来。这个表示给用户登录系统几个字增加超链接\x0d\x0ainfoLab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));\x0d\x0a\x0d\x0a这个表示给这个文字添加鼠标样式,当鼠标移动到文字上,鼠标变成手型
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);
}
}
写出了部分代码
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修改button颜色和java设置按钮颜色的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。