当前位置: 代码迷 >> J2SE >> 纠错~ 请各位大神 帮帮忙,该怎么处理
  详细解决方案

纠错~ 请各位大神 帮帮忙,该怎么处理

热度:521   发布时间:2016-04-24 02:26:39.0
纠错~ 请各位大神 帮帮忙
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JButtonDemo extends JFrame {
private JButton button1, button2;
public JButtonDemo(){
super("ce shi an zhuang");
setSize(400,200);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exception e){}
Container container=getContentPane();
container.setLayout(new GridLayout(1,2));
container.setBackground(Color.RED);

button1= new JButton("按钮一");
button1.setFont(new Font("Serif",Font.PLAIN,12));
ImageIcon img1=new ImageIcon("source/6009.gif");
ImageIcon img2=new ImageIcon("");
button2=new JButton("按钮二",img1);
button2.setRolloverIcon(img2);
button2.setFont(new Font("Serif",Font.PLAIN, 12));

ButtonHandler handler=new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);

container.add(button1);
container.add(button2);


setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
  public static void main(String args[]){
  JButtonDemo demo=new JButtonDemo();
  demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
  }
  public class ButtonHandler implements ActionListener{
  public void actionPerformed(ActionEvent event){
  JOptionPane.showMessageDialog(JButtonDemo.this,
  "你按了:"+event.getActionCommand());
  }
  }
}
我插入图片在里面 但是为什么不显示出来呢!! 我补充一点 就是我把那张图片 放在了跟这个程序的一个目录下!!

------解决方案--------------------
你把6009.gif放在跟这个JButtonDemo.java一个目录下
代码改成这样
Java code
package csdn.p36;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class JButtonDemo extends JFrame {    private JButton button1, button2;    public JButtonDemo(){        super("ce shi an zhuang");        setSize(400,200);        try{            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        }catch(Exception e){}        Container container=getContentPane();        container.setLayout(new GridLayout(1,2));        container.setBackground(Color.RED);        button1= new JButton("按钮一");        button1.setFont(new Font("Serif",Font.PLAIN,12));        ImageIcon img1=new ImageIcon(this.getClass().getResource("6009.gif"));//这里修改        ImageIcon img2=new ImageIcon("");        button2=new JButton("按钮二",img1);        button2.setRolloverIcon(img2);        button2.setFont(new Font("Serif",Font.PLAIN, 12));        ButtonHandler handler=new ButtonHandler();        button1.addActionListener(handler);        button2.addActionListener(handler);        container.add(button1);        container.add(button2);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String args[]){        JButtonDemo demo=new JButtonDemo();        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public class ButtonHandler implements ActionListener{        public void actionPerformed(ActionEvent event){            JOptionPane.showMessageDialog(JButtonDemo.this,                    "你按了:"+event.getActionCommand());        }    }}
  相关解决方案