日期:2014-05-20  浏览次数:20790 次

麻烦大家看我的结果为什么会这样
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.检查图片绝对路径是否正确。
2.检查图片是否太小,显示出来的是一个点。xx.setSize(300, 300);

------解决方案--------------------
Java code
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);

    }

------解决方案--------------------
你把尺寸调一下就应该出来了吧?或者不具有问题
------解决方案--------------------
果断设计那个Panel为边界布局。。流式布局有时候显示不出来