本文目錄一覽:
- 1、如何改變java按鈕中的顏色?
- 2、java 單擊按鈕改變背景顏色
- 3、java改變按鈕顏色
- 4、java怎麼做點擊一個按鈕彈出一個顏色選擇窗格改變文本區文字顏色?
- 5、java如何改變按鈕的顏色,不是背景的顏色
如何改變java按鈕中的顏色?
setForeground() 設置前景/字體顏色
setBackground() 設置背景顏色
具體實現:(假設按鈕名稱為:button)
設置紅字:
button.setForeground(Color.red);
設置黑色背影:
button.setBackground(Color.black);
java 單擊按鈕改變背景顏色
public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand().equals(“紅色”){((Button)evt.getSource()).setBackground(Color.red);} else if(evt.getActionCommand().equals(“藍色”){((Button)evt.getSource()).setBackground(Color.blue);} else if(evt.getActionCommand().equals(“黃色”){((Button)evt.getSource()).setBackground(Color.yellow);} }
記得採納啊
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怎麼做點擊一個按鈕彈出一個顏色選擇窗格改變文本區文字顏色?
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如何改變按鈕的顏色,不是背景的顏色
setForeground() 設置前景/字體顏色
setBackground() 設置背景顏色
具體實現:(假設按鈕名稱為:button)
設置紅字:
button.setForeground(Color.red);
設置黑色背影:
button.setBackground(Color.black);
原創文章,作者:UESW3,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129847.html