導航:首頁 > 編程語言 > 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改變按鈕的顏色相關的資料

熱點內容
數控機床fanuc編程 瀏覽:607
天刀mode不是內部或外部命令 瀏覽:854
長城c30壓縮機價格 瀏覽:1000
java打開圖片文件 瀏覽:409
跟程序員聊天聊到半夜 瀏覽:411
自己怎麼做app代碼 瀏覽:915
win7旗艦版進不去帶命令符 瀏覽:799
單片機溫度檢測電路 瀏覽:802
拼圖軟體不壓縮 瀏覽:656
紅袖添香小說源碼 瀏覽:624
erp加密工具在哪裡買 瀏覽:516
怎麼給qq群里的文件加密 瀏覽:762
androidsetbitmap 瀏覽:597
mt4反向編譯 瀏覽:201
sun伺服器命令 瀏覽:827
程序員同乘電梯 瀏覽:617
49乘以235的簡便演算法 瀏覽:673
新概念51單片機c語言教程光碟 瀏覽:262
伺服器分區如何選擇 瀏覽:354
php官方網站 瀏覽:592