why I can't adding a window to a container
源程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComCountry extends JFrame implements ItemListener
{
private String[] flagTitle = { "Canada ", "China ", "Denmark ",
"France ", "Germany ", "India ", "Norway ", "United Kingdom ",
"United States of America "};
private ImageIcon flagImage[]= new ImageIcon[9];
private String flagDescription[]=new String[9];
private JComboBox countryNameComboBox=new JComboBox();
private DescriptionPanel descriptionPanel=new DescriptionPanel();
public ComCountry()
{
flagImage[0] = new ImageIcon( "images/ca.gif ");
flagImage[1] = new ImageIcon( "images/china.gif ");
flagImage[2] = new ImageIcon( "images/denmark.gif ");
flagImage[3] = new ImageIcon( "images/fr.gif ");
flagImage[4] = new ImageIcon( "images/germany.gif ");
flagImage[5] = new ImageIcon( "images/india.gif ");
flagImage[6] = new ImageIcon( "images/norway.gif ");
flagImage[7] = new ImageIcon( "images/uk.gif ");
flagImage[8] = new ImageIcon( "images/us.gif ");
// Set text description
flagDescription[0] = "The Maple Leaf flag \n\n " +
"The Canadian National Flag was adopted by the Canadian " +
"Parliament on October 22, 1964 and was proclaimed into law " +
"by Her Majesty Queen Elizabeth II (the Queen of Canada) on " +
"February 15, 1965. The Canadian Flag (colloquially known " +
"as The Maple Leaf Flag) is a red flag of the proportions " +
"two by length and one by width, containing in its centre a " +
"white square, with a single red stylized eleven-point " +
"mapleleaf centred in the white square. ";
flagDescription[1] = "Description for China ... ";
flagDescription[2] = "Description for Denmark ... ";
flagDescription[3] = "Description for France ... ";
flagDescription[4] = "Description for Germany ... ";
flagDescription[5] = "Description for India ... ";
flagDescription[6] = "Description for Norway ... ";
flagDescription[7] = "Description for UK ... ";
flagDescription[8] = "Description for US ... ";
JLabel TitleLabel=new JLabel( "QZM 's first program ");
JPanel p1=new JPanel();
p1.setLayout(new BorderLayout());
p1.add(TitleLabel,BorderLayout.CENTER);
p1.add(countryNameComboBox,BorderLayout.WEST);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(descriptionPanel,BorderLayout.CENTER);
countryNameComboBox.addItemListener(this);
}
public static void main(String [] args)
{
// DescriptionPanel frame1=new DescriptionPanel();
ComCountry frame1=new ComCountry();
frame1.setVisible(true);
frame1.pack();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
DisplayDescriptionPanel(countryNameComboBox.getSelectedIndex());
}
public void DisplayDescriptionPanel(int index)
{
descriptionPanel.setImage(flagImage[index]);
descriptionPanel.setTitle(flagTitle[index]);
descriptionPanel.setText(flagDescription[index]);
}
}
class DescriptionPanel extends JFrame
{
private JTextArea textArea1=new JTextArea();
private JLabel countryImageLabel=new JLabel();
private JLabel countryTitleLabel=new JLabel();
public DescriptionPanel()
{
JPanel p1=new JPanel();
p1.setLayout(new BorderLayout());
p1.add(countryImageLabel,BorderLayout.CENTER);
p1.add(countryImageLabel,BorderLayout.SOUTH);
JScrollPane jScrollPane=new JScrollPane();
jScrollPane.add(textArea1);
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
setLayout(new BorderLayout());
add(jScrollPane,BorderLayout.EAST);
add(p1,BorderLayout.CENTER);
}
public void setImage(ImageIcon icon)
{
countryImageLabel.setIcon(icon);
Dimension dimension=new Dimension(icon.getIconWidth(),icon.getIconHeight());
countryImageLabel.setPreferredSize(dimension);
}
public void setTitle(String title)
{
countryTitleLabel.setText(title);
}
public void setText(String text)
{
textArea1.setText(text);
}
}
编译 OK
运行出现如下错误。
Exception in thread "main " java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.addImpl(Container.java:1022)
at java.awt.Container.add(Container.java:903)
at ComCountry. <init> (ComCountry.java:55)
at ComCountry.main(ComCountry.java:62)
本人系Java初学者,望高手指点。不甚感激。
[解决办法]
代码贴的太多,没看完。
[解决办法]
应该是这个的
class DescriptionPanel extends JFrame{}
这里的
private DescriptionPanel descriptionPanel=new DescriptionPanel();
生成的descriptionPanel 是一个窗口对象。
不能再向ComCountry中增加相同等级的对象了。
可以把class DescriptionPanel extends JFrame{}
改为一个轻量级的容器。比如说
改为class DescriptionPanel extends JPanel{}