Duplicating a JPanel - (Copy JPanel's static i

2019-05-29 03:22发布

问题:

I'm pretty new to swing and I would like to receive some help as Im stuck with a task.

Current state:

Im having a nice JFrame object (guiFrame) which is having two JPanel on it (tabsPanel and cardPanel)(one is a simple JPanel with buttons, the other has CardLayout which is switched by the tabsPanel buttons).

Problem:

The task is that if I press the button "Show" on tabsPanel I need to send the cardPanel to a different window (ShowFrame) as a static "image", while on the previous window the program is still running and nice. So basicly Im trying to copy / clone the cardPanel.

What I have tried:

  • I have tried to simply

    JPanel jPanelShow = cardPanel; 
    show.add(jPanelShow);
    

    Of course not working because the reference number is being copied and if I run the program, the cardPanel "disappears".

  • I have tried to use clone() For this its almost working but I'm getting some weird NullPointerException which isn't caused by my code.

Current codes (cloning try):

CardPanel.java

/**
 * This is basicly a JPanel, just with a clone() implemented
 */
package javaapplication5;

import javax.swing.JPanel;
import java.util.Stack;

public class CardPanel extends JPanel implements Cloneable {

    public CardPanel() {
        super();
    }

    @Override
    public CardPanel clone() throws NullPointerException {
        /* Creating return object */
        final CardPanel copy;

        try {

            /* Cloning */
            copy = (CardPanel) super.clone();
        } catch (CloneNotSupportedException e) {

            /* Exception (should not happen though) */
            e.printStackTrace();
            return null;
        }
        return copy;
    }
}

CardLayoutExample.java

package javaapplication5;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class CardLayoutExample {


    JFrame guiFrame;
    CardLayout cards;
    CardPanel cardPanel;
    private int showFrameNotShownYet = 1;
    public ShowFrame show = new ShowFrame();

    public static void main(String[] args) {

         /* Random things for Swing */
         EventQueue.invokeLater(new Runnable()
         {            
            @Override
             public void run()
             {     
                 new CardLayoutExample();         
             }
         });

    }

    public CardLayoutExample()
    { 
        /* Creating the main JFrame */
        guiFrame = new JFrame();

        /* Making sure the program exits when the frame closes */
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("CardLayout Example");
        guiFrame.setSize(400,300);

        /* This will center the JFrame in the middle of the screen */
        guiFrame.setLocationRelativeTo(null);
        guiFrame.setLayout(new BorderLayout());

        /* Border for JPanel separation */
        Border outline = BorderFactory.createLineBorder(Color.black);

        /* Creating JButton1 for tabsPanel */
        JButton switchCards1 = new JButton("1");
        switchCards1.setActionCommand("1");
        switchCards1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                cards.show(cardPanel, "TestContent");     
            }
        });

        /* Creating JButton2 for tabsPanel */
        JButton switchCards2 = new JButton("2");
        switchCards2.setActionCommand("2");
        switchCards2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                cards.show(cardPanel, "TestContent1");      
            }
        });

        /* Creating JButton3 for tabsPanel */
        JButton switchCards3 = new JButton("3");
        switchCards3.setActionCommand("3");
        switchCards3.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                cards.show(cardPanel, "TestContent2");    
            }
        });

        JButton switchCards4 = new JButton("Show");
        switchCards4.setActionCommand("Show");
        switchCards4.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {

                /* If there is no ShowFrame yet */
                if(showFrameNotShownYet == 1){

                    show.add((JPanel)cardPanel.clone());
                    show.setVisible(true);
                    showFrameNotShownYet = 0; 
                } 

                /* If there is a ShowFrame already */
                else {
                    show.setVisible(false);
                    showFrameNotShownYet = 1;
                    guiFrame.repaint();
                }   
            }
        });

        JButton switchCards5 = new JButton("Refresh");
        switchCards5.setActionCommand("Refresh");
        switchCards5.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {                    
                    show.setVisible(false);
                    show.add((JPanel)cardPanel.clone());
                    show.setVisible(true);
                    showFrameNotShownYet = 0;
            }
        });

        /* Creating JPanel for buttons */
        JPanel tabsPanel = new JPanel();
        tabsPanel.setBorder(outline);
        tabsPanel.add(switchCards1);
        tabsPanel.add(switchCards2);
        tabsPanel.add(switchCards3);
        tabsPanel.add(switchCards4);
        tabsPanel.add(switchCards5);

        /* Creating JPanel for CardLayout */
        cards = new CardLayout();
        cardPanel = new CardPanel();
        cardPanel.setLayout(cards);
        cards.show(cardPanel, "TestContent");

        /* Adding 1st card */
        JPanel firstCard = new TestContent();
        cardPanel.add(firstCard, "TestContent");

        /* Adding 2nd card */
        JPanel secondCard = new TestContent1();
        cardPanel.add(secondCard, "TestContent1");

        /* Adding 3rd card */
        JPanel thirdCard = new TestContent2();
        cardPanel.add(thirdCard, "TestContent2");

        /* Filling up JFrame with stuff */
        guiFrame.add(tabsPanel,BorderLayout.NORTH);
        guiFrame.add(cardPanel,BorderLayout.CENTER);
        guiFrame.setVisible(true);
    }

}

TestContent, TestContent1 and TestContent2 are simple JPanels with random stuff generated by SwingGUI, just as ShowFrame is empty JFrame. But if needed I will paste in those codes too.

回答1:

If you only need an "image" of the cardPanel, you can simply create an image and use a JLabel to show, for example...

BufferedImage img = new BufferedImage(cardPanel.getWidth(), cardPane.getHeight(), BufferedImage.BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
cardPanel.printAll(g2d);
g2d.dispose();

Now you have a "copy" of the cardPanel, you can simply use a JLabel to display it, for example...

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.DIPOSE_ON_CLOSE);
frame.add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setLocationRelativeTo(this);
frame.setVisible(true);


标签: java swing clone