导航:首页 > 编程语言 > java改变按钮的颜色

java改变按钮的颜色

发布时间:2022-05-29 15:50:04

java 点击按纽改变背景颜色

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;

//本类继承JFrame,实现了ActionListener接口
{

intr=90;
intg=15;
intb=195;

publicMyFrame(){
//组件的初始化
JButtonjbRed=newJButton("red");
jbRed.setLocation(20,80);//按钮位置
jbRed.setSize(80,40);//按钮大小
jbRed.addActionListener(this);//添加点击按钮后的事件响应,因为本类实现了ActionListener接口,所以可以传入参数this

JButtonjbGreen=newJButton("green");
jbGreen.setLocation(120,80);
jbGreen.setSize(80,40);
jbGreen.addActionListener(this);

JButtonjbBlue=newJButton("blue");
jbBlue.setLocation(220,80);
jbBlue.setSize(80,40);
jbBlue.addActionListener(this);

//添加组件到窗口
add(jbRed);
add(jbGreen);
add(jbBlue);

//窗口的设置
setLayout(null);//因为每一个按钮都设置了位置和大小,那么应该把窗口设置为空布局,那么位置和大小才能有效
setTitle("窗口标题");
getContentPane().setBackground(newColor(r,g,b));//设置窗口的面板背景色
setLocation(220,160);//窗口位置
setSize(320,240);//窗口大小
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮时,结束程序

//下面也可以实现,点击关闭按钮时,结束程序
addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){//点击关闭按钮会触发这个事件,调用这个方法
System.out.println("通过WindowListener实现关闭");
System.exit(0);//退出
}
});
}

publicvoidactionPerformed(ActionEvente){
Stringcmd=e.getActionCommand();
//通过ActionCommand来判断是哪一个按钮被点击了
if("red".equals(cmd)){//如果是红色按钮被点击了,那么红色+10
r+=10;
if(r>255){//如果red大于255,可以设置为0,也可以设置为255,一直锁定为255也可设置为初始的90,这里题目这里没有要求
r=90;
}
}elseif("green".equals(cmd)){
g+=10;
if(g>255){
g=15;
}
}elseif("blue".equals(cmd)){
b+=10;
if(b>255){
b=195;
}
}
this.getContentPane().setBackground(newColor(r,g,b));
//System.out.println(this.getContentPane().getBackground());
}

publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
newMyFrame().setVisible(true);//启动窗口并设置可见
}
});
}
}

② 怎样改变java中按钮的字体和颜色

submit= new JButton("登陆");

submit.setFont(new Font("宋体", Font.PLAIN, 16));
三个参数分别表示: 字体,样式(粗体,斜体等),字号

submit.setForeground(Color.RED);
这个表示给组件上的文字设置颜色Color.RED表示红色
当然你也可以自己给RGB的值 比如 submit.setForeground(new Color(215,215,200));

JLabel组件支持HTML标记代码
infoLab= new JLabel("<html><a href='地址'>用户登陆系统</a></html>", JLabel.CENTER);

*注意:地址要单引号引起来。这个表示给用户登录系统几个字增加超链接
infoLab .setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

这个表示给这个文字添加鼠标样式,当鼠标移动到文字上,鼠标变成手型

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

submit= new JButton("登陆");

submit.setFont(new Font("宋体", Font.PLAIN, 16));
三个参数分别表示: 字体,样式(粗体,斜体等),字号

submit.setForeground(Color.RED);
这个表示给组件上的文字设置颜色Color.RED表示红色
当然你也可以自己给RGB的值 比如 submit.setForeground(new Color(215,215,200));

JLabel组件支持HTML标记代码
infoLab= new JLabel("<html><a href='地址'>用户登陆系统</a></html>", JLabel.CENTER);

*注意:地址要单引号引起来。这个表示给用户登录系统几个字增加超链接
infoLab .setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

这个表示给这个文字添加鼠标样式,当鼠标移动到文字上,鼠标变成手型

④ JAVA初学,想改变多个按键的颜色,可是没法改变

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
public class Jewels extends JFrame implements ActionListener {

//set up a board
// a locked up board's constructer
public Jewels(){
this(8,10,4);
}
// a flexibe board's constructer
public Jewels(int rows, int columns, int numJewels) {
setTitle("Jewels");
// setBackground(Color.green);
setBounds(100,100,300,300);
JButton[] buttons = new JButton[rows*columns];
Random r=new Random();
this.getContentPane().setLayout(new GridLayout(rows,columns,3,3));
for(int i = 0; i<rows*columns;i++){
buttons[i] = new JButton();
this.getContentPane().add(buttons[i]);
buttons[i].addActionListener(this);
//set button colors,使颜色随机变化
buttons[i].setBackground(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
}
this.setVisible(true);
}
public static void main(String[] args){
// Jewels haha = new Jewels();
Jewels haha = new Jewels(8,8,3);
haha.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

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

setForeground() 设置前景/字体颜色
setBackground() 设置背景颜色
具体实现:(假设按钮名称为:button)
设置红字:
button.setForeground(Color.red);
设置黑色背影:
button.setBackground(Color.black);

⑥ java怎么做点击一个按钮弹出一个颜色选择窗格改变文本区文字颜色

1、示例代码

public class ColorFrame extends JFrame {

private Container container; //容器

private JPanel colorPanel; //用于反映颜色变化的面板


public ColorFrame() { //构造函数

super( "调色板演示" ); //调用JFrame的构造函数

container = getContentPane(); //得到容器

colorPanel=new JPanel(); //初始化面板


JButton selectColorButton = new JButton( "选取颜色" ); //初始化颜色选择按钮

selectColorButton.addActionListener( //为颜色选择按钮增加事件处理

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

JColorChooser chooser=new JColorChooser(); //实例化颜色选择器

Color color=chooser.showDialog(ColorFrame.this,"选取颜色",Color.lightGray ); //得到选择的颜色

if (color==null) //如果未选取

color=Color.gray; //则设置颜色为灰色

colorPanel.setBackground(color); //改变面板的背景色

}


});

container.add(selectColorButton,BorderLayout.NORTH); //增加组件

container.add(colorPanel,BorderLayout.CENTER); //增加组件

setSize( 400, 130 ); //设置窗口尺寸

setVisible(true); //设置窗口可见

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); //关闭窗口时退出程序

}


public static void main(String args[]) {

new ColorFrame();

}

}

2、效果

⑦ java swing中按钮的颜色要怎么设置

1.1olor

1.1.1new Color(red, green,blue);

red, green,blue: 0~255

swing基础概念教程:

⑧ 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中 点击选择按钮,从33个按钮中随机选取七个按钮改变其颜色 怎么写啊

将33个按钮放到一个对象数组里面。然后用取7个随机数代表数组的序号,用这个序号到数组中获取按钮对象,然后改变颜色。获取随机数的代码如下:
Random random1 = new Random(33);
for(int i=0;i<7;i++){
int index=random1.nextInt();
}

阅读全文

与java改变按钮的颜色相关的资料

热点内容
可编程软件分为哪两种 浏览:340
格林什么app可以看 浏览:697
飞卢app仙侠热卖推荐怎么样 浏览:722
飞秋上传文件到共享文件夹 浏览:691
服务器的共享文件夹如何访问 浏览:232
复盛螺杆压缩机讲解 浏览:332
柱在基础插筋需要加密吗 浏览:80
51单片机中断寄存器 浏览:65
压缩文件后有病毒怎么办 浏览:618
苹果ipad怎么登安卓王者账号 浏览:862
街头足球服务器已满是什么意思 浏览:462
androidspeex回音消除 浏览:133
加密会议什么意思 浏览:34
ubuntu命令行联网 浏览:7
37选7中奖概率及算法 浏览:593
加密狗哪个版本好 浏览:619
cisco命令手册 浏览:502
omp算法c 浏览:129
上海浦东机场源码 浏览:885
扣扣加密了忘记密码如何打开 浏览:508