java swingä¸JLabelä¸æ·»å JButtonåªéè¦ä½¿ç¨JLabelçaddæ¹æ³å°±å¯ä»¥æ·»å ï¼å®ä¾å¦ä¸ï¼
package components;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JButtonTest extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
JButton button1,button2,button3;
public JButtonTest() {
//å建buttonä¸çå¾æ
ImageIcon imageIcon1 = createImage("images/right.gif");
ImageIcon imageIcon2 = createImage("images/middle.gif");
ImageIcon imageIcon3 = createImage("images/left.gif");
//å建Buttonï¼å¹¶è®¾ç½®Buttonçå¾æ
button1 = new JButton("ä¸é´æé®ä¸å¯ç¨",imageIcon1);
//设置Buttonçææ¬ä½ç½®
button1.setVerticalTextPosition(JButton.CENTER);
button1.setHorizontalTextPosition(JButton.LEADING);
//设置Buttonçå¿«æ·é®
button1.setMnemonic(KeyEvent.VK_D);
//设置Buttonçåé¦æ¶æ¯ï¼æ¶æ¯å¤çè
éè¿è¿ä¸ªæ è®°æ¥è¾¨å«æ¯åªä¸ªæé®è¢«ç¹å»
button1.setActionCommand("disable");
//为Buttonæ·»å çå¬è
button1.addActionListener(this);
//设置Buttonçæ示信æ¯
button1.setToolTipText("ç¹å»æ¤æé®ï¼æ¤æé®åä¸é´æé®å为ä¸å¯ç¨ï¼å³è¾¹æé®å为å¯ç¨");
//å°Buttonæ·»å å°panelä¸
add(button1);
}