请各位告诉下这几道JAVA上机题的做法?

  2 .请完成下列Java程序:用位于JFrame窗口下方的Open按钮,打开一个文件对话框,检索.gif或者.png图形文件,并在窗口中央将图象显示出来.(提示:将图象文件嵌入图象标内,再将图像图标置于标签中)
   注意:请勿改动main主方法和其他已有语句内容,仅在下划线处填入适当的语句.
  import java.awt.*;
  import jova.awt.event.*;
  import java.io.*;
  import javax.swing.*;
  public class ImageViewer
  { public stratic void main(String[]args)
   { JFrame frame=new ImageViewer Frame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.show();
   }
  }
  
  class ImgeViewerFrame extends JFrame
  { public ImageViewerFrame()
   { setTitle("ImageViewer");
   setSize(DEFAULT_WIDTH,DEFAULT_HEIGTH);
   label = new JLAbel();
   Container contentpane = getContentpane();
   contentpane.add(label.BorderLayout.CENTER);
   chooser = new JFileChooser();
   chooser.setCurrentDirectory(new File("."));
   Button button = new Button("open");
   contentpane.add(button,BorderLayout.SOUTH);
   button.addActionListener(new
   ActionListener()
   { public void actionperformed(ActionEvent evt)
   { int r = chooser.showOpenDialog(null));
   if(r = = JFilechooser.APPROVE_OPTLON)
   { String name
   =________________
   _______________
   }
   }
   });
   }
   private JLabel label;
   private JFileChooser chooser;
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 400;
  }
  
  3.下面是一个Applet程序,其功能是接收用户输入的两个整数,比较它们的大小,并在用户按下“比较”按钮后,将Applet中显示的“请先输入两个待比较的整数”,改为“两个整数中最大值是:x”,x是两个数中的最大值.请改正程序中的错误(有下划线的语句),使程序能输出正确的结果.
  注意:不改动程序的结构,不得增行或删行.
  import java.applet.*;
  import java.awt.*;
  import java.awr.event.*;
  
  public class FindMax extends Applet implements ActionListener{
   Label result;
   TextField in1,in2;
   Button btn;
   int a=0,b=0,max;
  
   public void init(){
   result = new Label"请先输入两个待比较的整数"};
   in1=new TextField(5);
   in2=new TextField(5);
   btn=new Button("比较");
   add(in1);
   add(in2);
   add(btn);
   add(result);
   btn.addActionListener(this);
   }
   public void actionperformed(ActionEvent e){
   a=in1;
   n=in2;
   if(a>b)
   max=a;
   else
   max=b;
   btn.setText("两个数中最大值是:"+max);
   }
  }
  
  findmax.html:
  

String name=chooser.getSelectedFile().getPath();

package ImageViewer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ImageViewer
{
public static void main(String[] args)

{
JFrame frame=new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}

}

class ImageViewerFrame extends JFrame
{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=400;
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
label=new JLabel();
Container contentPane=getContentPane();
contentPane.add(label,BorderLayout.CENTER);
chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
Button button=new Button("open");
contentPane.add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt)
{
int r=chooser.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
}
}
});
}
}

这个题目有问题!
具体的文件过滤应该这样!
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;

public class ImageViewer
{
public static void main(String[] args)
{
JFrame frame=new ImageViewerFrame();
frame.setTitle("ImageViewer");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ImageViewerFrame extends JFrame
{

public ImageViewerFrame()
{
//set up menu bar
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);

JMenu menu=new JMenu("File");
menuBar.add(menu);

JMenuItem openItem=new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new FileOpenListener());

JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)

{
System.exit(0);
}
}
);

// use a label to display the images
label=new JLabel();
Container contentPane=getContentPane();
contentPane.add(label);

}

private class FileOpenListener implements ActionListener

{
public void actionPerformed(ActionEvent evt)
{
// set up file chooser
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));

// accept all files ending with .gif
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".jpg")|| f.isDirectory();
}
public String getDescription()
{
return "*.jpg";
}
});
//show file chooser dialog
int r=chooser.showOpenDialog(ImageViewerFrame.this);

// if image file accepted, set it as icon of the label
if(r==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
}

private JLabel label;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答