首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

虚心请问~小弟实在没法了.

2012-01-08 
虚心请教~~~小弟实在没法了........我是新手正在学习中。。。。。。这是个简单的程序,还没写完但是应该可以看到

虚心请教~~~小弟实在没法了........
我是新手正在学习中。。。。。。
这是个简单的程序,还没写完但是应该可以看到程序界面了呀,为什么编译的时候没问题,运行的时候就出问题了啊???大哥大姐教教我呀。。。谢谢。。。。

java.lang.IllegalArgumentException:   adding   container 's   parent   to   itself
                at   java.awt.Container.addImpl(Container.java:1005)
                at   java.awt.Container.add(Container.java:899)
                at   Calculator.init(Calculator.java:21)
                at   Calculator.main(Calculator.java:128)
Press   any   key   to   continue...

-----------------以下是程序-----------------------------

import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   java.awt.event.ActionListener;
import   java.util.EventListener;
public   class   Calculator   extends   JFrame   implements   ActionListener  
{
JTextField   jtf=   new   JTextField();
public   void   init()
{
JFrame   tf=new   JFrame();
Container   c=tf.getContentPane();
tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf, "NORTH ");
JPanel   jpl=new   JPanel();
c.add(jpl, "CENTER ");
//---------------------------------------------
jpl.setLayout(new   GridLayout(4,4));
//--------------------------------------------
JButton   b=null;

b=   new   JButton( "1 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "2 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "3 ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "+ ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "4 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "5 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "6 ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "- ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "7 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "8 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "9 ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "* ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "0 ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( ". ");
b.addActionListener(this);
jpl.add(b);


b=   new   JButton( "= ");
b.addActionListener(this);
jpl.add(b);

b=   new   JButton( "\\ ");
b.addActionListener(this);
jpl.add(b);

tf.setSize(400,400);
tf.setTitle( "计算器 ");
tf.setVisible(true);
}

public   void   actionPerformed(ActionEvent   e)  


{
//   TODO:   在这添加你的代码
jtf.setText(jtf.getText()+e.getActionCommand());
}
  public   static   void   main(String[]   args)  
        {
new   Calculator().init();
}
}


[解决办法]
adding container 's parent to itself
写的很明白了

c.add(tf, "NORTH ");错
你把
它的parent add 给 它自己了
[解决办法]
呵呵 给分吧

你把tf 这个JFrame加到了容器上面 错误
然后后面的
c.add(tf, "NORTH ");
c.add(jpl, "CENTER ");
也是错误的 改成BorderLayout.NORTH 和BorderLayout.CENTER



[解决办法]
其实可以用add(JComponent, "North ")的!

热点排行