Java swing 在 ContentPane 中交换 ImagePanel
我有一组图像想要在 JFrame 中呈现。它们的大小都相同 - 每个图像都填充 JFrame。我在可见的图层样式之间交换:
f = new JFrame("xx");
f.setSize(480, 854);
contentPane = f.getContentPane();
ip1 = new ImagePanel(new File("assets/1.jpg"));
ip2 = new ImagePanel(new File("assets/2.jpg"));
ip3 = new ImagePanel(new File("assets/3.jpg"));
f.setVisible(true);
contentPane.add(ip1);
contentPane.addMouseListener(mouseListener);
ImagePanel 是:
public class ImagePanel extends JPanel {
private BufferedImage image;
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
public ImagePanel(File imageFile) {
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Image could not be read: " + imageFile);
}
setVisible(true);
}
}
我尝试在 MouseListener 中在它们之间交换:
static MouseListener mouseListener = new MouseListener() {
private int i = 1;
@Override
public void mouseClicked(MouseEvent e) {
// logger.log(Level.INFO, e.toString());
contentPane.removeAll();
if (++i > 3) i = 1;
switch (i) {
case 1: contentPane.add(ip1); break;
case 2: contentPane.add(ip2); break;
case 3: contentPane.add(ip3); break;
}
contentPane.repaint();
}
};
第一个图像根据需要显示。单击给我白色,第二次单击时相同,第三次单击使我返回到所需的第一张图像。如果我单击白色之间的框架,则会出现第二个和第三个图像,并且一旦渲染,它就会按预期工作。我缺少什么?
嗯,新手!毫无疑问有更好的方法...
谢谢
I have a set of images that I want to present in a JFrame. They are all the same size - each image fills the JFrame. I swap between which is visible, layer style:
f = new JFrame("xx");
f.setSize(480, 854);
contentPane = f.getContentPane();
ip1 = new ImagePanel(new File("assets/1.jpg"));
ip2 = new ImagePanel(new File("assets/2.jpg"));
ip3 = new ImagePanel(new File("assets/3.jpg"));
f.setVisible(true);
contentPane.add(ip1);
contentPane.addMouseListener(mouseListener);
An ImagePanel is:
public class ImagePanel extends JPanel {
private BufferedImage image;
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
public ImagePanel(File imageFile) {
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Image could not be read: " + imageFile);
}
setVisible(true);
}
}
And I try swapping between them in the MouseListener:
static MouseListener mouseListener = new MouseListener() {
private int i = 1;
@Override
public void mouseClicked(MouseEvent e) {
// logger.log(Level.INFO, e.toString());
contentPane.removeAll();
if (++i > 3) i = 1;
switch (i) {
case 1: contentPane.add(ip1); break;
case 2: contentPane.add(ip2); break;
case 3: contentPane.add(ip3); break;
}
contentPane.repaint();
}
};
The first image displays as desired. Click gives me white, and same on 2nd click, and third brings me back round to my first image as desired. If I click on the frame in between clicks on the white, the 2nd and 3rd images appear, and once rendered it works as expected. What am I missing?
Ack, newbs! No doubt there is a much better way...
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将两个面板放入
CardLayout
。Put both panels in a
CardLayout
.将您的
图像
作为图标 JLabel,Swing GUI 的规则
那么你必须打电话
put your
Images
as Icon to the JLabel,rulles for Swing GUI
then you have to call