碰到一个问题!百思不得其解!高手帮忙!!

raigor235 2008-04-13 10:15:41
package wuchunzhi;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class wuchunzhi
{
public static void main(String[] args)
{
shujukuFrame f=new shujukuFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

}
class shujukuFrame extends JFrame
{
public shujukuFrame()
{
setResizable(false);
setTitle("电话薄实验");
setLocation(300,300);
setSize(546,153);

shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3();

add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
}
}
class shujukuPanel2 extends JPanel
{
private JTextField text1,text2,text3,text4,text5;
public shujukuPanel2()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
text1=new JTextField(43);text2=new JTextField(43);text3=new JTextField(16);
text4=new JTextField(9);text5=new JTextField(9);
add(new JLabel("姓名:"));add(text1);add(new JLabel("地址:"));add(text2);
add(new JLabel("城市:"));add(text3);add(new JLabel("省份:"));add(text4);
add(new JLabel("邮编:"));add(text5);}

public String getString()
{

return text1.getText()+"wo";
}
}
class shujukuPanel3 extends JPanel implements ActionListener
{
private JButton button1,button2,button3,button4,button5;
shujukuPanel2 p2=new shujukuPanel2();
public void add(String s)
{
try{
FileWriter name = new FileWriter("name.txt");
name.write(s);
}
catch(IOException ex){
}
}
public shujukuPanel3()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
button1=new JButton("添加记录");
button2=new JButton("第一条记录");
button3=new JButton("下一条记录");
button4=new JButton("前一条记录");
button5=new JButton("最末一条记录");
add(button1);add(button2);add(button3);add(button4);add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==button1){


System.out.println(p2.getString());
add(p2.getString());

}
}
}
上面是我写的程序,功能是向一个文件写入文本文档,这些都不是关键.关键的是为什么我在控制台上只能看到"wo"这个字符串(这个"wo"字符串是我加上测试的)而看不到text1.getText也就是文本域输入的内容.拜托各位高手不还要叫我怎么修改,我只想知道我是怎么错的,不想知道是怎么对的.
...全文
176 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
raigor235 2008-04-28
  • 打赏
  • 举报
回复
不知道  humsuccess 还在不在 !前段时间因为 在css上遇到了难题!所以把这个问题给丢了一段时间!!今天试了一下你的代码!!感觉有点开窍了!!不过还是有个疑问!既然p2 和p3没有通信,那怎么又会能够看见"wo"这个字符串呢!???
awusoft 2008-04-28
  • 打赏
  • 举报
回复
关于出现"wo"是因为不管哪个shujukuPanel2对像都会出现"wo"字.

在添加到面板的时候有一个shujukuPanel2对像,在事件对像又有一个shujukuPanel2对像.但事件里的与添加到面板的不是同一个对像.两个对像都会有"wo"出现.实际中,事件里的shujukuPanel2对像,应该是添加到时候的shujukuPanel2对像
awusoft 2008-04-28
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class wuchunzhi
{
public static void main(String[] args)
{
shujukuFrame f=new shujukuFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

}
class shujukuFrame extends JFrame
{
public shujukuFrame()
{
setResizable(false);
setTitle("电话薄实验");
setLocation(300,300);
setSize(546,153);

shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3();
p3.setP2(p2);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
}
}
class shujukuPanel2 extends JPanel
{
private JTextField text1,text2,text3,text4,text5;
public shujukuPanel2()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
text1=new JTextField(43);
text2=new JTextField(43);
text3=new JTextField(16);
text4=new JTextField(9);
text5=new JTextField(9);
add(new JLabel("姓名:"));
add(text1);
add(new JLabel("地址:"));
add(text2);
add(new JLabel("城市:"));
add(text3);add(new JLabel("省份:"));
add(text4);
add(new JLabel("邮编:"));
add(text5);}

public String getString()
{

return text1.getText()+"wo";
}
}
class shujukuPanel3 extends JPanel implements ActionListener
{
private JButton button1,button2,button3,button4,button5;

shujukuPanel2 p2=null;

public shujukuPanel2 getP2() {
return p2;
}
public void setP2(shujukuPanel2 p2) {
this.p2 = p2;
}

public void add(String s)
{
try{
FileWriter name = new FileWriter("name.txt");
name.write(s);
}
catch(IOException ex){
}
}
public shujukuPanel3()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
button1=new JButton("添加记录");
button2=new JButton("第一条记录");
button3=new JButton("下一条记录");
button4=new JButton("前一条记录");
button5=new JButton("最末一条记录");
add(button1);add(button2);add(button3);add(button4);add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==button1){
System.out.println(p2.getString());
add(p2.getString());
}
}
}
awusoft 2008-04-28
  • 打赏
  • 举报
回复

代码中两次出现了p2,不是对一同像.
shujukuPanel2 p2=new shujukuPanel2();



shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3();
raigor235 2008-04-28
  • 打赏
  • 举报
回复
我觉得是不是这样的!!在我的代码中!中是声明了shujukuPanel2的引用p2而没有为这个引用分配对象.所以这个引用的对象为空,所以才不能调用shujukuPanel2的构造方法.这只是我 猜测的!!希望有高手能够指点一二啊!
hmsuccess 2008-04-15
  • 打赏
  • 举报
回复
关键是shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3(p2);
这两句,因为两个类没有相互通信
hmsuccess 2008-04-15
  • 打赏
  • 举报
回复
按照我改的是可以的,控制台应该输出姓名+“wo”,你用上面的两个来替换你原来的
raigor235 2008-04-14
  • 打赏
  • 举报
回复
各位大哥!不是那样的!!关键是我连在控制台上都看不到text1.getText啊!!拜托各位大哥给看仔细点!!求救!!!
hmsuccess 2008-04-13
  • 打赏
  • 举报
回复


package guitest;


import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;



class shujukuPanel3 extends JPanel implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton button1,button2,button3,button4,button5;
shujukuPanel2 p2;
public void add(String s)
{
try{
FileOutputStream name = new FileOutputStream("name.txt",true);
name.write(s.getBytes(), 0, s.getBytes().length);
name.write("\n".getBytes());
name.flush();
name.close();
}
catch(IOException ex){
}
}
public shujukuPanel3(shujukuPanel2 p2)
{
setLayout(new FlowLayout(FlowLayout.LEFT));
button1=new JButton("添加记录");
button2=new JButton("第一条记录");
button3=new JButton("下一条记录");
button4=new JButton("前一条记录");
button5=new JButton("最末一条记录");
add(button1);add(button2);add(button3);add(button4);add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
this.p2 = p2;
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==button1){


System.out.println(p2.getString());
add(p2.getString());

}
}
}



package guitest;
import java.awt.BorderLayout;

import javax.swing.*;

class shujukuFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;

public shujukuFrame()
{
setResizable(false);
setTitle("电话薄实验");
setLocation(300,300);
setSize(546,153);

shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3(p2);

add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
}
}



我帮你改了一下
J_Factory 2008-04-13
  • 打赏
  • 举报
回复
在所有写操作完了之后分别调用name.flush()和name.close(),你自己找个合适的位置加进去,流要关闭了东西才会全写到文件里去
hmsuccess 2008-04-13
  • 打赏
  • 举报
回复

shujukuPanel2 p2=new shujukuPanel2();
shujukuPanel3 p3=new shujukuPanel3();

62,623

社区成员

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

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