当应用程序递归太深时抛出异常

nicholasmars 2008-03-17 04:21:40
package com.tsinghuasoftfive.java;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class AddInterface extends JFrame {
// "姓名", "性别", "年龄", "籍贯", "电话号码", "QQ号码", "就业去向"
private static final long serialVersionUID=1L;
JPanel jp1 = new JPanel();

JPanel jp2 = new JPanel();

JPanel jp3 = new JPanel();

JPanel jp4 = new JPanel();

JPanel jp5 = new JPanel();

JPanel jp6 = new JPanel();

JPanel jp7 = new JPanel();

JPanel jp8 = new JPanel();

JLabel lb1 = new JLabel("姓名:");

JLabel lb2 = new JLabel("性别:");

JLabel lb3 = new JLabel("年龄:");

JLabel lb4 = new JLabel("籍贯:");

JLabel lb5 = new JLabel("电话:");

JLabel lb6 = new JLabel("QQ:");

JLabel lb7 = new JLabel("就业去向:");

JTextField tf1 = new JTextField(10);

JRadioButton rb1 = new JRadioButton("男");

JRadioButton rb2 = new JRadioButton("女");

JComboBox cb1 = new JComboBox();

JComboBox cb2 = new JComboBox();

JTextField tf2 = new JTextField(10);

JTextField tf3 = new JTextField(10);

JTextField tf4 = new JTextField(10);

JButton bt1 = new JButton("提 交");

JButton bt2 = new JButton("取 消");
Icon img=new ImageIcon("Pic\\welcome.gif");
public AddInterface() {

layout();

}
public void layout() {
this.setTitle("软五班添加成员");
this.setSize(280, 400);
this.setLayout(new GridLayout(7,1));
JLabel image=new JLabel(img);
jp1.setLayout(new BorderLayout());
jp1.add(image,"Center");
jp2.add(lb1);
jp2.add(tf1);
jp3.add(lb2);
jp3.add(rb1);
jp3.add(rb2);
jp4.add(lb3);
jp4.add(cb1);
jp5.add(lb4);
jp5.add(cb2);
jp6.add(lb5);
jp6.add(tf2);
jp7.add(lb6);
jp7.add(tf3);
jp8.add(bt1);
jp8.add(bt2);

this.add(jp1,"North");
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
this.add(jp8);
this.setLocationRelativeTo(null);
this.setVisible(true);

}
}

就这么一目了然的代码,居然报错???
...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fy_kenny 2008-03-19
  • 打赏
  • 举报
回复
jp1.add(image,"Center");
jp2.add(lb1);
jp2.add(tf1);
jp3.add(lb2);
jp3.add(rb1);
jp3.add(rb2);
jp4.add(lb3);
jp4.add(cb1);
jp5.add(lb4);
jp5.add(cb2);
jp6.add(lb5);
jp6.add(tf2);
jp7.add(lb6);
jp7.add(tf3);
jp8.add(bt1);
jp8.add(bt2);

this.add(jp1,"North");
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
this.add(jp8);

1。可能由于你 重写了方法layout(),所以在你往JPanel上放完其他控件后,又把他们放到了 this上。 这样说不对。

2。首先,layout()方法是用于刷新界面的。由于你 重写了方法layout(),在 layout()方法又使用了this.setLayout()。这样可能又引发了调用layout()进行刷新。从而不停循环导致溢出。

3。原理大致和 第二种情况相同。由于this.setVisible(true)引起调用layout()方法。

具体我也不清楚只是我的推测。仅供参考。希望能对你有所帮助。
favorite7w 2008-03-17
  • 打赏
  • 举报
回复
lz把JFrame原有的layout方法给覆盖了,按照2楼的做法应该就行啦。
nihuajie05 2008-03-17
  • 打赏
  • 举报
回复
貌似我没有看见递归...
老紫竹 2008-03-17
  • 打赏
  • 举报
回复
你把loyout,改称
  public AddInterface() {
init();
}

public void init() {
老紫竹 2008-03-17
  • 打赏
  • 举报
回复
循环递归调用,但我没看到返回或终止的条件!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧