| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 646 人关注过本帖
标题:求助,照着老师敲的代码,只是有些小改动而已,出现不能读取问题。
只看楼主 加入收藏
夕痕
Rank: 1
来 自:临界
等 级:新手上路
帖 子:40
专家分:3
注 册:2013-4-22
结帖率:90%
收藏
已结贴  问题点数:15 回复次数:9 
求助,照着老师敲的代码,只是有些小改动而已,出现不能读取问题。
写的数据都能存进该txt文档中,就是当点击显示的时候不能读取出来。
代码的要求是:每敲进一组数据都保存,然后再让其在文本域中每行显示一组
程序代码:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import import import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class shang extends JFrame implements ActionListener,DocumentListener
{
    File file = null;   
    MenuBar menubar;
    Menu menu;
    MenuItem menuitem1,menuitem2,menuitem3;
    JTextArea textarea;
    JPanel panel1,panel2,panel3;
    Button button1,button2;
    Box basebox,box1,box2;
    JTextField textfield1,textfield2,textfield3,textfield4,textfield5;
    CardLayout card;
    Boolean t = false;
    Boolean k = false;
//    JLabel l = null;
   
    public shang()
    {
        menubar = new MenuBar();
        file = new File("记录.txt");
        this.setMenuBar(menubar);
        menu = new Menu("菜单选项");
        menuitem1 = new MenuItem("录入");
        menuitem2 = new MenuItem("显示");
        menuitem3 = new MenuItem("主页面");
       
        card = new CardLayout();
        panel1 = new JPanel();
        panel3 = new JPanel();
        panel2 = new JPanel();
        JPanel jpanel = new JPanel();
    //    l = new JLabel();
    //    l = new JLabel("信息查询",JLabel.CENTER);
    //    l.setIcon(new ImageIcon("welcome.jpg"));
    //    l.setFont(new Font("宋体",Font.PLAIN,18));
    //    l.setHorizontalTextPosition(SwingConstants.CENTER);
    //    l.setForeground(Color.red);
        panel1.setLayout(card);
        panel3.setBackground(Color.darkGray);
        panel2.setLayout(new BorderLayout());
        textarea = new JTextArea(350,350);            //文本域
        JTextArea text1 = new JTextArea(2,20);
        JScrollPane text = new JScrollPane(text1);            //滚动条
        menubar.add(menu);
        menu.add(menuitem3);
        menu.add(menuitem1);
        menu.add(menuitem2);
       
        button1 = new Button("录入");
        button2 = new Button("显示");
   
        textfield1 = new JTextField(15);
        textfield2 = new JTextField(15);
        textfield3 = new JTextField(15);
        textfield4 = new JTextField(15);
        textfield5 = new JTextField(15);
        textfield1.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield2.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield3.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield4.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield5.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield3.getDocument().addDocumentListener(this);
        textfield4.getDocument().addDocumentListener(this);
   
   
        box1 = Box.createVerticalBox();
        box1.add(new Label("货物编号"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("货物名称"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("单价"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("数量"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("金额"));
        box1.add(Box.createVerticalStrut(11));
        box2 = Box.createVerticalBox();
        box2.add(textfield1);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield2);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield3);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield4);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield5);
        box2.add(Box.createVerticalStrut(8));
        basebox = Box.createHorizontalBox();
        basebox.add(box1);
        basebox.add(Box.createHorizontalStrut(15));
        basebox.add(box2);
        panel3.add(basebox);
        jpanel.add(button1);
        jpanel.add(button2);
        panel2.add(jpanel,BorderLayout.SOUTH);
        panel2.add(panel3,BorderLayout.CENTER);
//        panel1.add(l);
        panel1.add(panel2);
        panel1.add(text);
//        panel1.add("1",l);
        panel1.add("2",panel2);
        panel1.add("3",text);
       
        this.add(panel1,BorderLayout.CENTER);
        setSize(400,400);
        setLocation(300,85);
        setVisible(true);
   
        menuitem1.addActionListener(this);
        menuitem2.addActionListener(this);
        menuitem3.addActionListener(this);
        button1.addActionListener(this);
        button2.addActionListener(this);
       
   
        addWindowListener(new WindowAdapter()
        {
             public void windowClosing( WindowEvent e )
             {
                 System.exit( 0 );
             }
        });
    }
    public void actionPerformed(ActionEvent e)
    {
//        if(e.getSource() == menuitem3)
//            cl.show(panel1,"1");
        if(e.getSource() ==menuitem1 || e.getSource() == button1)
        {
            card.show(panel1,"2");
            t = true;
        //    k = true;
        }
        if(t)                    //写入数据
        {
            try{
                RandomAccessFile out = new RandomAccessFile(file, "rw");
                if(file.exists())
                {
                    long length = file.length();
                    out.seek(length);
                }
                out.writeUTF("货物编号"+textfield1.getText());
                out.writeUTF("货物名称"+textfield2.getText());
                out.writeUTF("单价"+textfield3.getText());
                out.writeUTF("数量"+textfield4.getText());
                out.writeUTF("金额"+textfield5.getText());
                out.close();
            }catch(Exception a)
            {}
    /*        f1.setText("");
            f2.setText("");
            f3.setText("");
            f4.setText("");
            f5.setText("");    */
        }
        if(e.getSource() ==menuitem2 || e.getSource() == button2)
        {
            if(textfield1.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "编号不能为空");                           
                else if(textfield2.getText().isEmpty())
                    JOptionPane.showMessageDialog(null, "名称不能为空");
                    else if(textfield3.getText().isEmpty())
                        JOptionPane.showMessageDialog(null, "单价不能为空");
                        else if(textfield4.getText().isEmpty())
                            JOptionPane.showMessageDialog(null, "数量不能为空");
                            else if(textfield5.getText().isEmpty())
                                JOptionPane.showMessageDialog(null, "金额不能为空");
            else
            {
                card.show(panel1,"3");        //转至文本域
                t = false;
                textarea.setText(null);
                int count = 1;
                try                    //读取数据
                {
                    String file = null;
                    RandomAccessFile in = new RandomAccessFile(file,"r");
                    String 货物编号 = null;
                    while((货物编号 = in.readUTF())!=null)
                    {
                        textarea.append("\n"+count+" "+货物编号);
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append("\n-----------------------------");
                        count ++ ;
                    }
                    in.close();
                }catch(Exception a)
                {}
            }
        }
    }

    public static void main(String[] shang)
    {
        new shang();
    }
    public void changedUpdate(DocumentEvent e)
    {
        doChange();
    }
    public void insertUpdate(DocumentEvent e)
    {
        doChange();
    }
    public void removeUpdate(DocumentEvent e)
    {
        doChange();
    }
    private void doChange()
    {
        try
        {
            int n1 = Integer.parseInt(textfield3.getText());
            int n2 = Integer.parseInt(textfield4.getText());
            textfield5.setText(String.valueOf(n1*n2));
        } catch (Exception ee)
        {}
    }
}

搜索更多相关主题的帖子: import 
2013-11-12 17:14
夕痕
Rank: 1
来 自:临界
等 级:新手上路
帖 子:40
专家分:3
注 册:2013-4-22
收藏
得分:0 
不会大神都休息了吧。

爱的本质是赋予一个人伤害自己的权利。
2013-11-12 22:02
xstar海绵
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:26
专家分:160
注 册:2013-11-3
收藏
得分:6 
JScrollPane text = new JScrollPane(text1);  text1 改成 testarea
2013-11-12 22:52
夕痕
Rank: 1
来 自:临界
等 级:新手上路
帖 子:40
专家分:3
注 册:2013-4-22
收藏
得分:0 
回复 3楼 xstar海绵
貌似你说的这个无关吧。

爱的本质是赋予一个人伤害自己的权利。
2013-11-13 12:37
zjcl817
Rank: 4
来 自:残狼部落
等 级:业余侠客
威 望:2
帖 子:68
专家分:206
注 册:2013-11-11
收藏
得分:6 
isEmpty()这个方法,找不到!
2013-11-13 17:53
夕痕
Rank: 1
来 自:临界
等 级:新手上路
帖 子:40
专家分:3
注 册:2013-4-22
收藏
得分:0 
唉,没人能仔细的讲解么?本人能确定实在读取数据的时候出了问题。

爱的本质是赋予一个人伤害自己的权利。
2013-11-13 20:14
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
等下晚上回去帮你看看

学海无涯#¥%……&*(
2013-11-18 17:02
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
程序代码:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import   javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class shang extends JFrame implements ActionListener, DocumentListener {
    private static final long serialVersionUID = 1L;
    File file = null;
    MenuBar menubar;
    Menu menu;
    MenuItem menuitem1, menuitem2, menuitem3;
    JTextArea textarea;
    JPanel panel1, panel2, panel3;
    Button button1, button2;
    Box basebox, box1, box2;
    JTextField textfield1, textfield2, textfield3, textfield4, textfield5;
    CardLayout card;
    Boolean t = false;
    Boolean k = false;

    public shang() {
        menubar = new MenuBar();
        file = new File("记录.txt");
        this.setMenuBar(menubar);
        menu = new Menu("菜单选项");
        menuitem1 = new MenuItem("录入");
        menuitem2 = new MenuItem("显示");
        menuitem3 = new MenuItem("主页面");
        card = new CardLayout();
        panel1 = new JPanel();
        panel3 = new JPanel();
        panel2 = new JPanel();
        JPanel jpanel = new JPanel();
        panel1.setLayout(card);
        panel3.setBackground(Color.darkGray);
        panel2.setLayout(new BorderLayout());
        textarea = new JTextArea(350, 350); // 文本域
        JTextArea text1 = new JTextArea(2, 20);
        JScrollPane text = new JScrollPane(text1); // 滚动条
        menubar.add(menu);
        menu.add(menuitem3);
        menu.add(menuitem1);
        menu.add(menuitem2);
        button1 = new Button("录入");
        button2 = new Button("显示");
        textfield1 = new JTextField(15);
        textfield2 = new JTextField(15);
        textfield3 = new JTextField(15);
        textfield4 = new JTextField(15);
        textfield5 = new JTextField(15);
        textfield1.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield2.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield3.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield4.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield5.setFont(new Font("宋体", Font.PLAIN, 14));
        textfield3.getDocument().addDocumentListener(this);
        textfield4.getDocument().addDocumentListener(this);

        box1 = Box.createVerticalBox();
        box1.add(new Label("货物编号"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("货物名称"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("单价"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("数量"));
        box1.add(Box.createVerticalStrut(11));
        box1.add(new Label("金额"));
        box1.add(Box.createVerticalStrut(11));
        box2 = Box.createVerticalBox();
        box2.add(textfield1);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield2);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield3);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield4);
        box2.add(Box.createVerticalStrut(8));
        box2.add(textfield5);
        box2.add(Box.createVerticalStrut(8));
        basebox = Box.createHorizontalBox();
        basebox.add(box1);
        basebox.add(Box.createHorizontalStrut(15));
        basebox.add(box2);
        panel3.add(basebox);
        jpanel.add(button1);
        jpanel.add(button2);
        panel2.add(jpanel, BorderLayout.SOUTH);
        panel2.add(panel3, BorderLayout.CENTER);
        panel1.add(panel2);//这里添加的参数不合法,你是不是写错了,这里添加的应该是控件什么的,你加的是有一个jpanel,
        //如果不是这个问题,你就看看有关于这种窗口嵌套是怎么设置的
        panel1.add(text);
        panel1.add("2", panel2);
        panel1.add("3", text);

        this.add(panel1, BorderLayout.CENTER);
        setSize(400, 400);
        setLocation(300, 85);
        setVisible(true);
        menuitem1.addActionListener(this);
        menuitem2.addActionListener(this);
        menuitem3.addActionListener(this);
        button1.addActionListener(this);
        button2.addActionListener(this);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuitem1 || e.getSource() == button1) {
            card.show(panel1, "2");
            t = true;
            // k = true;
        }
        // 写入数据
        if (t) {
            try {
                RandomAccessFile out = new RandomAccessFile(file, "rw");
                if (file.exists()) {
                    long length = file.length();
                    out.seek(length);
                }
                out.writeUTF("货物编号" + textfield1.getText());
                out.writeUTF("货物名称" + textfield2.getText());
                out.writeUTF("单价" + textfield3.getText());
                out.writeUTF("数量" + textfield4.getText());
                out.writeUTF("金额" + textfield5.getText());
                out.close();
            } catch (Exception a) {
            }
        }
        if (e.getSource() == menuitem2 || e.getSource() == button2) {
            if (textfield1.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "编号不能为空");
            else if (textfield2.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "名称不能为空");
            else if (textfield3.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "单价不能为空");
            else if (textfield4.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "数量不能为空");
            else if (textfield5.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "金额不能为空");
            else {
                card.show(panel1, "3"); // 转至文本域
                t = false;
                textarea.setText(null);
                int count = 1;
                try // 读取数据
                {
                    String file = null;
                    RandomAccessFile in = new RandomAccessFile(file, "r");
                    String 货物编号 = null;
                    while ((货物编号 = in.readUTF()) != null) {
                        textarea.append("\n" + count + " " + 货物编号);
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append(in.readUTF());
                        textarea.append("\n-----------------------------");
                        count++;
                    }
                    in.close();
                } catch (Exception a) {
                }
            }
        }
    }
    public static void main(String[] shang) {
        new shang(); //此内方法又问题
    }
    public void changedUpdate(DocumentEvent e) {
        doChange();
    }
    public void insertUpdate(DocumentEvent e) {
        doChange();
    }
    public void removeUpdate(DocumentEvent e) {
        doChange();
    }
    private void doChange() {
        try {
            int n1 = Integer.parseInt(textfield3.getText());
            int n2 = Integer.parseInt(textfield4.getText());
            textfield5.setText(String.valueOf(n1 * n2));
        } catch (Exception ee) {
        }
    }
}

学海无涯#¥%……&*(
2013-11-19 12:57
夕痕
Rank: 1
来 自:临界
等 级:新手上路
帖 子:40
专家分:3
注 册:2013-4-22
收藏
得分:0 
回复 7楼 java小蚂蚁
,谢了!

爱的本质是赋予一个人伤害自己的权利。
2013-11-19 12:57
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
panel1.add(panel2);//这里添加的参数不合法,你是不是写错了,这里添加的应该是控件什么的,你加的是有一个jpanel,
        //如果不是这个问题,你就看看有关于这种窗口嵌套是怎么设置的

学海无涯#¥%……&*(
2013-11-19 12:57
快速回复:求助,照着老师敲的代码,只是有些小改动而已,出现不能读取问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026709 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved