JButton增加图标

来源:互联网 发布:鸳鸯 知乎 编辑:程序博客网 时间:2024/05/23 02:12
package String;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;/** *  * @author 李林君 * @version 2013-6-22下午5:13:38 */public class JButtonDemo {public static void main(String[] args) {JFrame f = new JFrame("jbiconfm");Container contentPane = f.getContentPane();JButton b = new JButton("click", new ImageIcon(".\\icons\\handle.png"));// 如果没有设置文字的位置,系统默认会将文字放置于图形的右边中间位置。b.setHorizontalTextPosition(JButton.CENTER);b.setVerticalTextPosition(JButton.BOTTOM);b.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});contentPane.add(b);f.pack();f.show();f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});}}




原创粉丝点击