javase swing

package com.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame implements ActionListener{

    private JPanel jPanel;
    private JButton jb1, jb2;
    public Test() {
        this.jPanel = new JPanel();
        
        ImageIcon imageIcon = new ImageIcon("bdlogo.gif");
        
        this.jb1 = new JButton(imageIcon);
        this.jb1.addActionListener(this);
        this.jb1.setActionCommand("black");
        
        this.jb2 = new JButton("WHITE");
        this.jb2.addActionListener(this);
        this.jb2.setActionCommand("white");
        
        this.add(this.jb1, BorderLayout.NORTH);
        this.add(this.jPanel, BorderLayout.CENTER);
        this.add(this.jb2, BorderLayout.SOUTH);
        
        this.setSize(300, 300);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    public static void main(String[] args) {
        new Test();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if("black".equals(command)) {
            this.jPanel.setBackground(Color.BLACK);
        } else {
            this.jPanel.setBackground(Color.WHITE);
        }
    }

}

 

posted @ 2013-10-11 08:38  High阔天空  阅读(278)  评论(0编辑  收藏  举报