我刚刚起步,我想得到一些帮助,因为我坚持执行任务。
当前状态:
我有一个不错的JFrame
对象(guiFrame),上面有两个JPanel
(tabsPanel和cardPanel)(一个是带有按钮的简单JPanel
,另一个是由tabsPanel按钮切换的CardLayout
。
问题:
任务是,如果我按下tabsPanel上的“显示”按钮,则需要将cardPanel作为静态“图像”发送到另一个窗口(ShowFrame),而在前一个窗口上,程序仍在运行并且运行良好。所以从根本上说,我试图复制/克隆cardPanel。
我尝试过的
我试图简单地
JPanel jPanelShow = cardPanel;
show.add(jPanelShow);
当然不起作用,因为正在复制参考号,并且如果我运行该程序,则cardPanel会“消失”。
我尝试使用
clone()
为此,它几乎可以正常工作,但是我得到了一些奇怪的
NullPointerException
,这不是由我的代码引起的。当前代码(克隆尝试):
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和TestContent2是简单的JPanel,带有由SwingGUI生成的随机内容,就像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);