复制JPanel - (将JPanel的静态图像复制到不同的JPanel) [英] Duplicating a JPanel - (Copy JPanel's static image to different JPanel)

查看:204
本文介绍了复制JPanel - (将JPanel的静态图像复制到不同的JPanel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很擅长摇摆,我希望得到一些帮助,因为我坚持完成任务。

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

当前状态:

我有一个不错的 JFrame 对象(guiFrame)有两个 JPanel 就可以了(tabsPanel和cardPanel)(一个是简单的 JPanel 带按钮,另一个有 CardLayout 由tabsPanel按钮切换)。

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

问题:

任务是如果我按按钮 在tabsPanel上我需要将cardPanel作为静态图像发送到另一个窗口(ShowFrame),而在上一个窗口中,程序仍然运行且很好。所以基本上我试图复制/克隆cardPanel。

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.

我尝试了什么:


  • 我试过简单地

  • I have tried to simply

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

当然无法正常工作,因为正在复制参考编号,如果我运行程序,则为cardPanel消失。

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

我试图使用 clone()
为此它几乎工作,但我得到一些奇怪的 NullPointerException 这不是我的代码造成的。

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.

当前代码(克隆尝试):

CardPanel.java

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

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和TestContent2很简单带有SwingGUI生成的随机内容的JPanel,就像ShowFrame是空的JFrame一样。但如果需要,我也会粘贴这些代码。

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.

推荐答案

如果您只需要<$ c $的图像 c> cardPanel ,您只需创建一个图像并使用 JLabel 来显示,例如......

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

现在您拥有 cardPanel的副本,您只需使用 JLabel 即可显示它,例如......

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

这篇关于复制JPanel - (将JPanel的静态图像复制到不同的JPanel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆