我是java的新手。我正在研究课程项目,该课程将在文本文件中查找书籍并显示有关它们的信息。主要是如果这本书有货。文本文件的设置如下:{ISBN,作者,类型,股票}
我编写了一个用户界面,允许用户输入ISBN,作者和类型。理想情况下,我希望用户只搜索其中一个并返回所需的信息。但是,现在只需通过ISBN搜索即可。我的代码现在只接受在文本框中输入的内容并将其显示在一个大文本框中。我对阅读文本文件有些熟悉,但不知道如何从文本框中获取文本并使用它来搜索文件。任何帮助将不胜感激。
这是我的代码:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
public class InventoryInterface extends JFrame
{
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 350;
private JButton btnSearch;
private JButton btnDatabase;
private JButton btnRefresh;
private JLabel lblISBN, lblAuthor, lblType;
private JTextField txtISBN, txtAuthor, txtType;
private JTextArea txtOutput;
public InventoryInterfaceSimple()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createComponents()
{
btnSearch = new JButton("Search");
lblISBN = new JLabel("ISBN");
lblAuthor = new JLabel("Author");
lblType = new JLabel("Type");
txtISBN = new JTextField(10);
txtAuthor = new JTextField(10);
txtType = new JTextField(10);
txtOutput = new JTextArea(30,30);
txtOutput.setText("");
txtOutput.setEditable(false);
ActionListener action = new InventoryOutput();
btnSearch.addActionListener(action);
JPanel panel = new JPanel();
panel.setLayout(null);
lblISBN.setBounds(10,10,50,25);
txtISBN.setBounds(55,10,125,25);
lblAuthor.setBounds(10,40,50,25);
txtAuthor.setBounds(55,40,125,25);
lblType.setBounds(10,70,50,25);
txtType.setBounds(55,70,125,25);
btnSearch.setBounds(30,130,150,25);
JScrollPane scrollArea = new JScrollPane(txtOutput);
scrollArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollArea.setBounds(200,10,350,200);
panel.add(scrollArea);
panel.add(lblISBN);
panel.add(txtISBN);
panel.add(lblAuthor);
panel.add(txtAuthor);
panel.add(lblType);
panel.add(txtType);
panel.add(btnSearch);
panel.add(scrollArea);
add(panel);
}
class InventoryOutput implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String inventoryString = "";
inventoryString += txtISBN.getText() + " - ";
inventoryString += "Author: " + txtAuthor.getText() + " - ";
inventoryString += "Type: " + txtType.getText() + " - ";
txtOutput.append(inventoryString + "\n");
txtISBN.setText("");
txtAuthor.setText("");
txtType.setText("");
}
}
}
答案 0 :(得分:0)
您可以创建private String chosenISBN
并在actionPerformed
方法中设置它的值。然后,您可以访问班级中的任何值。
如果需要,您甚至可以为chosenISBN
创建一个getter。
答案 1 :(得分:0)
你可以用这个代替你的代码......这是一个起点,你可以对这段代码做很多改进,但这样可行。将 REPOSITORY_FILE_PATH 更改为您的数据文件
class InventoryOutput implements ActionListener {
private final String REPOSITORY_FILE_PATH = "C:\\temp\\book-repo.txt";
private final File REPOSITORY_FILE = new File(REPOSITORY_FILE_PATH);
public void actionPerformed(ActionEvent event) {
String inventoryString = "";
String requestedISBN = txtISBN.getText().trim().toLowerCase();
String requestedAuthor = txtAuthor.getText().trim();
String requestedType = txtType.getText().trim();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(REPOSITORY_FILE));
String line;
while ((line = reader.readLine()) != null) {
String lineLtrim = line.toLowerCase().replaceAll("^\\{", ""); //{
String lineRtrim = lineLtrim.replaceAll("\\}$", ""); //}
String[] data = lineRtrim.split(","); //ISBN, Author, Type, Stock
if (data.length < 4) {
throw new IllegalArgumentException("bad datafile: All fields must be entered: " + line);
}
if (data[0].equals(requestedISBN)) {
inventoryString += txtISBN.getText() + " - Author: " + data[1] + " - Type: " + data[2] + " - Stock:" + data[3];
txtOutput.append(inventoryString + "\n");
return;
}
}
reader.close();
inventoryString += txtISBN.getText() + " - Not Found";
txtOutput.append(inventoryString + "\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
你可以添加这个main()代码进行测试,理想情况下你应该编写单元测试 - 来涵盖所有情况。
public static void main(String[] args) {
JFrame f = new InventoryInterface();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setVisible(true);
}
数据文件
{ISBN1234,me,drama,5}
{ISBN1235,me,Tech,0}
{ISBN1236,me,Fiction,2}
{ISBN1237,me,Audio/Kids,4}
不确定你没有得到哪一部分,
String
requestedISBN = txtISBN.getText().trim().toLowerCase();
输入的ISBN并进行转换
它在小写的情况下,我们可以与较低的数据进行比较if (data.length < 4) {
如果您自己编写代码(监听器),这对您来说应该是微不足道的。