java实现(1)实现以下窗口,单击显示图像1-图像4按纽分别用不同的方法显示1.jpg-4.jpg对应的图像在左边区域,单击另存为可将当前显示的图像保存为与源文件同目录curimage.jpg文件,单击”刷新清除”可将显示的图像清除。(用setimagination)
时间: 2024-02-24 09:57:22 浏览: 13
下面是实现要求的示例代码,其中使用了 `JFrame`、`JLabel`、`JButton`、`JFileChooser` 等组件,以及 `ImageIO` 类和 `File` 类来加载和保存图片。
```java
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
public class ImageDisplay extends JFrame {
private JLabel imageLabel;
private JButton imageButton1;
private JButton imageButton2;
private JButton imageButton3;
private JButton imageButton4;
private JButton saveButton;
private JButton clearButton;
public ImageDisplay() {
setTitle("Image Display");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
imageLabel = new JLabel();
add(imageLabel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
imageButton1 = new JButton("显示图像1");
imageButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setImage("1.jpg");
}
});
imageButton2 = new JButton("显示图像2");
imageButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setImage("2.jpg");
}
});
imageButton3 = new JButton("显示图像3");
imageButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setImage("3.jpg");
}
});
imageButton4 = new JButton("显示图像4");
imageButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setImage("4.jpg");
}
});
saveButton = new JButton("另存为");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveImage();
}
});
clearButton = new JButton("刷新清除");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearImage();
}
});
buttonPanel.add(imageButton1);
buttonPanel.add(imageButton2);
buttonPanel.add(imageButton3);
buttonPanel.add(imageButton4);
buttonPanel.add(saveButton);
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void setImage(String imagePath) {
try {
Image image = ImageIO.read(new File(imagePath));
imageLabel.setIcon(new ImageIcon(image));
pack();
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveImage() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
String filePath = fileChooser.getSelectedFile().getPath();
File imageFile = new File(filePath);
try {
ImageIO.write(getImage(), "jpg", imageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void clearImage() {
imageLabel.setIcon(null);
pack();
}
public Image getImage() {
ImageIcon imageIcon = (ImageIcon) imageLabel.getIcon();
return imageIcon.getImage();
}
public static void main(String[] args) {
ImageDisplay imageDisplay = new ImageDisplay();
}
}
```
在这个示例中,我们首先创建了一个 `JFrame`,然后添加了一个 `JLabel` 用于显示图片,并添加了四个 `JButton` 分别用于显示不同的图片,以及一个 `JButton` 用于保存当前显示的图片,还有一个 `JButton` 用于清除显示的图片。我们在 `setImage` 方法中使用 `ImageIO` 类来读取指定路径下的图片,并将其设置为 `JLabel` 的图标。在 `saveImage` 方法中,我们使用 `JFileChooser` 来选择保存文件的路径,然后使用 `ImageIO` 类将当前显示的图片保存到指定的文件中。在 `clearImage` 方法中,我们将 `JLabel` 的图标设置为 `null` 来清除显示的图片。