gpt4 book ai didi

java - GUI 和二进制文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:28 27 4
gpt4 key购买 nike

我目前正在为我的一门类(class)开发一个项目,我们必须创建一个要求提供号码的 GUI,然后读取 10,000 个客户的二进制文件,并返回输入了客户号码的文件的其他信息。当输入 1-10,000 之外的数字时,我们还必须有一个以 JOptionPane 形式弹出的错误。我在错误消息方面遇到了一些问题,我的教授还说我们需要一个具有此签名的方法:

      private Customer getCustomer(long custNumber) throws IOException

搜索文件并返回客户对象。我也不确定具体应该在哪里做到这一点。所以我从我已经知道的开始,这就是我所拥有的:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class CustomerLocator extends JFrame
{
private JPanel panel;
private JLabel custNumLabel;
private JLabel custNameLabel;
private JLabel custDisLabel;
private JLabel custBalLabel;
private JLabel custPrefLabel;
private JTextField custNumField;
private JTextField custNameField;
private JTextField custDisField;
private JTextField custBalField;
private JTextField custPrefField;
private JButton findCustBut;
private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 500;

public CustomerLocator()
{
setTitle("Customer Locator");

setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

buildPanel();

add(panel);

setVisible(true);
}

private void buildPanel()
{
custNumLabel = new JLabel("Enter a valid Customer Number: ");

custNumField = new JTextField(10);

custNameLabel = new JLabel("Customer Name: ");

custNameField = new JTextField(10);
custNameField.setEditable(false);

custDisLabel = new JLabel("Customer Discount: ");

custDisField = new JTextField(10);
custDisField.setEditable(false);

custBalLabel = new JLabel("Customer Balance: ");

custBalField = new JTextField(10);
custBalField.setEditable(false);

custPrefLabel = new JLabel("Preferred? ");

custPrefField = new JTextField(10);
custPrefField.setEditable(false);

findCustBut = new JButton("Find this Customer!");

panel = new JPanel();

findCustBut.addActionListener(new ListenerToFindCustomer());

panel.add(custNumLabel);
panel.add(custNumField);
panel.add(findCustBut);
panel.add(custNameLabel);
panel.add(custNameField);
panel.add(custDisLabel);
panel.add(custDisField);
panel.add(custBalLabel);
panel.add(custBalField);
panel.add(custPrefLabel);
panel.add(custPrefField);

}
private class ListenerToFindCustomer implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String numEnteredStr;
long custNumEntered;
String custName;
boolean preferred;
String preferredDisplay;
double acctBal;
String acctBalDisplay;
double discount;
String discountDisplay;
numEnteredStr = custNumField.getText();
custNumEntered = Long.parseLong(numEnteredStr);
int ct=0;
try
{

FileInputStream inStream =
new FileInputStream("c:\\cps\\CustomerObjects.dat");
// DataInputStream inputFile = new DataInputStream(inStream);
ObjectInputStream objectInputFile =
new ObjectInputStream(inStream);


while(true)
{
ct++;
Customer obj = (Customer)objectInputFile.readObject();
//System.out.println(obj.getCustName());
if (custNumEntered == obj.getCustNum())
{
custName = obj.getCustName();
acctBal = obj.getBalance();
acctBalDisplay = Double.toString(acctBal);
discount = obj.getDiscount();
discountDisplay = Double.toString(discount);
preferred = obj.getPreferred();
preferredDisplay = Boolean.toString(preferred);

custNameField.setText(custName);
custBalField.setText(acctBalDisplay);
custPrefField.setText(preferredDisplay);
custDisField.setText(discountDisplay);

if (custNameField == null)
{
JOptionPane.showMessageDialog(null, "Error");
}
}

}
}
catch (Exception ex)
{
System.out.println(ex.toString());

}
}
}
public static void main(String[] args)
{
new CustomerLocator();
}
}

因此,JOptionPane 不会出现,所以我的问题是1. 如何让错误信息弹出?2. 我如何采用教授要求的方法?

最佳答案

你的代码

                if (custNameField == null)
{
JOptionPane.showMessageDialog(null, "Error");
}

应该阅读

                if (custName == null)
{
JOptionPane.showMessageDialog(null, "Error");
}

它也应该移到 while 之外环形。当到达文件末尾时,您还需要一种方法来中断循环。里面的一切,包括你的try { ... } catch (Exception ex) { ... }可以转移到您的教练建议的方法。您还应该更改 try-catch 以获得另一个 catch (IOException ioEx) { throw ioEx; }

关于java - GUI 和二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43121409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com