java 点击按纽改变背景颜色

Python011

java 点击按纽改变背景颜色,第1张

分析题目:

一 分析布局: 题目明确的指出了按钮的位置和大小 ,那么说明需要使用的布局是空布局(绝对布局) , 而JFrame窗口的内容面板默认布局是边界布局(BorderLayout),所以需要设置一下

setLayout(null)//设置为绝对布局

二了解颜色. Color 可以通过红,绿,蓝 三原色, 不同的搭配, 形成不同的颜色.

每个原色的取值范围是0~255, 比如红色的rgb值就是r=255,g=0,b=0

胡萝卜色 r=237,g=145,b=33

三添加颜色 ,java给JFrame添加颜色,比较特殊. 必须添加到内容面板上,才能正常显示(因为JFrame分了好多层)

getContentPane().setBackground(new Color(r,g,b))//设置窗口的面板背景色

四 事件处理分析: 点击按钮,会触发ActionEvent 事件,这个事件会被ActionListener 接收器接收到, 只需要重写ActionListener 里的actionPerformed 方法, 即可实现点击按钮后,做某件事

五 具体参考代码

import java.awt.*

import java.awt.event.*

import javax.swing.*

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

public class MyFrame extends JFrame implements ActionListener{

int r = 90

int g = 15

int b = 195

public MyFrame() {

//组件的初始化

JButton jbRed = new JButton("red")

jbRed.setLocation(20, 80)//按钮位置

jbRed.setSize(80, 40)//按钮大小

jbRed.addActionListener(this)//添加点击按钮后的事件响应 ,因为本类实现了ActionListener接口,所以可以传入参数this

JButton jbGreen = new JButton("green")

jbGreen.setLocation(120, 80)

jbGreen.setSize(80, 40)

jbGreen.addActionListener(this)

JButton jbBlue = new JButton("blue")

jbBlue.setLocation(220, 80)

jbBlue.setSize(80, 40)

jbBlue.addActionListener(this)

//添加组件到窗口

add(jbRed)

add(jbGreen)

add(jbBlue)

//窗口的设置

setLayout(null)//因为每一个按钮都设置了位置和大小, 那么应该把窗口设置为空布局, 那么位置和大小才能有效

setTitle("窗口标题")

getContentPane().setBackground(new Color(r,g,b))//设置窗口的面板背景色

setLocation(220, 160)// 窗口位置

setSize(320, 240)// 窗口大小

//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//点击关闭按钮时,结束程序

//下面也可以实现,点击关闭按钮时, 结束程序

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {//点击关闭按钮会触发这个事件,调用这个方法

System.out.println("通过WindowListener实现关闭")

System.exit(0)//退出

}

})

}

public void actionPerformed(ActionEvent e) {

String cmd=e.getActionCommand()

//通过ActionCommand 来判断是哪一个按钮被点击了

if("red".equals(cmd)) {//如果是红色按钮被点击了,那么红色+10

r+=10

if(r>255) {//如果red大于255 ,可以设置为0 ,也可以设置为255,一直锁定为255 也可设置为初始的90,这里题目这里没有要求

r=90

}

}else if("green".equals(cmd)) {

g+=10

if(g>255) {

g=15

}

}else if("blue".equals(cmd)){

b+=10

if(b>255) {

b=195

}

}

this.getContentPane().setBackground(new Color(r,g,b))

//System.out.println(this.getContentPane().getBackground())

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new MyFrame().setVisible(true)//启动窗口并设置可见

}

})

}

}

setBackground设置背景色!可使用JPanel

布局随便选,下面程序使用了边框(BorderLayout)和流水(FlowLayout)2种布局方式!

顺便帮你加了一个按钮事件!有问题再追问吧!~

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Container

import java.awt.FlowLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

public class Test extends JFrame implements ActionListener {

    private JPanel panel0 = null, panel2 = null

    private JButton b1 = null, b2 = null, b3 = null, b4 = null

    public Test() {

        Container c = this.getContentPane()

        //边框布局

        c.setLayout(new BorderLayout())

        //创建panel

        panel0 = new JPanel()

        panel2 = new JPanel()

        //为2个panel设置底色

        panel0.setBackground(Color.red)

        panel2.setBackground(Color.BLUE)

        //2个panel都是用流水布局

        panel0.setLayout(new FlowLayout())

        panel2.setLayout(new FlowLayout())

        //创建按钮

        b1 = new JButton("panel2黄色")

        b2 = new JButton("panel2绿色")

        b3 = new JButton("panel0橙色")

        b4 = new JButton("panel0灰色")

        /**

         * 添加按钮事件

         */

        b1.addActionListener(this)

        b2.addActionListener(this)

        b3.addActionListener(this)

        b4.addActionListener(this)

        /**

         * 将按钮添加相应panel上

         */

        panel0.add(b1)

        panel0.add(new JLabel())

        panel0.add(b2)

        panel2.add(b3)

        panel2.add(b4)

        /**

         * 将panel添加到容器

         */

        c.add(panel0, BorderLayout.CENTER)

        c.add(panel2, BorderLayout.EAST)

        this.setSize(500, 500)

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        this.setVisible(true)

        

    }    

    public static void main(String[] args) {

        new Test()    

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        if (e.getSource() == b1) {

            panel2.setBackground(Color.yellow)

        } else if (e.getSource() == b2) {

            panel2.setBackground(Color.green)

        } else if (e.getSource() == b3) {

            panel0.setBackground(Color.ORANGE)

        }  else if (e.getSource() == b4) {

            panel0.setBackground(Color.GRAY)

        } 

    }

}