读书人

JButton里有没有更改按钮上图片的set方

发布时间: 2012-04-11 17:42:33 作者: rapoo

JButton里有没有更改按钮上图片的set方法?
求助!!

[解决办法]

Java code
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);    }} 

读书人网 >J2SE开发

热点推荐