gpt4 book ai didi

java - 如何设置要读入 GUI 中文本字段的某些文本?

转载 作者:行者123 更新时间:2023-12-01 13:12:57 25 4
gpt4 key购买 nike

我正在使用二进制 IO 和事件处理进行作业。看起来像这样,我想我已经掌握了大部分基本代码,现在开始讨论困难的事情了。如何将 .bin 文件中的文本读取到单独的文本字段中? enter image description here

我的字段当前是空白的,但是当读入时,它们应该看起来像这样,并且我应该能够使用底部的两个按钮来解析单独的信息集。

这是我到目前为止所拥有的。我有一个 RecordViewerUI JFrame 表单、一个 RecordViewerPanel JFrame 面板和一个 SalesAgent java 类,该类包含四个变量的所有 getter 和 setter/构造函数。

这是我的面板。对生成的代码感到抱歉。预先感谢任何人可以提供的任何帮助。

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RecordViewerPanel extends javax.swing.JPanel {

String filename = "sales.bin";
ArrayList<SalesAgent> customer = new ArrayList<>();
int curIndex = 0;

/**
* Creates new form RecordViewerPanel
*/
public RecordViewerPanel() throws IOException, ClassNotFoundException {
initComponents();
try {
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(filename)));
ArrayList<SalesAgent> salesForce = new ArrayList<SalesAgent>();
String first = in.readUTF();
String last = in.readUTF();
double sales = in.readDouble();
Date date = (Date) (in.readObject());
} catch (FileNotFoundException ex) {
Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
}

}

private void refreshUI() {
SalesAgent sale = (customer.get(curIndex));
firstTextField.setText(sale.getFirst());
lastTextField.setText(sale.getLast());
salesTextField.setText("$" + String.valueOf(sale.getSales()));
dateTextField.setText(String.valueOf(sale.getDate()));
recordPanel.repaint();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

filePanel = new javax.swing.JPanel();
fileLabel = new javax.swing.JLabel();
fileLabel1 = new javax.swing.JLabel();
recordPanel = new javax.swing.JPanel();
firstTextField = new javax.swing.JTextField();
lastTextField = new javax.swing.JTextField();
salesTextField = new javax.swing.JTextField();
dateTextField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
previousButton = new javax.swing.JButton();
nextButton = new javax.swing.JButton();

filePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

fileLabel.setBackground(new java.awt.Color(204, 204, 204));
fileLabel.setText("sales.bin");

fileLabel1.setBackground(new java.awt.Color(204, 204, 204));
fileLabel1.setText("Current file:");

javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
filePanel.setLayout(filePanelLayout);
filePanelLayout.setHorizontalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(fileLabel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(fileLabel1)
.addContainerGap(562, Short.MAX_VALUE)))
);
filePanelLayout.setVerticalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addContainerGap())
.addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addContainerGap()))
);

recordPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

firstTextField.setText("jTextField1");

lastTextField.setText("jTextField1");

salesTextField.setText("jTextField1");

dateTextField.setText("jTextField1");

jLabel1.setText("First Last Sales Date");

previousButton.setText("<<");
previousButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
previousButtonActionPerformed(evt);
}
});

nextButton.setText(">>");
nextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout recordPanelLayout = new javax.swing.GroupLayout(recordPanel);
recordPanel.setLayout(recordPanelLayout);
recordPanelLayout.setHorizontalGroup(
recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 203, Short.MAX_VALUE))
.addGroup(recordPanelLayout.createSequentialGroup()
.addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(dateTextField)))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, recordPanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
);
recordPanelLayout.setVerticalGroup(
recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(previousButton)
.addComponent(nextButton))
.addGap(27, 27, 27))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(filePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>

private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (curIndex > 0) {
curIndex--;
} else if (curIndex == 0) {
curIndex = 4;
}
customer.get(curIndex);
}

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (curIndex < 4) {
curIndex++;
} else if (curIndex == 4) {
curIndex = 0;
}
customer.get(curIndex);
}
// Variables declaration - do not modify
private javax.swing.JTextField dateTextField;
private javax.swing.JLabel fileLabel;
private javax.swing.JLabel fileLabel1;
private javax.swing.JPanel filePanel;
private javax.swing.JTextField firstTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField lastTextField;
private javax.swing.JButton nextButton;
private javax.swing.JButton previousButton;
private javax.swing.JPanel recordPanel;
private javax.swing.JTextField salesTextField;
// End of variables declaration
}

最佳答案

这个问题很难回答,因为我不了解文件格式或 Customer 类,但基本上,您需要从您从文件中读取的值,将其添加到 customer List 并调用 refreshUI

try {
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(filename)));
String first = in.readUTF();
String last = in.readUTF();
double sales = in.readDouble();
Date date = (Date) (in.readObject());
Customer cust = ...;
customer.add(cust);
} catch (FileNotFoundException ex) {
Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
}
refreshUI();

关于java - 如何设置要读入 GUI 中文本字段的某些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702238/

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