java swing图片

qq_34711731 2016-12-28 03:03:07
下面的代码我该怎么写才会让很多张图片随机出现并且以一秒的速度换一次
package p1;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class TextArea extends JFrame implements ActionListener{

JLabel jlabel=new JLabel();
JFrame jframe=new JFrame();
private Icon icon1,icon2;
private JLabel j1,j2,j3;
JButton b1=new JButton("开始");
JButton b2=new JButton("停止");
boolean isGo=false;
Thread th1=new Thread();
Thread th2=new Thread();
JPanel p=new JPanel();
int action=0;
private Image image;

public TextArea(){
jlabel.setOpaque(true);
b1.setActionCommand("start");
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,700);
this.setLocation(300, 30);
Cursor cu=new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
//设置背景图片
icon1=new ImageIcon("F:/tupian/a1.png");
j1=new JLabel(icon1);
//设置开始按钮的图片和位置
icon2=new ImageIcon("F:/a5.png");
j2=new JLabel(icon2);
b1.setIcon(icon2);
b1.setLocation(500,100);
b1.addActionListener(this);
b1.setHorizontalTextPosition(SwingConstants.CENTER);
b1.setVerticalTextPosition(SwingConstants.BOTTOM);
//设置停止按钮的图片和位置
icon2=new ImageIcon("F:/a6.png");
j3=new JLabel(icon2);
b2.setIcon(icon2);
b2.setLocation(600,100);
b2.addActionListener(this);
b2.setHorizontalTextPosition(SwingConstants.CENTER);
b2.setVerticalTextPosition(SwingConstants.BOTTOM);
//添加进面板
p.add(j1);
p.add(j2);
p.add(j3);
// this.getContentPane().setBackground(Color.BLACK);
// th1.run();
}

public void init1Frame() {
// TODO Auto-generated method stub


ImageIcon icon=new ImageIcon("F:/tupian/a3.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),icon.getIconHeight(),Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight()+""+icon.getIconWidth());
// jlabel.setBounds(300,300,700,768);
jlabel.setHorizontalAlignment(0);
jlabel.setIcon(icon);
jframe.setSize(1366, 768);
jframe.add(jlabel);
jframe.setBounds(343,250, 300, 300);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jframe.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1){
isGo=true;
init1Frame();
b1.setEnabled(false);
b2.setEnabled(true);
}else{
isGo=false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}


public static void main(String[] args) {
new TextArea();
}
}
...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_34711731 2016-12-28
  • 打赏
  • 举报
回复
大神,太谢谢了 ,这个可以
bcsflilong 2016-12-28
  • 打赏
  • 举报
回复
package dbtotxt;


import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class TextArea extends JFrame implements ActionListener {

    JLabel jlabel = new JLabel();
    JFrame jframe = new JFrame();
    private Icon icon1, icon2;
    private JLabel j1, j2, j3;
    JButton b1 = new JButton("开始");
    JButton b2 = new JButton("停止");
    boolean isGo = false;
    Thread th1 = new Thread();
    Thread th2 = new Thread();
    JPanel p = new JPanel();
    int action = 0;
    private Image image;

    public TextArea() {
        jlabel.setOpaque(true);
        b1.setActionCommand("start");
        p.add(b1);
        p.add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b2.setEnabled(false);
        this.getContentPane().add(p, "South");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700, 700);
        this.setLocation(300, 30);
        Cursor cu = new Cursor(Cursor.HAND_CURSOR);
        this.setCursor(cu);
        this.setVisible(true);
//设置背景图片
        icon1 = new ImageIcon("F:/tupian/a1.png");
        j1 = new JLabel(icon1);
//设置开始按钮的图片和位置
        icon2 = new ImageIcon("F:/a5.png");
        j2 = new JLabel(icon2);
        b1.setIcon(icon2);
        b1.setLocation(500, 100);
        b1.addActionListener(this);
        b1.setHorizontalTextPosition(SwingConstants.CENTER);
        b1.setVerticalTextPosition(SwingConstants.BOTTOM);
//设置停止按钮的图片和位置
        icon2 = new ImageIcon("F:/a6.png");
        j3 = new JLabel(icon2);
        b2.setIcon(icon2);
        b2.setLocation(600, 100);
        b2.addActionListener(this);
        b2.setHorizontalTextPosition(SwingConstants.CENTER);
        b2.setVerticalTextPosition(SwingConstants.BOTTOM);
//添加进面板
        p.add(j1);
        p.add(j2);
        p.add(j3);
// this.getContentPane().setBackground(Color.BLACK);
// th1.run();
    }

    public void init1Frame() {
// TODO Auto-generated method stub


        ImageIcon icon = new ImageIcon("F:/tupian/a3.jpg");
        icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT));
        System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
// jlabel.setBounds(300,300,700,768);
        jlabel.setHorizontalAlignment(0);
        Thread thread=new Thread(new Runnable() {
            
            @Override
            public void run() {
                
                int countI=0;
                while(isGo){
                    try {
                        Thread.sleep(1000);
                        countI=countI+1;
                        if (countI>3) {
                            countI=1;
                        }
                        ImageIcon icon = new ImageIcon("F:/tupian/a"+countI+".jpg");
                        icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT));
                        jlabel.setIcon(icon);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(TextArea.class.getName()).log(Level.SEVERE, null, ex);
                    }
                     
                }
                
            }
        });
        thread.start();
        jlabel.setIcon(icon);
        jframe.setSize(1366, 768);
        jframe.add(jlabel);
        jframe.setBounds(343, 250, 300, 300);
        jframe.pack();
        jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jframe.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
        if (e.getSource() == b1) {
            isGo = true;
            init1Frame();
            b1.setEnabled(false);
            b2.setEnabled(true);
        } else {
            isGo = false;
            b2.setEnabled(false);
            b1.setEnabled(true);
        }
    }

    public static void main(String[] args) {
        new TextArea();
    }
}

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧