gpt4 book ai didi

java - SwingPropertyChangeSupport 从文件读取后刷新 GUI

转载 作者:行者123 更新时间:2023-12-04 05:36:24 27 4
gpt4 key购买 nike

继昨晚我的问题: SwingPropertyChangeSupport to dynamically update JTextArea ,我正在尝试在读取文本文件后刷新 GUI。

我有一个包含两位整数的数组。我可以通过在文本区域中输入一串数字来动态更新它,然后选择要修改的索引。该字符串被分成两位数的部分并按顺序添加。输入字符串并单击按钮后,将调用此方法:

public void modifyArray() {

// show dialog to retrieve entered address
addressToModify = (String) JOptionPane
.showInputDialog("At which location?");

// convert to integer if decimal address entered
memAddress = Integer.parseInt(addressToModify);
arrayForUpdate.instructionsIn(codeIn.getText(), memAddress);
}

我的这部分代码现在按预期工作。也就是说,如果我输入“123456”然后输入位置“0”(或“0000”),则显示会更新为如下所示:
Index     0000   Value:   12
Index 0001 Value: 34
Index 0002 Value: 56
Index 0003 Value: 00

我还有一个文本文件,其中包含一个由四位数字组成的字符串,提供数组索引,然后是一系列要添加到数组中的两位数字值。我可以通过 gui 按钮/文件选择器加载它。示例文件的内容是:
0000123456

我有以下方法来处理文件:
    public void readRecord(File fileName) {

// create file reader
try {
FileReader reader = null;
try {
// open input file
reader = new FileReader(fileName);

// create scanner to read from file reader
Scanner in = new Scanner(reader);

// read each line and remove whitespace
while (in.hasNextLine()) {
String line = in.nextLine().trim();
parseRecord(line);
}

} finally {
// close reader assuming it was successfully opened
if (reader != null)
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void parseRecord(String record) {

// create address substring from start, 4 long
String addrString = record.substring(0, 3);
int s1Address = Integer.parseInt(addrString);

// create binary data substring (4 from start, up to end)
String dataString = record.substring(4, record.length());

// pass data string as parameter to InstructionsIn method
arrayForUpdate.instructionsIn(dataString, s1Address);
}

在上述两种情况下,都会调用“instructionsIn”方法。在第一种情况下,这会导致显示被更新,但在第二种情况下却没有,我不知道为什么。我希望有人能够发现我遗漏的东西。

这是运行的代码的完整简化版本:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.SwingPropertyChangeSupport;

public class PropertyChangeExample extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private String addressToModify, mList;
private int memAddress;
private JTextArea codeIn, displayOutput;
private S_Record sRec;
private JButton loadButton, modifyArrayButton;
private FocusListener focusListener;
private JPanel displayPanel;
private ArrayForUpdate arrayForUpdate = new ArrayForUpdate();

public static void main(String[] arg) {
PropertyChangeExample display = new PropertyChangeExample();
display.setVisible(true);
}

public PropertyChangeExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(450, 170);
layoutLeft();
layoutDisplay();
layoutBottom();
}

public void layoutDisplay() {
displayPanel = new JPanel();
add(displayPanel, BorderLayout.CENTER);
displayOutput = new JTextArea(32, 38);
displayPanel.add(displayOutput);
displayOutput.addFocusListener(focusListener);

mList = arrayForUpdate.getBoundProperty();
displayOutput.setText(mList);

arrayForUpdate.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (pcEvt.getPropertyName().equals(
ArrayForUpdate.BOUND_PROPERTY)) {
mList = (pcEvt.getNewValue().toString());
displayOutput.setText(mList);
}
}
});
}

public void layoutLeft() {
JPanel left = new JPanel();
add(left, BorderLayout.WEST);
codeIn = new JTextArea(10, 7);
left.add(codeIn, BorderLayout.NORTH);
codeIn.addFocusListener(focusListener);
}

public void layoutBottom() {
JPanel bottom = new JPanel();
bottom.setBackground(Color.LIGHT_GRAY);
loadButton = new JButton("Load file");
loadButton.addActionListener(this);
bottom.add(loadButton);
add(bottom, BorderLayout.SOUTH);
modifyArrayButton = new JButton("Add value to array");
modifyArrayButton.addActionListener(this);
bottom.add(modifyArrayButton);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == modifyArrayButton) {
modifyArray();
}

if (ae.getSource() == loadButton) {
processInputFile();
}
}

public void modifyArray() {
addressToModify = (String) JOptionPane
.showInputDialog("At which location?");

// convert to integer if decimal address entered
memAddress = Integer.parseInt(addressToModify);
arrayForUpdate.instructionsIn(codeIn.getText(), memAddress);
}

public void processInputFile() {

sRec = new S_Record();
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
// create the file
File file = chooser.getSelectedFile();

// pass to readRecord method in S_Record class
sRec.readRecord(file);
}
}
}

class S_Record {

private ArrayForUpdate arrayForUpdate = new ArrayForUpdate();

public void readRecord(File fileName) {

// create file reader
try {
FileReader reader = null;
try {
// open input file
reader = new FileReader(fileName);

// create scanner to read from file reader
Scanner in = new Scanner(reader);

// read each line and remove whitespace
while (in.hasNextLine()) {
String line = in.nextLine().trim();
parseRecord(line);
}
} finally {
// close reader assuming it was successfully opened
if (reader != null)
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void parseRecord(String record) {

// create address substring from start, 4 long
String addrString = record.substring(0, 3);

int s1Address = Integer.parseInt(addrString);

// create binary data substring (4 from start, up to end)
String dataString = record.substring(4, record.length());

// pass data string as parameter to InstructionsIn method
arrayForUpdate.instructionsIn(dataString, s1Address);
}
}

class ArrayForUpdate {

public static final String BOUND_PROPERTY = "bound property";
private String boundProperty = "";
private SwingPropertyChangeSupport spcSupport = new SwingPropertyChangeSupport(
this);
private int[] myArray;
private final int MEM_LOCATIONS = 6;
/** StringBuilder object for displaying memory */
private StringBuilder mList;

public ArrayForUpdate() {

myArray = new int[MEM_LOCATIONS];
for (int i = 0; i < myArray.length; i++) {
myArray[i] = 0;
}
setArrayyDisplayString();
}

/**
* method to create formatted string of array
*/
public void setArrayyDisplayString() {

// create StringBuilder for display in memory tab
mList = new StringBuilder();
for (int i = 0; i < myArray.length; i++) {

mList.append(String.format("%10s %04x %10s %02x", "Index: ", i,
"Value: ", myArray[i]));
mList.append("\n");
}
setBoundProperty(mList.toString());
}

/**
* This method takes in a string passed through from the GUI
*/
public void instructionsIn(String codeIn, int loc) {

String code = codeIn.trim();
int len = code.length();
int chunkLength = 2; // the length of each chunk of code
int i = 0;

// traverse entered code and split into 2 digit chunks
for (i = 0; i < len; i += chunkLength) {

String chunk = code.substring(i, Math.min(len, i + chunkLength));
int oc = Integer.parseInt(chunk, 16);

// add the data to the array
setArrayData(loc, oc);
loc++;
}
}

/**
* method to add data to the array
*/
public void setArrayData(int a, int memData) {
myArray[a] = memData;
setArrayyDisplayString();
}

public String getBoundProperty() {
return boundProperty;
}

/**
* Method to implement changes to array for display
*
* @param boundProperty - the String representing the memory array
*/
public void setBoundProperty(String boundProperty) {
String oldValue = this.boundProperty;
String newValue = boundProperty;
this.boundProperty = newValue;
spcSupport.firePropertyChange(BOUND_PROPERTY, oldValue, newValue);
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
spcSupport.addPropertyChangeListener(listener);
}
}

最佳答案

这看起来是引用的问题。

您似乎至少有两个 ArrayForUpdate 对象,一个由 GUI 监听,另一个完全不同的实例保存在 S_Record 类中。后一种是从文件中获取信息的,而前一种,GUI 监听的不是。解决方案:确保只有一个 ArrayForUpdate 对象在两个地方都使用。也许您可以通过其构造函数将 GUI 实例的引用传递到您的 S_Record 对象中?

关于java - SwingPropertyChangeSupport 从文件读取后刷新 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11873141/

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