[英]How can I return a value from a JDialog box to the parent JFrame?
我創建了一個模態JDialog框,其上有一個自定義繪圖和一個JButton。 當我單擊JButton時,JDialog框應該關閉,並且應該返回一個值。
我在父JFrame中創建了一個名為setModalPiece的函數,它接收一個值並將其設置為本地JFrame變量。
問題是從JDialog框中看不到此函數(即使JDialog框具有對父JFrame的引用)。
兩個問題:1)有沒有更好的方法將值從JDialog框返回到其父JFrame?
2)為什么不能使用傳遞給JDialog的JFrame的引用來訪問我的JFrame函數setModalPiece?
我通常這樣做:
Dialog dlg = new Dialog(this, ...);
Value result = dlg.showDialog();
Dialog.showDialog()
函數如下所示:
ReturnValue showDialog() {
setVisible(true);
return result;
}
由於在JDialog上將可見性設置為true是一種模態操作,因此OK按鈕可以將實例變量( result
)設置為對話框的選定結果(如果取消則為null
)。 在“確定/取消”按鈕方法中處理后,執行以下操作:
setVisible(false);
dispose();
將控制權返回給showDialog()
函數。
您應該通過向自定義JDialog
添加自定義方法getValue()
來執行相反的操作。
通過這種方式,您可以從JFrame
詢問對話框的值,而不是通過在JFrame
本身上調用某些內容來設置它。
如果您在這里查看有關對話框的Oracle教程,請說明
如果您正在設計自定義對話框,則需要設計對話框的API,以便查詢用戶選擇的對話框。 例如,CustomDialog有一個getValidatedText方法,該方法返回用戶輸入的文本。
(您可以找到CustomDialog
來源,看看他們如何設計您將設計自定義對話框)
我不知道我是否能夠以一種很酷的方式解釋我的方法...假設我需要productPrice和JDialog的數量來獲取用戶的信息,我需要從JFrame調用它。
將productPrice和ammount聲明為JDialog中的公共非靜態全局變量。
public float productPrice;
public int amount;
*這是在對話框的類全局范圍內。
在JDialog構造函數中添加這些行以確保模態
super((java.awt.Frame) null, true);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
*這在對話框的類構造函數中
讓我們說你的JDialog的類名是'MyJDialog',當你做這樣的事情時
MyJDialog question = new MyJDialog();
MyJDialog.setVisible(true);
// Application thread will stop here until MyJDialog calls dispose();
// this is an effect of modality
//
// When question calls for dispose(), it will leave the screen,
// but its global values will still be accessible.
float myTotalCostVar = question.productPrice * question.ammount;
// this is acceptable.
// You can also create public getter function inside the JDialog class,
// its safer and its a good practice.
*這適用於JFrame中的任何函數,並將調用JDialog來獲取信息。
當你將任何值傳遞給JFrame到JDialog然后在你想要調用的時候在jframe中創建jdialog的參數化構造函數。 例如,參數化的構造函數,如:
public EditProduct(java.awt.Frame parent, boolean modal, int no) {
//int no is number of product want to edit.
//Now we can use this pid in JDialog and perform whatever you want.
}
當您想要將值從JDialog傳遞到JFrame時,使用set和get方法創建一個bean類,使用vector創建值並在jframe中獲取這些值。 更多信息
這是我通常這樣做的方式。 我不確定,這就是為什么我創建了這個帖子:
添加一個接口到您的構造函數?
public class UploadConfimation extends JDialog {
private final JPanel contentPanel = new JPanel();
public interface GetDialogResponse{
void GetResponse(boolean response);
}
/**
* Create the dialog.
*/
public UploadConfimation(String title, String message, GetDialogResponse result) {
setBounds(100, 100, 450, 300);
setTitle(title);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JLabel lblMessage = new JLabel(message);
contentPanel.add(lblMessage);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("YES");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(true);
dispose();
}
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("NO");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.