程序 如下 。。 中间 问号的部分怎么写 才能 正确的打开 一张图片 啊 。。 郁闷 ! 各位帮忙看看啊 ! import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import java.io.*; public class Main extends JFrame implements ActionListener
{
private ImagePanel panel;
private JScrollPane scrollPane;
private JButton zoomin;
private JButton zoomout;
private JButton open; private JPanel button_panel; private JFileChooser fc; private String file;
public Main ()
{ super ("digiMAP");
panel = new ImagePanel ("/1.jpg"); panel.setPreferredSize (new Dimension (600, 500));
scrollPane = new JScrollPane (panel);
scrollPane.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); zoomin = new JButton ("ZOOM IN");
zoomout = new JButton ("ZOOM OUT");
open=new JButton("OPEN"); zoomin.addActionListener (this);
zoomout.addActionListener (this);
open.addActionListener(this);
fc=new JFileChooser();
button_panel=new JPanel(); button_panel.add(open);
button_panel.add(zoomin);
button_panel.add(zoomout); this.getContentPane().setLayout(new BorderLayout());
this.add (scrollPane,BorderLayout.CENTER);
this.add (button_panel,BorderLayout.SOUTH); } public static void main ( String[] args )
{
Main f=new Main();
f.pack();
f.setVisible(true);
f.setSize(600,500);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
} public void actionPerformed ( ActionEvent e )
{
if ((JButton)(e.getSource())==open)
{
switch (fc.showOpenDialog(this))
{ case JFileChooser.CANCEL_OPTION:System.out.println("Open cancelled");
break; case JFileChooser.APPROVE_OPTION: String file = fc.getSelectedFile().getPath();
??????????????????????????????????
System.out.println("File to open:"+fc.getSelectedFile().toString());
break;
case JFileChooser.ERROR_OPTION: System.out.println("Error happened"); }
return;
} if ((JButton) (e.getSource ()) == zoomin)
{
panel.enlarge ();
panel.setPreferredSize (panel.getPreferredSize ());
scrollPane.validate ();
}
if ((JButton) (e.getSource ()) == zoomout)
{
panel.ensmall ();
panel.setPreferredSize (panel.getPreferredSize ());
scrollPane.validate ();
}
}
}class ImagePanel extends JLayeredPane
{
private Dimension theSize = new Dimension (600, 400);
private ImageIcon img = null;
public ImagePanel (String imgpath )
{
super ();
setLayout (null);
this.img = new ImageIcon (getClass ().getResource (imgpath));
}
public void paintComponent ( Graphics g )
{
g.clearRect (0, 0, 1024, 768);
g.drawImage (img.getImage (), 0, 0, theSize.width, theSize.height,null);
}
public void enlarge ( )
{
theSize.width = (theSize.width * 101) / 100;
theSize.height = (theSize.height * 101) / 100;
setSize (theSize);
}
public void ensmall ( )
{
theSize.width = (theSize.width * 100) / 101;
theSize.height = (theSize.height * 100) / 101;
setSize (theSize);
}
public Dimension getPreferredSize ( )
{
return this.theSize;
}
}
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import java.io.*; public class Main extends JFrame implements ActionListener
{
private ImagePanel panel;
private JScrollPane scrollPane;
private JButton zoomin;
private JButton zoomout;
private JButton open; private JPanel button_panel; private JFileChooser fc; private String file;
public Main ()
{ super ("digiMAP");
panel = new ImagePanel ("/1.jpg"); panel.setPreferredSize (new Dimension (600, 500));
scrollPane = new JScrollPane (panel);
scrollPane.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); zoomin = new JButton ("ZOOM IN");
zoomout = new JButton ("ZOOM OUT");
open=new JButton("OPEN"); zoomin.addActionListener (this);
zoomout.addActionListener (this);
open.addActionListener(this);
fc=new JFileChooser();
button_panel=new JPanel(); button_panel.add(open);
button_panel.add(zoomin);
button_panel.add(zoomout); this.getContentPane().setLayout(new BorderLayout());
this.add (scrollPane,BorderLayout.CENTER);
this.add (button_panel,BorderLayout.SOUTH); } public static void main ( String[] args )
{
Main f=new Main();
f.pack();
f.setVisible(true);
f.setSize(600,500);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
} public void actionPerformed ( ActionEvent e )
{
if ((JButton)(e.getSource())==open)
{
switch (fc.showOpenDialog(this))
{ case JFileChooser.CANCEL_OPTION:System.out.println("Open cancelled");
break; case JFileChooser.APPROVE_OPTION: String file = fc.getSelectedFile().getPath();
??????????????????????????????????
System.out.println("File to open:"+fc.getSelectedFile().toString());
break;
case JFileChooser.ERROR_OPTION: System.out.println("Error happened"); }
return;
} if ((JButton) (e.getSource ()) == zoomin)
{
panel.enlarge ();
panel.setPreferredSize (panel.getPreferredSize ());
scrollPane.validate ();
}
if ((JButton) (e.getSource ()) == zoomout)
{
panel.ensmall ();
panel.setPreferredSize (panel.getPreferredSize ());
scrollPane.validate ();
}
}
}class ImagePanel extends JLayeredPane
{
private Dimension theSize = new Dimension (600, 400);
private ImageIcon img = null;
public ImagePanel (String imgpath )
{
super ();
setLayout (null);
this.img = new ImageIcon (getClass ().getResource (imgpath));
}
public void paintComponent ( Graphics g )
{
g.clearRect (0, 0, 1024, 768);
g.drawImage (img.getImage (), 0, 0, theSize.width, theSize.height,null);
}
public void enlarge ( )
{
theSize.width = (theSize.width * 101) / 100;
theSize.height = (theSize.height * 101) / 100;
setSize (theSize);
}
public void ensmall ( )
{
theSize.width = (theSize.width * 100) / 101;
theSize.height = (theSize.height * 100) / 101;
setSize (theSize);
}
public Dimension getPreferredSize ( )
{
return this.theSize;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货