首页 > 编程知识 正文

java点击按钮后按钮变颜色(java按钮点击改变颜色)

时间:2023-12-09 23:11:58 阅读:313899 作者:JPKW

本文目录一览:

如何改变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);

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