单击按钮时如何更改背景颜色
How to change Background color when i click a button
我的问题只是改变的一小部分 color.I 当我点击那些 buttons.I 搜索时我想改变整个背景 google 没有任何反应。
我使用面板,但它似乎不仅可以改变一小部分我想要整个背景。
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
/**
*
*
* @author Christopher Porras
* @Version 0.1
* @Doing GUI
*/
public class Button extends JFrame {
private JButton bred;
private JButton bblue;
private JButton bgreen;
private JPanel mousepanel;
public Button()
{
super("ChangeColor");
setLayout(new FlowLayout());
setSize(200,200);
mousepanel = new JPanel();
mousepanel.setBackground(Color.white);
add(mousepanel);
bred = new JButton("REd");
add(bred);
bblue = new JButton("Blue");
add(bblue);
bgreen = new JButton("Green");
add(bgreen);
thehandler handler = new thehandler();
bred.addMouseListener(handler);
bblue.addMouseListener(handler);
bgreen.addMouseListener(handler);
}
private class thehandler implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
//To change body of generated methods, choose Tools | Templates.
}
}
public static void main(String[]args)
{
Button button = new Button();
button.setVisible(true);
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
您的方向是正确的,但是您需要将事件绑定到按钮组件而不是鼠标的中断信号。
代替 MouseListener,将 theHandler
更改为:
public class TheActionHandler implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
}
我的问题只是改变的一小部分 color.I 当我点击那些 buttons.I 搜索时我想改变整个背景 google 没有任何反应。
我使用面板,但它似乎不仅可以改变一小部分我想要整个背景。
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
/**
*
*
* @author Christopher Porras
* @Version 0.1
* @Doing GUI
*/
public class Button extends JFrame {
private JButton bred;
private JButton bblue;
private JButton bgreen;
private JPanel mousepanel;
public Button()
{
super("ChangeColor");
setLayout(new FlowLayout());
setSize(200,200);
mousepanel = new JPanel();
mousepanel.setBackground(Color.white);
add(mousepanel);
bred = new JButton("REd");
add(bred);
bblue = new JButton("Blue");
add(bblue);
bgreen = new JButton("Green");
add(bgreen);
thehandler handler = new thehandler();
bred.addMouseListener(handler);
bblue.addMouseListener(handler);
bgreen.addMouseListener(handler);
}
private class thehandler implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
//To change body of generated methods, choose Tools | Templates.
}
}
public static void main(String[]args)
{
Button button = new Button();
button.setVisible(true);
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
您的方向是正确的,但是您需要将事件绑定到按钮组件而不是鼠标的中断信号。
代替 MouseListener,将 theHandler
更改为:
public class TheActionHandler implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
}