package java1;import javax.swing.*;
public class JButton1 {    public static void main(String[] args) {
       JFrame jf=new JFrame();
       JPanel jp=new JPanel();
       jf.add(jp);
       JButton yes=new JButton("yes",new ImageIcon("smile.gif"));
       JButton no=new JButton("no",new ImageIcon("cry.gif"));
       jp.add(yes);
       jp.add(no);
       jf.pack();
       jf.setVisible(true);
       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }

怎么没有笑面和哭面的按钮的?是不是要设置按钮的大小的?

解决方案 »

  1.   

    1.检查图片绝对路径是否正确。
    2.检查图片是否太小,显示出来的是一个点。xx.setSize(300, 300);
      

  2.   

    public static void main(String[] args) {
    JPanel jp = new JPanel();
    jp.setLayout(null);
    JButton yes = new JButton("yes", new ImageIcon(JButton1.class.getResource("/smile.gif")));
    yes.setBounds(2,2,100, 30);
    JButton no = new JButton("no", new ImageIcon(JButton1.class.getResource("/cry.gif")));
    no.setBounds(155,2,100, 30);
    jp.add(yes);
    jp.add(no);

    JFrame jf = new JFrame();
    jf.setBounds(12, 12, 300,100);
    jf.add(jp);
    jf.validate();
    jf.setVisible(true); }
      

  3.   

    果断设计那个Panel为边界布局。。流式布局有时候显示不出来