興味のある方は、ここで私が最後にやったことがあります。私はそれが最良の方法であるかどうかはわかりませんが、私の目的のために働いています。
// 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#バージョンである:
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;
}
は*「別のフォームからイベントをサブスクライブするためのベストプラクティスは何ですか?」 *あなたは 'JFrame'を意味しますか?私はあなたのことを話していないことに注意してください。 –
@AndrewThompson申し訳ありません。 C#では、フォームと呼ばれます。まだJavaの用語に精通していません。 – Eric
A-Ha! Netbeansはまた、開発者を「フォーム」として「JFrames」と呼んでいる。質問を編集していただきありがとうございます。 –