首页 > Java > java教程 > 正文

为什么使用 SwingPropertyChangeSupport 修改数组后我的 JTextArea 不更新?

DDD
发布: 2024-11-05 21:57:02
原创
159 人浏览过

Why Does My JTextArea Not Update After Modifying an Array Using SwingPropertyChangeSupport?

SwingPropertyChangeSupport 动态更新 JTextArea

SwingPropertyChangeSupport 是一个可用于监视对象属性更改的类。这允许在属性更改时通知其他对象,这对于相应地更新 GUI 很有用。

在此示例中,我们使用 SwingPropertyChangeSupport 允许在更改发生时更新 JTextArea 中显示的数组通过输入对话框输入。数组更新正常,但 GUI 未刷新。

以下是修改后的代码:

<code class="java">import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.SwingPropertyChangeSupport;

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

class GuiForUpdate extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private FocusListener focusListener;
private String mList;
private JButton changeArrayButton;
private JTextArea codeIn, displayOutput;
private int arrayIndex;
private JPanel displayPanel;
private ArrayForUpdating arrayForUpdate = new ArrayForUpdating();

public GuiForUpdate() {
    setSize(224, 180);
    layoutLeft();
    layoutDisplay();
    layoutBottom();
}

/**
 * adds a display area for array
 */
public void layoutDisplay() {
    displayPanel = new JPanel();
    add(displayPanel, BorderLayout.CENTER);
    displayOutput = new JTextArea();
    displayPanel.add(displayOutput);
    displayOutput.addFocusListener(focusListener);

    mList = arrayForUpdate.getBoundProperty();

    arrayForUpdate.addPropertyChangeListener(new PropertyChangeListener() {

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

    displayOutput.setText(mList);
}

/**
 * adds left hand side elements to left of GUI
 */
public void layoutLeft() {
    JPanel left = new JPanel();
    add(left, BorderLayout.WEST);
    codeIn = new JTextArea(2, 2);
    left.add(codeIn);
    codeIn.addFocusListener(focusListener);
}

/**
 * adds bottom elements to bottom of GUI
 */
public void layoutBottom() {
    JPanel bottom = new JPanel();
    changeArrayButton = new JButton("Modify array");
    changeArrayButton.addActionListener(this);
    bottom.add(changeArrayButton);
    add(bottom, BorderLayout.SOUTH);
}

/**
 * Process button clicks
 */
public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == changeArrayButton) {

        // first check if any code entered
        if (codeIn.getText().trim().length() != 0) {

            // call modifyMemory() method
            modifyArray();

        } else
            JOptionPane.showMessageDialog(null,
                    "Please enter something first.");
    }
}

/**
 * method to process modify array
 */
public void modifyArray() {

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

    // confirm if a string was entered
    if ((addressToModify != null) &amp;&amp; (addressToModify.length() > 0)) {

        // convert to integer if decimal address entered
        arrayIndex = Integer.parseInt(addressToModify);
    }
    // pass as integer
    processInput(arrayIndex);
}

public void processInput(int a) {

    String newValue = codeIn.getText();
    arrayForUpdate.instructionsIn(newValue, a);
}

public void updateDisplay() {
    displayOutput.setText(mList);
}
}

class ArrayForUpdating {

public static final String BOUND_PROPERTY = "bound property";
private String boundProperty = "";

private SwingPropertyChangeSupport spcSupport = new SwingPropertyChangeSupport(
        this);
private StringBuilder mList;
private int[] myArray;

public ArrayForUpdating() {

    myArray = new int[5];
    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();</code>
登录后复制

以上是为什么使用 SwingPropertyChangeSupport 修改数组后我的 JTextArea 不更新?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!