gpt4 book ai didi

java - Duplicating a JPanel - (复制JPanel的静态图片到不同的JPanel)

转载 作者:行者123 更新时间:2023-11-30 09:07:21 26 4
gpt4 key购买 nike

我是 Swing 的新手,我希望在完成一项任务时得到一些帮助。

当前状态:

我有一个很好的 JFrame 对象 (guiFrame),它上面有两个 JPanel(tabsPanel 和 cardPanel)(一个是简单的 JPanel 有按钮,另一个有 CardLayout,由 tabsPanel 按钮切换)。

问题:

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

我尝试过的:

  • 我试过简单的

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

    当然不工作,因为正在复制引用号,如果我运行该程序,cardPanel“消失”。

  • 我尝试过使用clone()为此,它几乎可以正常工作,但我遇到了一些奇怪的 NullPointerException,这不是由我的代码引起的。

当前代码(克隆尝试):

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

最佳答案

如果您只需要 cardPanel 的“图像”,您可以简单地创建一个图像并使用 JLabel 来显示,例如...

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

现在您有了 cardPanel 的“副本”,您可以简单地使用 JLabel 来显示它,例如...

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 - Duplicating a JPanel - (复制JPanel的静态图片到不同的JPanel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24009608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com