GUI-Swing-图片按钮
图片按钮
代码:
1 package com.luckylu.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.net.URL; 6 7 public class IconButtonDemo extends JFrame { 8 public IconButtonDemo() { 9 Container container = this.getContentPane(); 10 //将图片变为图标 11 URL url = IconButtonDemo.class.getResource("ICON.png"); 12 Icon icon = new ImageIcon(url); 13 14 //把图标放在按钮上 15 JButton jButton = new JButton(); 16 jButton.setIcon(icon); 17 jButton.setToolTipText("图片按钮"); 18 19 container.add(jButton); 20 21 this.setVisible(true); 22 this.setTitle("图片按钮示例"); 23 this.setBounds(200,200,300,300); 24 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 25 26 } 27 28 public static void main(String[] args) { 29 new IconButtonDemo(); 30 } 31 }
结果: