public class JButtonTest extends JFrame{ public JButtonTest() { // TODO Auto-generated constructor stub Icon icon = new ImageIcon("src/huasacm.png"); 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) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null,"弹出对话框"); //弹出对话框 } }); c.add(jb); setSize(300,300); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { // TODO Auto-generated method stub new JButtonTest(); } }