⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttontest.java

📁 中国水利水电出版社java程序设计教程,附源码
💻 JAVA
字号:
//ButtonTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest
{
    public static void main(String args[])
    {
        ButtonFrame buttonframe = new ButtonFrame();
        buttonframe.setDefaultCloseOperation(3);
        buttonframe.show();
    }
}

class ButtonFrame extends JFrame
{
    public ButtonFrame()
    {
        setTitle("点击按钮改变颜色");
        setSize(300, 200);
        ButtonPanel buttonpanel = new ButtonPanel();
        Container container = getContentPane();
        container.add(buttonpanel);
    }
}

class ButtonPanel extends JPanel
{
    private class ColorAction implements ActionListener
    {
        private Color backgroundColor;
        public void actionPerformed(ActionEvent actionevent)
        {
            setBackground(backgroundColor);
            repaint();
        }

        public ColorAction(Color color)
        {
            backgroundColor = color;
        }
    }

    public ButtonPanel()
    {
        JButton jbutton = new JButton("橙色");
        JButton jbutton1 = new JButton("蓝色");
        JButton jbutton2 = new JButton("红色");
        add(jbutton);
        add(jbutton1);
        add(jbutton2);
        ColorAction coloraction = new ColorAction(Color.orange);
        ColorAction coloraction1 = new ColorAction(Color.blue);
        ColorAction coloraction2 = new ColorAction(Color.red);
        jbutton.addActionListener(coloraction);
        jbutton1.addActionListener(coloraction1);
        jbutton2.addActionListener(coloraction2);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -