求助,照着老师敲的代码,只是有些小改动而已,出现不能读取问题。
写的数据都能存进该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 java.io.File;
import java.io.RandomAccessFile;
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 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 java.io.File;
import java.io.RandomAccessFile;
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
----------------解决方案--------------------------------------------------------
不会大神都休息了吧。
----------------解决方案--------------------------------------------------------
JScrollPane text = new JScrollPane(text1); text1 改成 testarea
----------------解决方案--------------------------------------------------------
回复 3楼 xstar海绵
貌似你说的这个无关吧。 ----------------解决方案--------------------------------------------------------
isEmpty()这个方法,找不到!
----------------解决方案--------------------------------------------------------
唉,没人能仔细的讲解么?本人能确定实在读取数据的时候出了问题。
----------------解决方案--------------------------------------------------------