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

📄 buttonframe.java

📁 java第89章的实验,对学习java很有帮助的.
💻 JAVA
字号:
import java.awt.*;
import java.io.PrintStream;
import java.awt.event.*;
import javax.swing.*;

public class ButtonFrame extends SimpleFrame {
    public ButtonFrame(int width,int height) {
        super(width,height);
        setTitle("演示按钮事件");
        ColorPanel panel=new ColorPanel();
        Container contentPane=getContentPane();
        contentPane.add(panel);
    }

    public static void main(String[] args) {
       //设置程序观感
        try {
            UIManager.setLookAndFeel(windows);
        } catch (Exception e){
           System.err .println("程序异常:"+e.getMessage());
        }
        ButtonFrame frame = new ButtonFrame(400,300);
        frame.setVisible(true);
    }
    //可用于Windows平台的观感常量
    private static final String metal    =
            "javax.swing.plaf.metal.MetalLookAndFeel";
    private static final String motif    =
            "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    private static final String windows  =
            "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

}

class ColorPanel extends JPanel{
    public ColorPanel(){
        //创建组件
        colorButton = new JButton("随机颜色");
        darkerButton = new JButton("颜色变暗");
        brighterButton = new JButton("颜色变亮");
        colorText = new JTextArea(10, 30);
        //添加组件
        add(new JScrollPane(colorText));
        add(colorButton);
        add(darkerButton);
        add(brighterButton);
        //注册组件监听事件

        colorButton.addActionListener(new ColorActionListener());
        brighterButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                setBrighter();
            }
        });

        darkerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                setDarker();
            }
        });
    }

    private void setBrighter() {
        backgroundColor=backgroundColor.brighter();
        changeColor();
    }

    private void setDarker() {
        backgroundColor=backgroundColor.darker();
        changeColor();
    }

    private class ColorActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event){
            setColor();//设置面板颜色
        }
    }

    private void changeColor(){
        int r=backgroundColor.getRed();
        int g=backgroundColor.getGreen();
        int b=backgroundColor.getBlue();
        colorText.append("颜色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
        setBackground(backgroundColor);
    }

    private void setColor(){
        //产生随机颜色
        int r=(int)(Math.random()*255);
        int g=(int)(Math.random()*255);
        int b=(int)(Math.random()*255);
        colorText.append("颜色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
        backgroundColor=new Color(r,g,b);
        setBackground(backgroundColor);
    }

    private Color backgroundColor;
    private JButton colorButton;
    private JButton darkerButton ;
    private JButton brighterButton;
    private JTextArea colorText ;
}

⌨️ 快捷键说明

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