2011-11-15 6 views
2

다른 JFrame의 이벤트를 구독하는 가장 좋은 방법은 무엇입니까? 예를 들어, "설정"양식이 있고 사용자가 설정 양식에서 OK를 누르면이 양식에 대한 기본 양식을 알기 쉽게 설정을 검색 할 수 있습니다.다른 JFrames의 GUI 이벤트를 구독하는 방법

감사합니다.

public void showSettingsButton_Click() { 
    frmSettings sForm = new sForm(this._currentSettings); 
    //sForm.btnOkay.Click = okayButtonClicked; // What to do here? 
    sForm.setVisible(true); 
} 

public void okayButtonClicked(frmSettings sForm) { 
    this._currentSettings = sForm.getSettings(); 
} 
+0

*? "또 다른 형태의 이벤트에 가입하는 가장 좋은 방법은 무엇입니까"

// Method called when the "Show Settings" button is pressed from the main JFrame private void showSettingsButton_Click() { // Create new settings form and populate with my settings frmSettings sForm = new frmSettings(this.mySettings); // Get the "Save" button and register for its click event... JButton btnSave = sForm.getSaveButton(); btnSave.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { SaveSettings(sForm); } }); // Show the settings form sForm.setVisible(true); } // Method called whenever the save button is clicked on the settings form private void SaveSettings(frmSettings sForm) { // Get the new settings and assign them to the local member Settings newSettings = sForm.getSettings(); this.mySettings = newSettings; } 

그리고, 경우 나 같은, 당신이 .NET 관점에서 오는

, 여기에 C# 버전입니다 *'JFrame '을 의미합니까? 내가 '당신'이라고 말하지는 않습니다. –

+0

@AndrewThompson 죄송합니다. C#에서는 형식이라고합니다. 아직 Java 용어에 익숙하지 않습니다. – Eric

+1

A-Ha! Netbeans은 또한 'JFrames'를 '폼'이라고도 부르는 데 사용합니다. 질문을 편집 해 주셔서 감사합니다. –

답변

2

누군가가 뭔가 여기에 설정을 변경되었음을, 이벤트를 게시 :

여기에 내 이상적인 인터페이스입니다. 이 구체 이벤트에 등록한 가입자가 그것에 대해 알림을 받고 작업을 수행 할 수 있습니다. 여기서 설정을 가져옵니다. 이를 게시자/구독자라고합니다.

이 경우 Eventbus을 사용하거나 직접 작은 것을 구현할 수 있습니다.

2

하나의 접근법은 단 하나의 JFrame을 갖는 것입니다. 다른 모든 '자유 부동 최상위 컨테이너'는 모달 대화 상자가 될 수 있습니다. 기본 GUI 액세스는 현재 대화 상자가 닫힐 때까지 차단되며, 주 프레임의 코드는 닫힌 후 대화 상자의 설정을 확인할 수 있습니다.

0

관심있는 사람은 다음과 같이 결론을 냈습니다. 나는 그것이 최선의 방법인지는 모르겠지만 그것이 내 목적을 위해 일하고있다.

private void showSettingsButton_Click(object sender, EventArgs e) 
{ 
    frmSettings sForm = new frmSettings(this.mySettings); 
    sForm.btnSave += new EventHandler(SaveSettings); 
    sForm.Show(); 
} 

private void SaveSettings(object sender, EventArgs e) 
{ 
    frmSettings sForm = (frmSettings)sender; // This isn't the exact cast you need.. 
    Settings newSettings = sForm.Settings; 
    this.mySettings = newSettings; 
}