Java,如何修改下面的程序,才能让图片可以选择

Python045

Java,如何修改下面的程序,才能让图片可以选择,第1张

供你参考一下:

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.awt.image.BufferedImage

public class lianxi3 extends JFrame implements ActionListener {

    JPanel jPanel

    JLabel jLabel

    Image image

    ImageIcon imageIcon

    JButton jButton

    float www = 1

    float hhh = 1

    public lianxi3() {

        super("图片")

        jButton = new JButton("变大")

        jButton.addActionListener(this)

        image = Toolkit.getDefaultToolkit().getImage("src/img1.jpg")

        jLabel = new JLabel()

        jPanel = new JPanel()

        imageIcon = new ImageIcon(image)

        jLabel.setIcon(imageIcon)

        jPanel.add(jLabel)

        this.add(jLabel, BorderLayout.NORTH)

        this.add(jButton, BorderLayout.SOUTH)

        this.setVisible(true)

        this.setSize(600, 600)

    }

    public static void main(String[] args) {

        new lianxi3()

    }

    public void big() {

        int width = image.getHeight(jLabel)

        int height = image.getWidth(jLabel)

        double bigger = 1.5

        www = (float) (width * bigger)

        hhh = (float) (height * bigger)

        

        

        BufferedImage bufImg = new BufferedImage(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_4BYTE_ABGR)   

         Graphics g = bufImg .createGraphics()   

         g.drawImage(image, 0, 0, null)   

         g.dispose()     

         imageIcon.setImage(rotateImage(bufImg,30))

        //imageIcon.setImage(imageIcon.getImage().getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT))

        jLabel.setIcon(imageIcon)

        jLabel.setSize((int)www, (int)hhh)

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == jButton) {

            big()

            System.out.println("aaa")

        }

    }

    

public static BufferedImage rotateImage(final BufferedImage bufferedimage,

            final int degree) {

        int w = bufferedimage.getWidth()

        int h = bufferedimage.getHeight()

        int nw = (int) (bufferedimage.getWidth()*1.414)

        int nh = (int) (bufferedimage.getHeight()*1.414)

        int type = bufferedimage.getColorModel().getTransparency()

        BufferedImage img

        Graphics2D graphics2d

        (graphics2d = (img = new BufferedImage(nw, nh, type))

                .createGraphics()).setRenderingHint(

                RenderingHints.KEY_INTERPOLATION,

                RenderingHints.VALUE_INTERPOLATION_BILINEAR)

        graphics2d.rotate(Math.toRadians(degree), nw / 2, nh / 2)

        graphics2d.translate(nw/2-w/2, nh/2-h/2)

        graphics2d.drawImage(bufferedimage, 0, 0, null)

        graphics2d.dispose()

        return img

    }

    

}

一个图片时你知道,现在只是切换的问题?

给按钮添加一个 ActionListener,在它的 actionPerformed 方法中做你的事情,把 jpanel.setIcon(..) 换张图片。

用JTabPanel 的话 不好实现这个图片的效果的。

利用俩个 pane 方式。 其中一个隐藏 。 上面标签用 JLable 实现。 。 但点击其中一个Lable 的时候 JLabel 本身切换背景图片 切换俩个JPanel 的 显示。

这种效果我做过。