SwingPropertyChangeSupport to Dynamically Update JTextArea
SwingPropertyChangeSupport is a class that can be used to monitor changes in the properties of an object.这允许在属性更改时通知其他对象,这对于相应地更新GUI很有用。
在此示例中,我们使用的是swingpropertychangesupport允许在更改时显示在jtextarea中显示的数组通过输入对话框输入。数组的更新确定,但是GUI没有刷新。这是修改的代码:
arg){ guiforupdate display = new GuiforUpdate(); display.setvisible(true); } } guiforupdate扩展了Jframe实现ActionListener { /** * */ 私有静态最终长序列= 1L; 私人焦点列表焦点列表; 私人字符串mlist; 私人jbutton changearraybutton; 私有JTEXTAREA CODERIN,DISSISOUTUP; 私有int arrayIndex; 私人jpanel displaypanel; 私有Arrayforupdating arrayforupDate = new ArrayForupDating(); public guiforupdate(){ 设定(224,180); layoutleft(); layoutdisplay(); layoutbottom(); } /** *添加了一个数组的显示区域 */ public void layoutdisplay(){ DisplayPanel = new Jpanel(); 添加(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()。等于( arrayforupdating.bound_property)){ mlist =(pcevt.getNewValue()。toString()); 更新isPlay(); } } }); displayOutput.setText(mlist); } /** *在GUI的左侧添加左侧元素 */ public void layoutleft(){ jpanel left = new jpanel(); 添加(左,borderlayout.west); Codlein = New Jtextarea(2,2); left.add(codlein); codlein.Addfocuslistener(focusListener); } /** *将底部元素添加到GUI的底部 */ public void layoutbottom(){ jpanel bottom = new jpanel(); changearraybutton = new jbutton(“ modify array”); changearraybutton.addactionListener(this); botton.Add(changearraybutton); 添加(底部,borderlayout.south); } /** *过程按钮点击 */ 公共无效的操作表面(ActionEvent AE){ if(ae.getSource()== changearraybutton){ //首先检查是否输入任何代码 if(codein.getText()。trim()。长度()!= 0){ //调用modifyMemory()方法 modifyArray(); } 别的 joptionpane.showmessagedialog(null, “请先输入一些东西。”); } } /** *处理修改数组的方法 */ public void modifyArray(){ //显示以检索输入地址的对话框 字符串地址tomodify =(string)joptionpane .showInputDialog(“在哪个位置?”); //确认是否输入字符串 if((((adverseTomodify!= null) //如果输入十进制地址,请转换为整数 arrayIndex = integer.parseint(adversionTomodify); } //通过整数通过 ProcessInput(arrayIndex); } public void processInput(int a){ 字符串newValue = codein.getText(); arrayforupdate.instructionsin(newValue,a); } public void updatedisplay(){ displayOutput.setText(mlist); } } 类arrayforupdating { 公共静态最终字符串bound_property =“ bound属性”; 私有字符串boundproperty =“”; 私有swingpropertychangesupport spcsupport =新的swingpropertychangesupport( 这); 私人弦乐器mlist; 私人int [] myarray; public arrayforupdating(){ myArray = new Int [5]; for(int i = 0; i免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3