//JavaTest.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class JavaTest extends JFrame implements ActionListener{
JSlider slider;
JButton jb1,jb2;
WPanel sPanel;
double time=2;
Timer timer;
public JavaTest(String s){
super(s);
Container contentPane=getContentPane();
//north
JPanel panel=new JPanel(new GridLayout(1,2));
slider=new JSlider(JSlider.HORIZONTAL,0,5000,2000);
slider.setBorder(BorderFactory.createTitledBorder("滑动最大时间为5秒"));
slider.addChangeListener(new ChangeListener(){
    public void stateChanged(ChangeEvent e){
     time=slider.getValue();
     slider.setBorder(BorderFactory.createTitledBorder("间隔时间为"+time/1000+"秒"));
    }
});
panel.add(slider);

JPanel panel1=new JPanel(new GridLayout(1,2));
jb1=new JButton("连续播放");
panel1.add(jb1);
jb1.addActionListener(this);
jb2=new JButton("停止播放");
panel1.add(jb2);
jb2.addActionListener(this);

panel.add(panel1);
contentPane.add(panel,BorderLayout.NORTH);
//center
sPanel=new WPanel("图片\\2.jpg");//“图片\\”中有很多图片
contentPane.add(sPanel,BorderLayout.CENTER);
setSize(300,400);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
    time=slider.getValue();
    timer=new Timer((int)time,this);
timer.start();
}
if(e.getSource()==jb2){
timer.stop();
}
              //每隔时间time重绘一图片,但为什么无法重绘
         if(e.getSource()==timer){
sPanel.i++;
sPanel.repaint();
}
}
public static void main(String[]args){
JavaTest javatest=new JavaTest("我喜欢你");
javatest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
javatest.setVisible(true);
javatest.pack();
}
}
//WPanel.java
import java.awt.*;
import javax.swing.*;
public class WPanel extends JPanel{
int i=1;
String path="图片\\"+i+".jpg" ;
public WPanel( String path ){
this.path=path;
}
public void paint(Graphics g ){
super.paint(g);
Toolkit tk=getToolkit();
Image image=tk.getImage(path);
g.drawImage(image, 0,0,this);
}
}
为什么无法自动重绘图片?

解决方案 »

  1. try my stuffimport javax.swing.*;
    import java.awt.*;
    @SuppressWarnings("serial")
    class PicPanel extends JPanel {
    int p_width = 0;
    int p_height = 0;
    Image im = null; public PicPanel(String picName) {
    ImageIcon imageIcon = new ImageIcon(picName);
    im = imageIcon.getImage(); p_width = imageIcon.getIconWidth();
    p_height = imageIcon.getIconHeight();
    setBounds(0, 0, p_width, p_height);
    } public void paint(Graphics g) {
    g.drawImage(im, 0, 0, p_width, p_height, null);
    }
    }
      

  2. 程序我没怎么看明白啊 需求也不明白sPanel=new WPanel("图片\\2.jpg");//“图片\\”中有很多图片 
     这个算是背景?然后String path = "图片\\" + i + ".jpg"; 要怎么搞?先1 然后2 然后3 然后4 然后5  后面一个替代前面一个显示出来吗?
      

  3. 我建议你搞一个PanelManager 去管理你的Panelload的时候 一起加载进来 当然还是用你的WPanel去加载然后显示的时候  用PanelManager去调配  需要显示的就panel.setVisible(true);
    不需要显示就 panel.setVisible(false);不过额  想出这种办法骗女孩子  想当年 我也为一个MM做了一个游戏送给她 当生日礼物了如果你需要我帮你完成的话   把你的图片upload到一个地方  然后我去download 
    写好代码再贴给你
      

  4. 不巧了 还真没有。找个地方upload又不难  快点想办法哇  追到了就有咪咪摸了  哈哈哈哈
      


  5. import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.TexturePaint;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;public class BackgroundExample {    public static void main(final String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JFrame frame = new JFrame("Rich");
                    final String[] images = {"background/01.jpg",
                            "background/02.jpg",
                            "background/03.jpg",
                    "background/04.jpg"};
                    final JComboBox backgrounds;
                    final BackgroundPanel backgroundPanel = new BackgroundPanel();
                    backgrounds = new JComboBox(images);
                    //backgrounds.setSelectedIndex(1);
                    backgrounds.addActionListener(new ActionListener() {                    @Override
                        public void actionPerformed(final ActionEvent e) {
                            JComboBox box = (JComboBox)e.getSource();
                            String background = (String)box.getSelectedItem();
                            try {
                                backgroundPanel.changeBackground(background);
                                backgroundPanel.repaint();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }                });
                    
                    frame.setUndecorated(false);
                    backgroundPanel.setOpaque(false);
                    frame.getContentPane().add(backgroundPanel,BorderLayout.CENTER);
                    frame.getContentPane().add(backgrounds,BorderLayout.NORTH);
                    frame.setBounds(100, 100, 750, 560);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    class BackgroundPanel extends JPanel{    /**
         *
         */
        private static final long serialVersionUID = 11111111111111L;
        private BufferedImage background;
        private TexturePaint texture;    public void changeBackground(final String image) throws IOException {
            this.background = ImageIO.read(this.getClass().getClassLoader().getResource(image));
            Rectangle rect = new Rectangle(0,0,this.background.getWidth(),this.background.getHeight());
            this.texture = new TexturePaint(this.background,rect);
        }    @Override
        public void paintComponent(final Graphics g) {        if(this.texture!= null) {
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(this.texture);
                g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
            }        super.paintComponent(g);
        }
    }classes\BackgroundExample$1$1.class 
    classes\BackgroundExample$1.class 
    classes\BackgroundExample.class 
    classes\BackgroundPanel.class 
    resources\background\01.jpg 
    resources\background\02.jpg 
    resources\background\03.jpg 
    resources\background\04.jpg 
    sources\BackgroundExample.java javac sources\BackgroundExample.java -d classes 
    javaw -cp classes;resources BackgroundExample jdk6u17
      

类似问题 »