Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请问为什么我显示不了图片
io567n





发贴: 4
积分: 0
于 2004-11-29 19:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我想要编一个显示图片的程序,但加了其他控件后,一运行图片却显示不出来,我到底哪里错了。哪位大侠能否指点一下。我的程序如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ImageQuery extends JFrame
{

  private JPanel contentPane;

  private ImagePanel fillImagePanel=new ImagePanel();

  private CheckboxGroup checkboxGroup1=new CheckboxGroup();
  private Checkbox checkboxColor=new Checkbox();
  private Checkbox checkboxShape=new Checkbox();
  private Checkbox checkboxContent=new Checkbox();

  private Button buttonBrowse=new Button("浏览...");
  private Button buttonQuery=new Button("查询");

  private TextField textContent=new TextField("");

  private MenuFrame menuframe=new MenuFrame();

  private FileDialog filedialogImage=new FileDialog(this,"打开文件");

  public ImageQuery()
  {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    contentPane=(JPanel)this.getContentPane();
    contentPane.setLayout(null);
  

    this.setSize(new Dimension(400,300));
    this.setTitle("图像查询");

    checkboxColor.setCheckboxGroup(checkboxGroup1);
    checkboxColor.setLabel("颜色");
    checkboxColor.setState(true);
    checkboxShape.setCheckboxGroup(checkboxGroup1);
    checkboxShape.setLabel("形状");
    checkboxContent.setCheckboxGroup(checkboxGroup1);
    checkboxContent.setLabel("内容");
    
    textContent.setEnabled(false);

    buttonBrowse.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        buttonBrowse_actionPerformedEnvelope;
      }
    });

    buttonQuery.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        buttonQuery_actionPerformedEnvelope;
      }
    });

    checkboxColor.addItemListener(new java.awt.event.ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
        checkboxColor_itemStateChangedEnvelope;
      }
    });

    checkboxShape.addItemListener(new java.awt.event.ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
        checkboxShape_itemStateChangedEnvelope;
      }
    });

    checkboxContent.addItemListener(new java.awt.event.ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
        checkboxContent_itemStateChangedEnvelope;
      }
    });

    contentPane.add(fillImagePanel);
    fillImagePanel.setBounds(120,30,100,100);    

    contentPane.add(checkboxColor);
    checkboxColor.setBounds(30,30,40,30);

    contentPane.add(checkboxShape);
    checkboxShape.setBounds(30,80,40,30);

    contentPane.add(buttonBrowse);
    buttonBrowse.setBounds(90,60,60,25);

    contentPane.add(checkboxContent);
    checkboxContent.setBounds(30,140,40,30);

    contentPane.add(textContent);
    textContent.setBounds(80,145,100,20);

    contentPane.add(buttonQuery);
    buttonQuery.setBounds(150,200,60,25);

    setMenuBar(menuframe.menubar);
  }

  //设置框架窗体的事件监听(关闭窗体事件)
  protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEventEnvelope;
    if(e.getID()==WindowEvent.WINDOW_CLOSING)
    {
      System.exit(0);
    }
  }

  void buttonBrowse_actionPerformed(ActionEvent e)
  {
    filedialogImage.show();
  }

  void buttonQuery_actionPerformed(ActionEvent e)
  {
  
  }

  void checkboxColor_itemStateChanged(ItemEvent e)
  {
    textContent.setEnabled(false);
    buttonBrowse.setEnabled(true);
  }

  void checkboxShape_itemStateChanged(ItemEvent e)
  {
    textContent.setEnabled(false);
    buttonBrowse.setEnabled(true);
  }

  void checkboxContent_itemStateChanged(ItemEvent e)
  {
    textContent.setEnabled(true);
    buttonBrowse.setEnabled(false);
    textContent.requestFocus();
  }
}

class ImagePanel extends JPanel
{
public ImagePanel()
{
image=Toolkit.getDefaultToolkit().getImage("E:/ZY/My Pictures/CA2NWXEV.jpg");
MediaTracker tracker=new MediaTracker(this);//把一个图像加入跟踪器对象
tracker.addImage(image,0);
try {tracker.waitForID(0);}
catch(InterruptedException e){}
}
public void paintComponent(Graphics g)//对内装文件图像的对象进行引用
{
super.paintComponentPresent;
g.drawImage(image,0,0,this);//引用图像对象
}
private Image image;
}

class MenuFrame extends Frame
{

MenuBar menubar=new MenuBar();
private Menu menuFile=new Menu("File");
  private MenuItem menuitemOpen=new MenuItem("Open");
  private MenuItem menuitemNew=new MenuItem("New");
  private MenuItem menuitemSave=new MenuItem("Save");
  private MenuItem menuitemExit=new MenuItem("Exit");
private Menu menuOption=new Menu("Option");
private Menu menuHelp=new Menu("Help");
  private MenuItem menuitemAbout=new MenuItem("About");
MenuFrame()
{
  menuFile.add(menuitemOpen);
  menuFile.add(menuitemNew);
  menuFile.add(menuitemSave);
  menuFile.add(menuitemExit);
  /*添加Option的菜单项*/
  menuHelp.add(menuitemAbout);
  menubar.add(menuFile);
  menubar.add(menuOption);
  menubar.add(menuHelp);
}
public void actionPerformed(ActionListener e)
{
}
}
  

public class Frame
{
public static void main(String args[]){
ImageQuery iq=new ImageQuery();
//iq.setBackground(Color.pink);
iq.show();
}
}




致JAVA初学者+如何下手学JAVA

话题树型展开
人气 标题 作者 字数 发贴时间
9470 请问为什么我显示不了图片 io567n 5144 2004-11-29 19:53

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923