当前位置: 代码迷 >> Java相关 >> [求助]这是什么意思!?
  详细解决方案

[求助]这是什么意思!?

热度:187   发布时间:2006-04-29 01:50:00.0
[求助]这是什么意思!?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JCheckBoxDemo extends JFrame
{
JCheckBox chinButton;
JCheckBox glassesButton;
JCheckBox hairButton;
JCheckBox teethButton;
StringBuffer choices;
JLabel wordLabel;
public JCheckBoxDemo()
{
chinButton = new JCheckBox("Chin");
chinButton.setMnemonic(KeyEvent.VK_C);
chinButton.setSelected(true);
glassesButton = new JCheckBox("Glasses");
glassesButton.setMnemonic(KeyEvent.VK_G);
glassesButton.setSelected(true);
hairButton = new JCheckBox("Hair");
hairButton.setMnemonic(KeyEvent.VK_H);
hairButton.setSelected(true);
teethButton =new JCheckBox("Teeth");
teethButton.setMnemonic(KeyEvent.VK_T);
teethButton.setSelected(true);
CheckBoxListener myListener=new CheckBoxListener();
chinButton.addItemListener(myListener);
glassesButton.addItemListener(myListener);
hairButton.addItemListener(myListener);
teethButton.addItemListener(myListener);
choices = new StringBuffer("cght");
wordLabel = new JLabel(new ImageIcon("images/geek-"+choices.toString()+".gif"));
wordLabel.setToolTipText(choices.toString());
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0,1));
checkPanel.add(chinButton);
checkPanel.add(glassesButton);
checkPanel.add(hairButton);
checkPanel.add(teethButton);
setLayout(new BorderLayout());
add(checkPanel,BorderLayout.WEST);
add(wordLabel,BorderLayout.CENTER);
checkPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
class CheckBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
int index = 0; char c ='_';
Object source =e.getItemSelectable();
if(source==chinButton)
{
index=0; c ='c';
}else if(source==glassesButton)
{
index=1; c='g';
}else if (source==hairButton)
{
index=2; c='h';
}else if (source==teethButton)
{
index=3;c='t';
}
if (e.getStateChange()==ItemEvent.DESELECTED)
c='_';
choices.setCharAt(index ,c);
wordLabel.setText(choices.toString());
}
}
public static void main(String s[])
{
JFrame frame=new JFrame("CheckBoxDemo");
frame.setContentPane(new JCheckBoxDemo());
frame.pack();
frame.setVisible(true);
}
}
我编译了没问题,可是运行却有这样的提示:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.addImpl(Container.java:1010)
at javax.swing.JLayeredPane.addImpl(JLayeredPane.java:204)
at java.awt.Container.add(Container.java:899)
at javax.swing.JRootPane.setContentPane(JRootPane.java:562)
at javax.swing.JFrame.setContentPane(JFrame.java:609)
at JCheckBoxDemo.main(JCheckBoxDemo.java:74)

蓝色的意思是不是说:“把窗口加入一个容器”吗!?
可是要怎么加我不知道!麻烦大家给我解说一下可以吗!?加在那里给我标明一下!麻烦了!

搜索更多相关的解决方案: class  public  import  

----------------解决方案--------------------------------------------------------
Exception in thread "main" java.lang.IllegalArgumentException
非法参数异常
你的JCheckBoxDemo就是继承自JFrame的,你不能把像JFrame这样的顶级窗口类放到别的窗口里面去。
你的JCheckBoxDemo本身就可以用做一个JFrame的,懂了吗?
直接写
JCheckBoxDemo jc=new JCheckBoxDemo();
jc.pack();
jc.setVisible(true);

----------------解决方案--------------------------------------------------------

恩!知道了!


----------------解决方案--------------------------------------------------------
  相关解决方案