下面的Java程序如何修改,当我连续点击变大按钮的时候,图片连续变大
importjava.awt.BorderLayout;importjava.awt.Image;importjava.awt.Toolkit;importjava.aw...
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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/2.jpg");
jLabel = new JLabel();
jPanel = new JPanel();
ImageIcon imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jPanel, 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() {
BufferedImage bffimage = new BufferedImage(700, 700,
BufferedImage.TYPE_3BYTE_BGR);
// 把图片读到bufferedImage中
bffimage.getGraphics().drawImage(image, 0, 0, 700, 700, null);
// 得到转换后的Image图片
Image realImage = bffimage;
// 可以把图片转化为ImageIcon,(Swing中运用)
ImageIcon img = new ImageIcon(realImage);
jLabel.setIcon(img);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
big();
}
}
} 展开
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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/2.jpg");
jLabel = new JLabel();
jPanel = new JPanel();
ImageIcon imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jPanel, 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() {
BufferedImage bffimage = new BufferedImage(700, 700,
BufferedImage.TYPE_3BYTE_BGR);
// 把图片读到bufferedImage中
bffimage.getGraphics().drawImage(image, 0, 0, 700, 700, null);
// 得到转换后的Image图片
Image realImage = bffimage;
// 可以把图片转化为ImageIcon,(Swing中运用)
ImageIcon img = new ImageIcon(realImage);
jLabel.setIcon(img);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
big();
}
}
} 展开
1个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianxi3 extends JFrame implements ActionListener{
JPanel jPanel,jPanel1;
JLabel jLabel;
Image image;
ImageIcon imageIcon;
JButton jButton;
float www=1;
float hhh=1;
public lianxi3() {
super("图片");
jPanel1=new JPanel();
jButton = new JButton("变大");
jButton.addActionListener(this);
image = Toolkit.getDefaultToolkit().getImage("src/1.jpg");
jLabel = new JLabel();
jPanel1.add(jButton);
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
this.add(jLabel, BorderLayout.NORTH);
this.add(jPanel1, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600, 600);
}
public static void main(String[] args) {
new lianxi3();
}
public void big(){
int width=imageIcon.getIconWidth();
int height=imageIcon.getIconWidth();
double bigger=1.25;
www=(float)(width*bigger);
hhh=(float)(height*bigger);
System.out.println(www+""+hhh);
Math.pow(www,hhh);
image = image.getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT);
imageIcon = new ImageIcon(image);
//jLabel.setSize((int)www, (int)hhh);
jLabel.setIcon(imageIcon);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jButton){
big();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询