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