public class JButtonTest extends JFrame{
public JButtonTest(){
URL url=JButtonTest.class.getResource("imageButtoo.jpg");
Icon icon=new ImageIcon(url);
setLayout(new GridLayout(3, 2,5,5));
Container c=getContentPane();
for (int i = 0; i < 5; i++) {
JButton j=new JButton("Button"+i,icon);
c.add(j);
if (i%2==0) {
j.setEnabled(false);
}
}
JButton jb=new JButton();
jb.setMaximumSize(new Dimension(90, 30));
jb.setIcon(icon);
jb.setHideActionText(true);
jb.setToolTipText("图片按钮");
jb.setBorderPainted(false);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"弹出对话框");
}
});
c.add(jb);
setTitle("按钮案例");
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest();
}
}
原文:http://www.cnblogs.com/douyunqian3520/p/5517965.html