zoukankan      html  css  js  c++  java
  • java 背景颜色更改

    简介

    java 事件处理机制

    code

    /*
     * @Author: your name
     * @Date: 2020-10-28 22:38:26
     * @LastEditTime: 2020-10-29 10:01:15
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/ImageTest.java
     */
    import java.awt.*;
    
    import javax.swing.*;
    
    public class ImageTest {
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                JFrame frame = new ButtonFrame();
                frame.setTitle("ImageTest");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            });
            ;
        }
    }
    
    
    /*
     * @Author: your name
     * @Date: 2020-10-29 09:41:06
     * @LastEditTime: 2020-10-29 10:01:27
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/ButtonFrame.java
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonFrame extends JFrame {
        private JPanel buttonPanel;
        private static final int DEFAULT_WIDTH = 300;
        private static final int DEFAULT_HEIGHT = 200;
    
        public ButtonFrame() {
    
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
            // create button
            JButton yellowButton = new JButton("Yellow");
            JButton blueButton = new JButton("Blue");
            JButton redButton = new JButton("Red");
    
            buttonPanel = new JPanel();
    
            // add buttons to panel
            buttonPanel.add(yellowButton);
            buttonPanel.add(blueButton);
            buttonPanel.add(redButton);
    
            add(buttonPanel);
    
            // create button actions
            ColorAction yellowAction = new ColorAction(Color.YELLOW);
            ColorAction blueAction = new ColorAction(Color.BLUE);
            ColorAction redAction = new ColorAction(Color.RED);
    
            // associate actions with buttons
            yellowButton.addActionListener(yellowAction);
            blueButton.addActionListener(blueAction);
            redButton.addActionListener(redAction);
        }
    
        private class ColorAction implements ActionListener {
            private Color backgroundColor;
    
            public ColorAction(Color c) {
                backgroundColor = c;
            }
    
            public void actionPerformed(ActionEvent event) {
                buttonPanel.setBackground(backgroundColor);
            }
        }
    }
    

    image

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    程序是访问数据库的组件
    Mac搭建Vue
    Sharding-JDBC基本使用,整合Springboot实现分库分表,读写分离
    随意写写
    maven打包工具assembly打包本地jar的解决办法
    Go语言笔记 (2) 变量命名与多重赋值
    Go语言笔记(1)变量的定义与赋值
    c期末笔记(4)
    c期末笔记(3)
    c期末笔记(2)
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13895168.html
Copyright © 2011-2022 走看看