在BookSalesFrame类的button1.addActionListener方法中创建Sale类的对象salebook并调用其方法getInfo(),但报错找不到getInfo(), 我在类TestProg(main的入口)使用Sale类创建对象salebook并调用其方法getInfo()又没问题。。是因为在那个函数中不能创建对象还是我的类代码有问题。。其它文件都编译通过了。。纠结。。大侠们赐教啊。。book.txt中内容:
0001 语文 25.0
0002 数学 32.5
0003 英语 28.5
0004 政治 21.8 package BookSales;public class Book
{
String name;
String code;
String price;
String quantity;
public Book(){};
public Book(String code,String name,String price,String quantity)
{
this.code=code;
this.name=name;
this.price=price;
this.quantity=quantity;
}
public Book(String code) //根据code创建book对象
{
BookList bl=new BookList();
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
Book book=bl.getVector().elementAt(i);
if(code==book.getCode())
{
this.code=book.getCode();
this.quantity=book.getQuantity();
this.name=book.getName();
this.price=book.getPrice();
}
return;
}
}
public void setName(String name) //设置成员变量
{
this.name = name;
}
public void setCode(String code)
{
this.code =code; //数据类型转换
}
public void setPrice(String price)
{
this.price = price;
}
public void setQuantity(String quantity)
{
this.quantity = quantity;//数据类型转换
}
public String getCode() //访问成员变量
{
return code;
}
public String getName()
{
return name;
}
public String getPrice()
{
return price;
}
public String getQuantity()
{
return quantity;
}
public void show()
{
System.out.print(name+"\t"+price+"\t"+code+"\n");
}}package BookSales;
import java.util.List;
import java.util.*;
import java.io.*;
import java.util.ArrayList; public class BookList
{
private Vector<Book> bookList=new Vector(); //使用List类数据结构储存书的列表
int amount=100; public Vector<Book> getVector()
{
return this.bookList;
}
/**
*读取文件
*创建Book类vector,将文件信息赋给vector的元素
**/
public void readText()
{
File f = new File("book.txt");
if(!f.exists()) //判断文件是否可读存在
{
System.out.println("文件不存在");
return;
}
try {
FileReader fr = new FileReader(f);
BufferedReader b = new BufferedReader(fr);
String line = b.readLine(); //按行读取
while (line != null)
{
bookList.add(splitString(line));
line = b.readLine();
}
System.out.print(bookList.size());
}
catch (FileNotFoundException e)
{
System.out.println("文件不存在");
} catch (IOException e)
{
System.out.println("输入流异常!");
}
} private Book splitString(String s)
{
Book book = new Book();
StringTokenizer t=new StringTokenizer(s); //字符串分析
int m=t.countTokens();
for(int i=0;i<m;i++)
{
switch(i)
{
case 0: book.setCode(t.nextToken());break;
case 1: book.setName(t.nextToken());break;
case 2: book.setPrice(t.nextToken());break;
}
}
return book;
}
}
package BookSales;
import java.lang.*;
public class Sale
{
public Book sbook; //要卖出的书
public BookList bl; //库存列表
String stuff;
int quantity; //整型!!
int total; //每种书总收入
public Sale(){}; public Sale(String code,String stuff,String quantity) //记录的每一笔交易
{
sbook=new Book();//!!!
sbook.code=code;
this.stuff=stuff;
this.quantity=Integer.parseInt(quantity);
total=Integer.parseInt(sbook.price)*this.quantity;//总收入
}
public Book getSalebook()
{
return sbook;
}
public int getQuantity()
{
return quantity;
}
public void getInfo()//获取总库存信息
{
bl=new BookList();//!!!
sbook=new Book();
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(bl.getVector().get(i).getQuantity());
}
}
public void saleBook(String code)//根据code来设置销售信息
{
BookList bl=new BookList();//!!!
sbook=new Book();//!!!
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
if(bl.getVector().get(i).getCode()==code)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(""+(quantity+=Integer.parseInt(bl.getVector().get(i).getPrice())));
}
}
}
}
0001 语文 25.0
0002 数学 32.5
0003 英语 28.5
0004 政治 21.8 package BookSales;public class Book
{
String name;
String code;
String price;
String quantity;
public Book(){};
public Book(String code,String name,String price,String quantity)
{
this.code=code;
this.name=name;
this.price=price;
this.quantity=quantity;
}
public Book(String code) //根据code创建book对象
{
BookList bl=new BookList();
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
Book book=bl.getVector().elementAt(i);
if(code==book.getCode())
{
this.code=book.getCode();
this.quantity=book.getQuantity();
this.name=book.getName();
this.price=book.getPrice();
}
return;
}
}
public void setName(String name) //设置成员变量
{
this.name = name;
}
public void setCode(String code)
{
this.code =code; //数据类型转换
}
public void setPrice(String price)
{
this.price = price;
}
public void setQuantity(String quantity)
{
this.quantity = quantity;//数据类型转换
}
public String getCode() //访问成员变量
{
return code;
}
public String getName()
{
return name;
}
public String getPrice()
{
return price;
}
public String getQuantity()
{
return quantity;
}
public void show()
{
System.out.print(name+"\t"+price+"\t"+code+"\n");
}}package BookSales;
import java.util.List;
import java.util.*;
import java.io.*;
import java.util.ArrayList; public class BookList
{
private Vector<Book> bookList=new Vector(); //使用List类数据结构储存书的列表
int amount=100; public Vector<Book> getVector()
{
return this.bookList;
}
/**
*读取文件
*创建Book类vector,将文件信息赋给vector的元素
**/
public void readText()
{
File f = new File("book.txt");
if(!f.exists()) //判断文件是否可读存在
{
System.out.println("文件不存在");
return;
}
try {
FileReader fr = new FileReader(f);
BufferedReader b = new BufferedReader(fr);
String line = b.readLine(); //按行读取
while (line != null)
{
bookList.add(splitString(line));
line = b.readLine();
}
System.out.print(bookList.size());
}
catch (FileNotFoundException e)
{
System.out.println("文件不存在");
} catch (IOException e)
{
System.out.println("输入流异常!");
}
} private Book splitString(String s)
{
Book book = new Book();
StringTokenizer t=new StringTokenizer(s); //字符串分析
int m=t.countTokens();
for(int i=0;i<m;i++)
{
switch(i)
{
case 0: book.setCode(t.nextToken());break;
case 1: book.setName(t.nextToken());break;
case 2: book.setPrice(t.nextToken());break;
}
}
return book;
}
}
package BookSales;
import java.lang.*;
public class Sale
{
public Book sbook; //要卖出的书
public BookList bl; //库存列表
String stuff;
int quantity; //整型!!
int total; //每种书总收入
public Sale(){}; public Sale(String code,String stuff,String quantity) //记录的每一笔交易
{
sbook=new Book();//!!!
sbook.code=code;
this.stuff=stuff;
this.quantity=Integer.parseInt(quantity);
total=Integer.parseInt(sbook.price)*this.quantity;//总收入
}
public Book getSalebook()
{
return sbook;
}
public int getQuantity()
{
return quantity;
}
public void getInfo()//获取总库存信息
{
bl=new BookList();//!!!
sbook=new Book();
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(bl.getVector().get(i).getQuantity());
}
}
public void saleBook(String code)//根据code来设置销售信息
{
BookList bl=new BookList();//!!!
sbook=new Book();//!!!
bl.readText();
for(int i=0;i<bl.getVector().size();i++)
{
if(bl.getVector().get(i).getCode()==code)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(""+(quantity+=Integer.parseInt(bl.getVector().get(i).getPrice())));
}
}
}
}
解决方案 »
- 初学java,弱弱的问一下,类名.class是啥意思?
- 我用了一个只支持GBK的插件,想把最后执行的串显示成UTF-8形式的怎么办
- 有没有什么可以让一张图片绕着某个点旋转的方法?
- JFileChooser 使用的问题
- 怎么画不出指定色彩的圆,请各位帮帮忙看哪儿错了
- 如何知道Connection对象正在连接什么数据库?
- 请教大侠,JAVA中如何实现A类调用B类中的main方法
- 有没有人做过协同开发的项目-----关于共享屏幕的问题!小女子在线等待!!
- 怎样在while循环外取值
- splash screen命令行调试OK,生产jar运行时SplashScreen.getSplashScreen()总是返回null
- 关于openjdk的用处,谁能给讲讲
- 新手求教!大侠们伸下援手吧!!!关于类与对象的问题,是在事件方法中一般类的对象创建和方法使用。。。
package BookSales;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;public class BookSalesFrame extends java.awt.Frame
{
private Frame frame;
private TextField bookcode,stuff,bookquantity;
private Panel panel1;
private Button button1,button2,button3,button4,button5;
private Label label1,label2,label3;
private TextArea textArea1;
public BookList bookList;///////
public BookSalesFrame() //默认构造函数
{
initComponents();
addListenner();
}
private void initComponents() //各组件初始化
{
panel1 = new Panel();
button1 = new Button("Process Sales");
button2 = new Button("Clear");
label1 = new Label("Staff");
label2 = new Label("Book Code");
label3 = new Label("Quantity");
bookcode = new TextField();
stuff = new TextField();
bookquantity = new TextField();
textArea1 = new TextArea();
button3 = new Button("Show Book");
button4 = new Button("Show Chart");
button5 = new Button("Save and Exit");
panel1.setLayout(null); //布局为null
add(panel1, java.awt.BorderLayout.CENTER);
panel1.add(button1);
button1.setBounds(70, 330, 110, 40);
panel1.add(button2);
button2.setBounds(70, 280, 110, 40); panel1.add(label1); //label
label1.setBounds(300, 280, 70, 30);
panel1.add(label2);
label2.setBounds(300, 310, 70, 30);
panel1.add(label3);
label3.setBounds(300, 340, 70, 30); panel1.add(bookcode); //bookcode
stuff.setBounds(370, 340, 110, 30);
panel1.add(stuff);
bookcode.setBounds(370, 280, 110, 30);
panel1.add(bookquantity);
bookquantity.setBounds(370, 310, 110, 30);
panel1.add(textArea1);
textArea1.setBounds(0, 60, 540, 220); panel1.add(button3); //button3-5
button3.setBounds(360, 30, 100, 30);
panel1.add(button4);
button4.setBounds(60, 30, 100, 30);
panel1.add(button5);
button5.setBounds(210, 30, 100, 30);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
pack();
}
private void exitForm(WindowEvent evt)
{
System.exit(0);
}
private boolean isNumber(String s) //判断字符串内容是不是整数
{
try{
int c=Integer.parseInt(stuff.getText());
}
catch(NumberFormatException ne)
{
return false;
}
return true;
}
private void addListenner() //各组件事件
{
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bookcode.setText("");
stuff.setText("");
bookquantity.setText("");
}
});
button1.addActionListener(new ActionListener()
{ public Sale salebook=new Sale(); //这里的对象不能创建吗?
public sa.getInfo(); //为什么这个方法没有用?
for(int i=0;i<bl.getVector().size();i++)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(bl.getVector().get(i).getQuantity());
} public void actionPerformed(ActionEvent e)
{
if (bookcode.getText().equals("") || stuff.getText().equals("")||bookquantity.getText().equals("")) //判断是否输入完整
{
JOptionPane.showMessageDialog(panel1,"输入不完整!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else{
if(isNumber(bookcode.getText())&&isNumber(bookquantity.getText())) //判断code、quantity是否是数字
{
if(salebook.getSalebook().getCode()==bookcode.getText())
{
if(100-salebook.getQuantity()>0)
textArea1.append( stuff.getText()+ "\t\t" + bookcode.getText() + " \t\t" + bookquantity.getText()+"\n");//输出
else JOptionPane.showMessageDialog(panel1,"库存不足!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else JOptionPane.showMessageDialog(panel1,"无库存!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(panel1,"输入格式有误!","ERROR", JOptionPane.ERROR_MESSAGE);
}
}
});
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BookFrame bf=new BookFrame();
//bf.setVisible(true);
BookList bl=new BookList();
bl.readText();
bf.getTextArea().setText(bl.getVector().get(1).getCode());
bf.show(bl.getVector().get(1).getCode());
System.out.print(bl.getVector().get(1).getCode()+"\t"+bl.getVector().get(1).getName());
}
});
}
}
package BookSales;
import java.io.*;
import java.util.*;
public class TestProg
{
public static void main(String args[])
{
BookSalesFrame bsf=new BookSalesFrame();
bsf.setSize(550,430);
bsf.setLocation(100,100);
bsf.show();
System.out.print("清单:");
BookList boo=new BookList();
boo.readText();
for(int i=0;i<boo.getVector().size();i++)
{
boo.getVector().elementAt(i).show();
}
Sale sa=new Sale(); //在这里都可以创建对象和使用方法getInfo()
sa.getInfo(); //
for(int i=0;i<sa.bl.getVector().size();i++)
{
System.out.println(sa.bl.getVector().get(i).getName());
} }
}
package BookSales;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;public class BookSalesFrame extends java.awt.Frame
{
private Frame frame;
private TextField bookcode,stuff,bookquantity;
private Panel panel1;
private Button button1,button2,button3,button4,button5;
private Label label1,label2,label3;
private TextArea textArea1;
public BookList bookList;///////
public BookSalesFrame() //默认构造函数
{
initComponents();
addListenner();
}
private void initComponents() //各组件初始化
{
panel1 = new Panel();
button1 = new Button("Process Sales");
button2 = new Button("Clear");
label1 = new Label("Staff");
label2 = new Label("Book Code");
label3 = new Label("Quantity");
bookcode = new TextField();
stuff = new TextField();
bookquantity = new TextField();
textArea1 = new TextArea();
button3 = new Button("Show Book");
button4 = new Button("Show Chart");
button5 = new Button("Save and Exit");
panel1.setLayout(null); //布局为null
add(panel1, java.awt.BorderLayout.CENTER);
panel1.add(button1);
button1.setBounds(70, 330, 110, 40);
panel1.add(button2);
button2.setBounds(70, 280, 110, 40); panel1.add(label1); //label
label1.setBounds(300, 280, 70, 30);
panel1.add(label2);
label2.setBounds(300, 310, 70, 30);
panel1.add(label3);
label3.setBounds(300, 340, 70, 30); panel1.add(bookcode); //bookcode
stuff.setBounds(370, 340, 110, 30);
panel1.add(stuff);
bookcode.setBounds(370, 280, 110, 30);
panel1.add(bookquantity);
bookquantity.setBounds(370, 310, 110, 30);
panel1.add(textArea1);
textArea1.setBounds(0, 60, 540, 220); panel1.add(button3); //button3-5
button3.setBounds(360, 30, 100, 30);
panel1.add(button4);
button4.setBounds(60, 30, 100, 30);
panel1.add(button5);
button5.setBounds(210, 30, 100, 30);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
pack();
}
private void exitForm(WindowEvent evt)
{
System.exit(0);
}
private boolean isNumber(String s) //判断字符串内容是不是整数
{
try{
int c=Integer.parseInt(stuff.getText());
}
catch(NumberFormatException ne)
{
return false;
}
return true;
}
private void addListenner() //各组件事件
{
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bookcode.setText("");
stuff.setText("");
bookquantity.setText("");
}
});
button1.addActionListener(new ActionListener()
{ public Sale salebook=new Sale(); //这里的对象不能创建吗?
public sa.getInfo(); //为什么这个方法没有用?
for(int i=0;i<bl.getVector().size();i++)
{
sbook.setCode(bl.getVector().get(i).getCode());
sbook.setName(bl.getVector().get(i).getName());
sbook.setPrice(bl.getVector().get(i).getPrice());
sbook.setQuantity(bl.getVector().get(i).getQuantity());
} public void actionPerformed(ActionEvent e)
{
if (bookcode.getText().equals("") || stuff.getText().equals("")||bookquantity.getText().equals("")) //判断是否输入完整
{
JOptionPane.showMessageDialog(panel1,"输入不完整!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else{
if(isNumber(bookcode.getText())&&isNumber(bookquantity.getText())) //判断code、quantity是否是数字
{
if(salebook.getSalebook().getCode()==bookcode.getText())
{
if(100-salebook.getQuantity()>0)
textArea1.append( stuff.getText()+ "\t\t" + bookcode.getText() + " \t\t" + bookquantity.getText()+"\n");//输出
else JOptionPane.showMessageDialog(panel1,"库存不足!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else JOptionPane.showMessageDialog(panel1,"无库存!","ERROR", JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(panel1,"输入格式有误!","ERROR", JOptionPane.ERROR_MESSAGE);
}
}
});
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BookFrame bf=new BookFrame();
//bf.setVisible(true);
BookList bl=new BookList();
bl.readText();
bf.getTextArea().setText(bl.getVector().get(1).getCode());
bf.show(bl.getVector().get(1).getCode());
System.out.print(bl.getVector().get(1).getCode()+"\t"+bl.getVector().get(1).getName());
}
});
}
}
package BookSales;
import java.io.*;
import java.util.*;
public class TestProg
{
public static void main(String args[])
{
BookSalesFrame bsf=new BookSalesFrame();
bsf.setSize(550,430);
bsf.setLocation(100,100);
bsf.show();
System.out.print("清单:");
BookList boo=new BookList();
boo.readText();
for(int i=0;i<boo.getVector().size();i++)
{
boo.getVector().elementAt(i).show();
}
Sale sa=new Sale(); //在这里都可以创建对象和使用方法getInfo()
sa.getInfo(); //
for(int i=0;i<sa.bl.getVector().size();i++)
{
System.out.println(sa.bl.getVector().get(i).getName());
} }
}