JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 一個Class輸入的Value怎樣傳到另一個Class??
dick0101





發文: 33
積分: 0
於 2008-03-13 23:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在這個程式裡想將InputPanel.java中的TextField裡的H、L、a、n傳到PotentialWell中的int H、L、a、n中?從而令User能控制PolyLine?

Tunnel_Effect.zip (5.41k)


reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
honeyaaa





發文: 159
積分: 0
於 2008-03-13 23:47 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以利用介面來傳達,例如以下範例。
個人淺見,請多多指教。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public interface IDeliverObject {
  public Object getDeliverObject();
}
 
public class A implements IDeliverObject{
  private String str ;
  
  public A(){
    
  }
  public void setStr(String str){
    this.str = str ;
  }
  @Override
  public Object getDeliverObject() {    
    return (Object)str;
  }
}
 
public class B {
  private IDeliverObject deliverObject;
  private String str;
 
  public B() {
    
  }
  
  public void addDeliverObjectEvent(IDeliverObject deliverObject){
    this.deliverObject = deliverObject ;
  }
  
 
  public String getStr() {
    if(deliverObject != null )
      str = (String)deliverObject.getDeliverObject() ;
    return str;
  }
}
 
public class Test {
  public static void main(String[] args) {
    A a = new A();
    B b = new B();
    b.addDeliverObjectEvent(a);
    a.setStr("123");
    System.out.println(b.getStr());
  }
}


honeyaaa edited on 2008-03-13 23:52
reply to postreply to post
努力,是成功唯一的途徑。
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
fillano





發文: 258
積分: 0
於 2008-03-14 09:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這樣有一點奇怪,不是應該在TextField註冊ActionListener來處理嗎?

reply to postreply to post
Sapere aude! Habe Mut, dich deines eigenen Verstandes zu bedienen! ist also der Wahlspruch der Aufklärung.
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-14 22:20 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不是很懂,請問能解釋一下嗎?

reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
fillano





發文: 258
積分: 0
於 2008-03-14 23:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
稍微看過你的程式,但是還沒深入了解。

不過你要讓其他類別控制PotentialWell的話,你得把你寫好的繪圖方法整理一下,抽繹出一些方法,然後把H,L,a,n等當作參數傳進去。

另外,要讓TextField輸入數值時呼叫PotentialWell中的方法來繪圖的話,當然要用到Listener阿(這是awt/swing等的基本吧)。

你可以讓InputPanel implements ActionListener介面,然後在InputPanel增加一個actionPerformed方法,呼叫TextField的addActionListener方法把InputPanel(在這裡會用this)加進去成為TextField的ActionListener。在actionPerformed方法中讀取TextField輸入的資料,傳給上述的繪圖方法來做相應的動作。

這樣TextField輸入資料時,就會呼叫InputPanel的actionPerformed方法來幫你把圖畫好。


fillano edited on 2008-03-14 23:29
reply to postreply to post
Sapere aude! Habe Mut, dich deines eigenen Verstandes zu bedienen! ist also der Wahlspruch der Aufklärung.
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-15 00:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
簡單來說,我係想寫一個Program,可以UserInput,然後Plot出我想要的畫

我看書大都只教一個變數,用一個Listener便可,但多個變數要怎樣做?

順便問問,如果我fix了plot畫部份個dimension係600x600,但如果UserInput的數令另幅圖超出這個Dimension但我想繼續plot(即是要有Srcoll),可以點做?


dick0101 edited on 2008-03-15 00:10
reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
fillano





發文: 258
積分: 0
於 2008-03-15 22:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
四個變數,四個TextField,可以用同一個Listener處理阿。處理的時候同時從四個TextField取得輸入的值來計算不就解決了?

Swing我也不熟,scroll怎麼放上去,你自己研究一下吧。(Listener,或換個字眼,Observer Pattern,是Java裡面處理事件的基本結構,所以我比較清楚)


reply to postreply to post
Sapere aude! Habe Mut, dich deines eigenen Verstandes zu bedienen! ist also der Wahlspruch der Aufklärung.
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-16 23:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
研究了很久也不行呀,我不明白怎樣將TextField取代int呀.......

reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
honeyaaa





發文: 159
積分: 0
於 2008-03-16 23:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你要有動作就處理嗎?,還是要全部輸入值完後來動作呢?

reply to postreply to post
努力,是成功唯一的途徑。
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-17 00:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
要全部輸入值完後來動作,是不是要加多一個"確定"按鈕?

reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
honeyaaa





發文: 159
積分: 0
於 2008-03-17 00:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
嗯,所以才要問你是即時輸入就改變,還是要多一個確定鈕。

reply to postreply to post
努力,是成功唯一的途徑。
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-17 00:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
要多一個確定鈕,因為變數多於一個

reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-17 00:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我解決唔到的係,當我create了TextField後,如何轉成一個Int並在別的Class使用。

如果用Silder我就明白怎樣做...


reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
honeyaaa





發文: 159
積分: 0
於 2008-03-17 01:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
對不起,因為我一開始沒有看的到你程式碼,你如果是單純類別傳值的話,只要封裝好你的a,n,H,L值,然後監聽元件事件,在重繪JPanel就可以,這裡我是用你最後說的加一個按鈕,你試試看吧,TextField的值轉成int可用Integer.parseInt() method。
個人淺見^^,希望能幫到你。

Tunnel_Effect.rar (7.4k)


honeyaaa edited on 2008-03-17 01:29
reply to postreply to post
努力,是成功唯一的途徑。
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-17 23:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
問題解決了,謝謝,但其實我的progame還有很多別的問題有待解決=="
例如menu,我想製作個menu可以轉去別的Frame..


reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
dick0101





發文: 33
積分: 0
於 2008-03-18 12:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如何檢查輸入的數值是否正確?

例如H,L,n,a不是數字或大於某個數就出Error訊息....


reply to postreply to post
作者 Re:一個Class輸入的Value怎樣傳到另一個Class?? [Re:dick0101]
honeyaaa





發文: 159
積分: 0
於 2008-03-18 13:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
@.@
這個你可以自訂Exception來處理。


reply to postreply to post
努力,是成功唯一的途徑。
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8