我正在尝试使用 java swing 制作幻灯片程序,基本上添加了图像,然后 2 个按钮允许您前进和后退。
package javaSwingLearning;
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.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ScreenShow2 extends JFrame implements ActionListener {
private JPanel mainPanel = new JPanel();
private ImagePanel imagePanel = new ImagePanel();
private JPanel buttonPanel = new JPanel();
private JButton previousButton = new JButton();
private JButton nextButton = new JButton();
private JButton exitButton = new JButton();
private ArrayList<BufferedImage> images = new ArrayList<>();
private int position = -1;
public ScreenShow2() {
this.add(mainPanel);
mainPanel.add(buttonPanel);
mainPanel.add(imagePanel);
buttonPanel.add(nextButton);
buttonPanel.add(previousButton);
buttonPanel.add(exitButton);
nextButton.setText("Next");
nextButton.addActionListener(this);
previousButton.setText("Back");
previousButton.addActionListener(this);
exitButton.setText("Close");
exitButton.addActionListener(this);
this.pack();
this.setVisible(true);
}
private BufferedImage loadImages(String imgPath) {
try {
return ImageIO.read(new File(imgPath));
} catch (IOException e) {
System.out.println("image does not exist");
}
return null;
}
public void addImage(String imagePath) {
BufferedImage tempImage = loadImages(imagePath);
if (tempImage != null){
images.add(tempImage);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ScreenShow2 thing = new ScreenShow2();
thing.addImage("src/javaSwingLearning/hornet.png");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource().equals(exitButton)) {
System.out.println("button works");
System.exit(0);
}
if (e.getSource().equals(nextButton)) {
System.out.println("nextButton works");
if (images.size() != 0) {
System.out.println("made it past size gate");
position++;
imagePanel.setImage(images.get(position));
}
}
package javaSwingLearning;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private BufferedImage image;
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, 810, 1440, null);
super.paintComponent(g);
}
public void setImage(BufferedImage image) {
System.out.println("into setImage");
paintComponent(this.getGraphics());
this.image = image;
}
抱歉,如果这对于需要的代码太多了,我只是觉得我应该涵盖我所有的基础 从本质上讲,代码将所有正确的信息传递给我制作的imagePanel,但它实际上不会在窗口中显示它。这是一个简单的解决方案还是一个根本问题?
g.drawImage(image, 0, 0, 810, 1440, null);
super.paintComponent(g);
您的代码正在绘制图像,然后调用super.paintComonent()
,该图像将面板的背景绘制在图像顶部。
正确的顺序应该是:
super.paintComponent(g);
g.drawImage (image, 0, 0, 810, 1440, null);
为什么要硬编码 810、1440?我的屏幕分辨率是 1024 x 768?
如果您尝试填充面板的整个区域,则应使用getWidth()
和getHeight()
方法。
paintComponent(this.getGraphics());
this.image = image;
切勿直接调用 paintComponent(...) 方法。当需要重新绘制组件时,Swing 将调用该方法。
相反,您只需调用repaint()
方法。
如果在尝试绘制组件之前不更改图像变量,则期望如何绘制新图像?