日期:2014-05-20 浏览次数:21017 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
    private JButton btn = new JButton();
    
    public TestFrame()
    {
        this.setLayout(new GridBagLayout());    //貌似需要GridBagLayout布局,不然btn旁边会有东西
        
        btn.setIcon(new ImageIcon("up.jpg"));    //初始时的图片
        btn.setPressedIcon(new ImageIcon("down.jpg")); //按下去的图片
        btn.setBorder(BorderFactory.createEmptyBorder());    //必须把边界设为空,否则btn四周会有东西
        add(btn);
    }
    
    public static void main(String[] args)
    {
        JFrame frame = new TestFrame();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}