English 中文(简体)
Java 缝 s 目录
原标题:Java swing swapping ImagePanels in a ContentPane

我有一套图像,我想在联合阵线上展示。 这些数字都是相同的,每个图像都填满JFrame。 I swap, 其中可见一层:

    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);

图像小组:

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();
    }
};

第一个图像按预期显示。 浮标给我白色,同点击2点,第三点使我回头看上去。 如果我点击frame,在点击白色之间,那么第二和三幅图像就出现,一旦达到预期效果。 我失踪了什么?

Ack, newbs! 毫无疑问,有更好的办法......

感谢

最佳回答

Put both panel in a CardLayout

问题回答

http://download.oracle.com/javase/tutorial/uiswing/components/icon.html” rel=“nofollow”

rulles for Swing GUI

然后,你必须打电话。

myLabel.setIcon(myIconn);
revalidate();
repaint();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签